为什么 RecyclerView item 只在点击两次后才改变背景颜色?
Why does the RecyclerView item only change background color after clicking on it twice?
我一直在探索 Recyclerview 和 Cardview,我偶然发现了一个我不知道如何处理的问题。我的 recyclerview 工作正常,但是当我让 recyclerview 的项目背景在点击时改变颜色时,它表现得很奇怪。
好的,recyclerview 正常加载,但是当我第一次单击某个项目时,该项目的背景颜色闪烁其新颜色,但随后保持其默认颜色。当我第二次单击任何项目时,recyclerview 的项目才会更改其背景颜色,因为它应该与单击的项目的其余部分一起更改。
从我添加到代码中的 Log.d
中,我发现第一次点击与第二次(以及其余)点击的不同之处在于 public PremiseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
class PremiseAdapter
。我发现 与我的问题类似,但我认为我的代码不是这样工作的。
日志如下。
- 加载 recyclerview 及其 Cardview 项目
.... D/rcvTest: PremiseAdapter.PremiseAdapter
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: ---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder
.... D/rcvTest: [ViewHolder].ViewHolder.TOP
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: FroyYo Mart
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM FroyYo Mart
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: ---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder
.... D/rcvTest: [ViewHolder].ViewHolder.TOP
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: Ice Cream Store
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM Ice Cream Store
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
- 首先点击项目
.... D/rcvTest: [ViewHolder].ViewHolder.listener: null
.... D/rcvTest: [ViewHolder].ViewHolder.getAdapterPosition(): 1
.... D/rcvTest: [ViewHolder].ViewHolder.position: 1
.... D/rcvTest: getLayoutPosition(): 1
.... D/rcvTest: [ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: ---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder
.... D/rcvTest: [ViewHolder].ViewHolder.TOP
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: Ice Cream Store
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM Ice Cream Store
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
- 第二次(以及其余)点击该项目
.... D/rcvTest: [ViewHolder].ViewHolder.listener: null
.... D/rcvTest: [ViewHolder].ViewHolder.getAdapterPosition(): 0
.... D/rcvTest: [ViewHolder].ViewHolder.position: 0
.... D/rcvTest: getLayoutPosition(): 0
.... D/rcvTest: [ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: FroyYo Mart
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM FroyYo Mart
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
.... D/rcvTest: PremiseAdapter.getItemCount
这是我的 PremiseAdapter.java
文件,其中包含我的 PremiseAdapter
class 和 ViewHolder
class.
public class PremiseAdapter extends RecyclerView.Adapter<PremiseAdapter.ViewHolder> {
private ArrayList<Premise> mPremiseData;
private Context context;
private int selected_position = -1;
OnItemClickListener onItemClickListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener itemClickListener) {
onItemClickListener = itemClickListener;
}
public PremiseAdapter(Context context, ArrayList<Premise> mPremiseData) {
Log.d("rcvTest", "PremiseAdapter.PremiseAdapter");
this.mPremiseData = mPremiseData;
this.context = context;
}
//Required method for creating the viewholder objects.
@NonNull
@Override
public PremiseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("rcvTest", "---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder");
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_premises, parent, false), onItemClickListener);
}
//Required method that binds the data to the viewholder.
@Override
public void onBindViewHolder(@NonNull PremiseAdapter.ViewHolder holder, int position) {
// Get current premise.
// Populate the textviews with data.
}
@Override
public int getItemCount() {
Log.d("rcvTest", "PremiseAdapter.getItemCount");
return mPremiseData.size();
}
/*----------------------------------- CLASS VIEWHOLDER -------------------------------------------*/
/*----------------------------------- CLASS VIEWHOLDER -------------------------------------------*/
/*class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {*/
class ViewHolder extends RecyclerView.ViewHolder {
// Member Variables for the TextViews
//Constructor for the ViewHolder, used in onCreateViewHolder().
ViewHolder(@NonNull View itemView, OnItemClickListener listener) {
super(itemView);
cardView = itemView.findViewById(R.id.cardview_premise);
Log.d("rcvTest", "[ViewHolder].ViewHolder.TOP");
// Initialize the views.
// Set the OnClickListener to the entire view.
int clickedColor = Color.parseColor("#D5F5E3");
itemView.setOnClickListener((v) -> {
int position = getAdapterPosition();
Log.d("rcvTest", "[ViewHolder].ViewHolder.listener: " + listener);
Log.d("rcvTest", "[ViewHolder].ViewHolder.getAdapterPosition(): " + getAdapterPosition());
Log.d("rcvTest", "[ViewHolder].ViewHolder.position: " + position);
Log.d("rcvTest", "getLayoutPosition(): " + getLayoutPosition());
if (position != RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION");
itemView.setBackgroundColor(clickedColor);
} else if (position == RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position == RecyclerView.NO_POSITION");
itemView.setBackgroundColor(clickedColor);
}
notifyItemChanged(position);
notifyItemChanged(getLayoutPosition());
});
}
void bindTo(Premise currentPremise) {
// Populate the textviews with data.
Log.d("rcvTest", "[ViewHolder].bindTo.TOP: " + currentPremise.getPremiseName());
}
}
}
试试这个。最近我编写了这个并且它工作正常,希望它也能解决你的问题。
int row_index = -1;
holder.itemView.setOnClickListener(v -> {
row_index = position;
notifyDataSetChanged();
});
if (row_index == position) {
holder.layout.setBackgroundColor(Color.parseColor("#ff6600"));
} else {
holder.layout.setBackgroundColor(Color.parseColor("#ffffff"));
}
我通常在 onbindviewholder 中实现 onclick 侦听器,它们似乎工作正常。我建议你试试看。
将这段代码移动到onbindviewholder
int clickedColor = Color.parseColor("#D5F5E3");
/*itemView.setBackgroundColor(defaultColor);*/
holder.itemView.setOnClickListener((v) -> {
int position = getAdapterPosition();
Log.d("rcvTest", "[ViewHolder].ViewHolder.listener: " + listener);
Log.d("rcvTest", "[ViewHolder].ViewHolder.getAdapterPosition(): " + getAdapterPosition());
Log.d("rcvTest", "[ViewHolder].ViewHolder.position: " + position);
Log.d("rcvTest", "getLayoutPosition(): " + getLayoutPosition());
if (position != RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION");
/*listener.onItemClick(position);*/
itemView.setBackgroundColor(clickedColor);
} else if (position == RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position == RecyclerView.NO_POSITION");
itemView.setBackgroundColor(clickedColor);
}
notifyItemChanged(position);
position = getLayoutPosition();
notifyItemChanged(position); //duplicate
});
只要确保更改
itemView.setOnClickListener(...) to holder.itemView.setOnClickListener(...)
我一直在探索 Recyclerview 和 Cardview,我偶然发现了一个我不知道如何处理的问题。我的 recyclerview 工作正常,但是当我让 recyclerview 的项目背景在点击时改变颜色时,它表现得很奇怪。
好的,recyclerview 正常加载,但是当我第一次单击某个项目时,该项目的背景颜色闪烁其新颜色,但随后保持其默认颜色。当我第二次单击任何项目时,recyclerview 的项目才会更改其背景颜色,因为它应该与单击的项目的其余部分一起更改。
从我添加到代码中的 Log.d
中,我发现第一次点击与第二次(以及其余)点击的不同之处在于 public PremiseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
class PremiseAdapter
。我发现
日志如下。
- 加载 recyclerview 及其 Cardview 项目
.... D/rcvTest: PremiseAdapter.PremiseAdapter
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: ---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder
.... D/rcvTest: [ViewHolder].ViewHolder.TOP
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: FroyYo Mart
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM FroyYo Mart
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: ---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder
.... D/rcvTest: [ViewHolder].ViewHolder.TOP
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: Ice Cream Store
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM Ice Cream Store
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
- 首先点击项目
.... D/rcvTest: [ViewHolder].ViewHolder.listener: null
.... D/rcvTest: [ViewHolder].ViewHolder.getAdapterPosition(): 1
.... D/rcvTest: [ViewHolder].ViewHolder.position: 1
.... D/rcvTest: getLayoutPosition(): 1
.... D/rcvTest: [ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: ---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder
.... D/rcvTest: [ViewHolder].ViewHolder.TOP
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: Ice Cream Store
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM Ice Cream Store
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
- 第二次(以及其余)点击该项目
.... D/rcvTest: [ViewHolder].ViewHolder.listener: null
.... D/rcvTest: [ViewHolder].ViewHolder.getAdapterPosition(): 0
.... D/rcvTest: [ViewHolder].ViewHolder.position: 0
.... D/rcvTest: getLayoutPosition(): 0
.... D/rcvTest: [ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.getItemCount
.... D/rcvTest: PremiseAdapter.onBindViewHolder
.... D/rcvTest: [ViewHolder].bindTo.TOP: FroyYo Mart
.... D/rcvTest: [ViewHolder].bindTo.BOTTOM FroyYo Mart
.... D/rcvTest: PremiseAdapter.onBindViewHolder.get(position)
.... D/rcvTest: PremiseAdapter.getItemCount
这是我的 PremiseAdapter.java
文件,其中包含我的 PremiseAdapter
class 和 ViewHolder
class.
public class PremiseAdapter extends RecyclerView.Adapter<PremiseAdapter.ViewHolder> {
private ArrayList<Premise> mPremiseData;
private Context context;
private int selected_position = -1;
OnItemClickListener onItemClickListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener itemClickListener) {
onItemClickListener = itemClickListener;
}
public PremiseAdapter(Context context, ArrayList<Premise> mPremiseData) {
Log.d("rcvTest", "PremiseAdapter.PremiseAdapter");
this.mPremiseData = mPremiseData;
this.context = context;
}
//Required method for creating the viewholder objects.
@NonNull
@Override
public PremiseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("rcvTest", "---------------------> PremiseAdapter.ViewHolder.onCreateViewHolder");
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_premises, parent, false), onItemClickListener);
}
//Required method that binds the data to the viewholder.
@Override
public void onBindViewHolder(@NonNull PremiseAdapter.ViewHolder holder, int position) {
// Get current premise.
// Populate the textviews with data.
}
@Override
public int getItemCount() {
Log.d("rcvTest", "PremiseAdapter.getItemCount");
return mPremiseData.size();
}
/*----------------------------------- CLASS VIEWHOLDER -------------------------------------------*/
/*----------------------------------- CLASS VIEWHOLDER -------------------------------------------*/
/*class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {*/
class ViewHolder extends RecyclerView.ViewHolder {
// Member Variables for the TextViews
//Constructor for the ViewHolder, used in onCreateViewHolder().
ViewHolder(@NonNull View itemView, OnItemClickListener listener) {
super(itemView);
cardView = itemView.findViewById(R.id.cardview_premise);
Log.d("rcvTest", "[ViewHolder].ViewHolder.TOP");
// Initialize the views.
// Set the OnClickListener to the entire view.
int clickedColor = Color.parseColor("#D5F5E3");
itemView.setOnClickListener((v) -> {
int position = getAdapterPosition();
Log.d("rcvTest", "[ViewHolder].ViewHolder.listener: " + listener);
Log.d("rcvTest", "[ViewHolder].ViewHolder.getAdapterPosition(): " + getAdapterPosition());
Log.d("rcvTest", "[ViewHolder].ViewHolder.position: " + position);
Log.d("rcvTest", "getLayoutPosition(): " + getLayoutPosition());
if (position != RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION");
itemView.setBackgroundColor(clickedColor);
} else if (position == RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position == RecyclerView.NO_POSITION");
itemView.setBackgroundColor(clickedColor);
}
notifyItemChanged(position);
notifyItemChanged(getLayoutPosition());
});
}
void bindTo(Premise currentPremise) {
// Populate the textviews with data.
Log.d("rcvTest", "[ViewHolder].bindTo.TOP: " + currentPremise.getPremiseName());
}
}
}
试试这个。最近我编写了这个并且它工作正常,希望它也能解决你的问题。
int row_index = -1;
holder.itemView.setOnClickListener(v -> {
row_index = position;
notifyDataSetChanged();
});
if (row_index == position) {
holder.layout.setBackgroundColor(Color.parseColor("#ff6600"));
} else {
holder.layout.setBackgroundColor(Color.parseColor("#ffffff"));
}
我通常在 onbindviewholder 中实现 onclick 侦听器,它们似乎工作正常。我建议你试试看。
将这段代码移动到onbindviewholder
int clickedColor = Color.parseColor("#D5F5E3");
/*itemView.setBackgroundColor(defaultColor);*/
holder.itemView.setOnClickListener((v) -> {
int position = getAdapterPosition();
Log.d("rcvTest", "[ViewHolder].ViewHolder.listener: " + listener);
Log.d("rcvTest", "[ViewHolder].ViewHolder.getAdapterPosition(): " + getAdapterPosition());
Log.d("rcvTest", "[ViewHolder].ViewHolder.position: " + position);
Log.d("rcvTest", "getLayoutPosition(): " + getLayoutPosition());
if (position != RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position != RecyclerView.NO_POSITION");
/*listener.onItemClick(position);*/
itemView.setBackgroundColor(clickedColor);
} else if (position == RecyclerView.NO_POSITION) {
Log.d("rcvTest", "[ViewHolder].ViewHolder.position == RecyclerView.NO_POSITION");
itemView.setBackgroundColor(clickedColor);
}
notifyItemChanged(position);
position = getLayoutPosition();
notifyItemChanged(position); //duplicate
});
只要确保更改
itemView.setOnClickListener(...) to holder.itemView.setOnClickListener(...)