单击另一个 CardView 时,CardView 会更改文本颜色
CardView changes text color when when clicking on another CardView
我在单击 CardView 时遇到问题。我有一个带有 CardView 的 RecyclerView,我想在其中做一个 select。第一次点击正确,但当我点击另一个时,前一个 CardView 的文本颜色变为白色。
这是我的适配器的代码:
holder.setiRecyclerItemSelectedListener(new IRecyclerItemSelectedListener() {
@Override
public void onItemSelectedListener(View view, int pos) {
// Loop all cards in card list
for (CardView cardView: cardViewList) {
if (cardView.getTag() == null) // Only available card time slots
{
cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot.setTextColor(context.getColor(android.R.color.tab_indicator_text));
holder.txt_time_slot_description.setTextColor(context.getColor(android.R.color.tab_indicator_text));
}
}
// Color of selected card time slot
holder.card_time_slot.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_time_slot.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot_description.setTextColor(context.getColor(R.color.colorWhite));
// Send broadcast to enable button NEXT
Intent intent = new Intent(Common.KEY_ENABLE_BUTTON_NEXT);
intent.putExtra(Common.KEY_TIME_SLOT, position); // Put index of time slot we have selected
intent.putExtra(Common.KEY_STEP, 3);
localBroadcastManager.sendBroadcast(intent);
}
});
图片:
第一个做对了
第二个没有
我认为你从 }
使用它的机器中隐含了所选卡片时间功能的颜色,并在不更改它的情况下结束前一个功能,这可能会有帮助
我猜你应该使 RecyclerView 中的视图无效。因此,根据您的问题,我认为您应该调用 notifyDataSetChanged()
函数,或者您应该通过调用 notifyItemChanged(position)
使 RV 中的特定项目无效。希望对你有帮助
在适配器中定义选定的索引:
int selectedIndex = -1;
您可以使用此方法访问 recyclerView 的子视图:
findViewHolderForAdapterPosition(position);
更改 onClicklistener:
if (selectedIndex != -1)
{
YourHolder holderOld = (YourHolder) recycleView.findViewHolderForAdapterPosition(selectedIndex);
holderOld.cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite));
holderOld.txt_time_slot.setTextColor(context.getColor(android.R.color.tab_indicator_text));
holderOld.txt_time_slot_description.setTextColor(context.getColor(android.R.color.tab_indicator_text));
}
selectedIndex = pos;
holder.card_time_slot.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_time_slot.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot_description.setTextColor(context.getColor(R.color.colorWhite));
我在单击 CardView 时遇到问题。我有一个带有 CardView 的 RecyclerView,我想在其中做一个 select。第一次点击正确,但当我点击另一个时,前一个 CardView 的文本颜色变为白色。
这是我的适配器的代码:
holder.setiRecyclerItemSelectedListener(new IRecyclerItemSelectedListener() {
@Override
public void onItemSelectedListener(View view, int pos) {
// Loop all cards in card list
for (CardView cardView: cardViewList) {
if (cardView.getTag() == null) // Only available card time slots
{
cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot.setTextColor(context.getColor(android.R.color.tab_indicator_text));
holder.txt_time_slot_description.setTextColor(context.getColor(android.R.color.tab_indicator_text));
}
}
// Color of selected card time slot
holder.card_time_slot.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_time_slot.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot_description.setTextColor(context.getColor(R.color.colorWhite));
// Send broadcast to enable button NEXT
Intent intent = new Intent(Common.KEY_ENABLE_BUTTON_NEXT);
intent.putExtra(Common.KEY_TIME_SLOT, position); // Put index of time slot we have selected
intent.putExtra(Common.KEY_STEP, 3);
localBroadcastManager.sendBroadcast(intent);
}
});
图片:
第一个做对了
第二个没有
我认为你从 }
使用它的机器中隐含了所选卡片时间功能的颜色,并在不更改它的情况下结束前一个功能,这可能会有帮助
我猜你应该使 RecyclerView 中的视图无效。因此,根据您的问题,我认为您应该调用 notifyDataSetChanged()
函数,或者您应该通过调用 notifyItemChanged(position)
使 RV 中的特定项目无效。希望对你有帮助
在适配器中定义选定的索引:
int selectedIndex = -1;
您可以使用此方法访问 recyclerView 的子视图:
findViewHolderForAdapterPosition(position);
更改 onClicklistener:
if (selectedIndex != -1)
{
YourHolder holderOld = (YourHolder) recycleView.findViewHolderForAdapterPosition(selectedIndex);
holderOld.cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite));
holderOld.txt_time_slot.setTextColor(context.getColor(android.R.color.tab_indicator_text));
holderOld.txt_time_slot_description.setTextColor(context.getColor(android.R.color.tab_indicator_text));
}
selectedIndex = pos;
holder.card_time_slot.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_time_slot.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot_description.setTextColor(context.getColor(R.color.colorWhite));