单击 recyclerView 中的项目以更改其颜色时的错误行为?
Wrong behavior when click on item in recyclerView to change its color?
我正在尝试从适配器更改 RecyclerView 中单击项目的背景颜色并且它有效,但问题是当我单击位置 1 时它更改了位置 1 和 7 的颜色,当我单击位置 2 它会更改位置 2 和 8 的颜色,依此类推...
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.viewHolder> {
private ArrayList<String> name = new ArrayList<>();
private Context context;
boolean added = false;
public RecyclerViewAdapter(ArrayList<String> name, Context context) {
this.name = name;
this.context = context;
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_listview, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(final viewHolder holder, final int position) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final Dialog dialog = new Dialog(view.getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.add_item_dialog_small);
Window window = dialog.getWindow();
window.setLayout(500, 450);
Button addToList = (Button) dialog.findViewById(R.id.addToList);
addToList.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v) {
holder.cardView.setBackgroundColor(R.color.layer4);
dialog.dismiss();
}
});
dialog.show();
}
});
}
编辑:
这是卡片视图:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="320dp"
android:layout_margin="10dp"
android:background="@color/layer2"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="@color/layer3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/textViewItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Item"
android:textColor="@color/add_button"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item_number"
android:textColor="@color/text_view_color"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewItemNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textColor="@color/second_color"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
您可以使用 lambda 简单地编写您的代码,这是肯定的。
这个:
addToList.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v) {
holder.cardView.setBackgroundColor(R.color.layer4);
}
dialog.dismiss();
}
});
您可以更改为:
addToList.setOnClickListener((View v) -> {
holder.cardView.setBackgroundColor(R.color.layer4);
}
dialog.dismiss();
}
});
将 var positionClicked 存储在您的适配器中,这样它将是
onClick{positionClicked = position)
然后在你的 onBindViewHolder 中放置
if(positionClicked==position){
//change color of element here//
}
如果要为 recyclerview 的每一行实现选项(例如收藏夹图标、复选框、突出显示或...),我认为最好的方法是使用任意参数创建一个 object。例如,对于最喜欢的 boolean
参数是最佳选择。
在您的情况下,您应该创建一个 object,其中包含一个字符串和一个布尔参数及其设置器和吸气器,如下所示:
public class mObject {
private String name;
private boolean clicked;
// setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isClicked() {
return clicked;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
}
然后在这个列表中设置你的数据object然后将它传递给适配器。
在onBindViewHolder
中,首先检查点击值,如果是则改变颜色。然后在 onClick
方法中同时更改布尔值和背景颜色,最后使用 notifyDataSetChanged();
更新视图。
您的适配器 onBindViewHolder
应如下所示:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder view,final int position) {
final MVH holder = (MVH) view;
holder.tv.setText(name.get(position).getName());
if (name.get(position).isClicked()){
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
} else {
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.add_item_dialog_small);
Window window = dialog.getWindow();
window.setLayout(500, 450);
Button addToList = (Button) dialog.findViewById(R.id.addToList);
addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (name.get(position).isClicked()){
name.get(position).setClicked(false);
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
} else {
name.get(position).setClicked(true);
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
dialog.dismiss();
notifyDataSetChanged();
}
});
dialog.show();
}
});
}
我也遇到这个问题。您可以做的是在单击 onBindViewHolder 方法之前设置背景颜色。
@Override
public void onBindViewHolder(final viewHolder holder, final int position) {
holder.cardView.setBackgroundColor(R.color.defaultBackgroundColour);
我正在尝试从适配器更改 RecyclerView 中单击项目的背景颜色并且它有效,但问题是当我单击位置 1 时它更改了位置 1 和 7 的颜色,当我单击位置 2 它会更改位置 2 和 8 的颜色,依此类推...
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.viewHolder> {
private ArrayList<String> name = new ArrayList<>();
private Context context;
boolean added = false;
public RecyclerViewAdapter(ArrayList<String> name, Context context) {
this.name = name;
this.context = context;
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_listview, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(final viewHolder holder, final int position) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final Dialog dialog = new Dialog(view.getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.add_item_dialog_small);
Window window = dialog.getWindow();
window.setLayout(500, 450);
Button addToList = (Button) dialog.findViewById(R.id.addToList);
addToList.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v) {
holder.cardView.setBackgroundColor(R.color.layer4);
dialog.dismiss();
}
});
dialog.show();
}
});
}
编辑: 这是卡片视图:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="320dp"
android:layout_margin="10dp"
android:background="@color/layer2"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="@color/layer3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/textViewItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Item"
android:textColor="@color/add_button"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item_number"
android:textColor="@color/text_view_color"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewItemNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textColor="@color/second_color"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
您可以使用 lambda 简单地编写您的代码,这是肯定的。
这个:
addToList.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v) {
holder.cardView.setBackgroundColor(R.color.layer4);
}
dialog.dismiss();
}
});
您可以更改为:
addToList.setOnClickListener((View v) -> {
holder.cardView.setBackgroundColor(R.color.layer4);
}
dialog.dismiss();
}
});
将 var positionClicked 存储在您的适配器中,这样它将是
onClick{positionClicked = position)
然后在你的 onBindViewHolder 中放置
if(positionClicked==position){
//change color of element here//
}
如果要为 recyclerview 的每一行实现选项(例如收藏夹图标、复选框、突出显示或...),我认为最好的方法是使用任意参数创建一个 object。例如,对于最喜欢的 boolean
参数是最佳选择。
在您的情况下,您应该创建一个 object,其中包含一个字符串和一个布尔参数及其设置器和吸气器,如下所示:
public class mObject {
private String name;
private boolean clicked;
// setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isClicked() {
return clicked;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
}
然后在这个列表中设置你的数据object然后将它传递给适配器。
在onBindViewHolder
中,首先检查点击值,如果是则改变颜色。然后在 onClick
方法中同时更改布尔值和背景颜色,最后使用 notifyDataSetChanged();
更新视图。
您的适配器 onBindViewHolder
应如下所示:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder view,final int position) {
final MVH holder = (MVH) view;
holder.tv.setText(name.get(position).getName());
if (name.get(position).isClicked()){
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
} else {
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.add_item_dialog_small);
Window window = dialog.getWindow();
window.setLayout(500, 450);
Button addToList = (Button) dialog.findViewById(R.id.addToList);
addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (name.get(position).isClicked()){
name.get(position).setClicked(false);
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
} else {
name.get(position).setClicked(true);
holder.tv.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}
dialog.dismiss();
notifyDataSetChanged();
}
});
dialog.show();
}
});
}
我也遇到这个问题。您可以做的是在单击 onBindViewHolder 方法之前设置背景颜色。
@Override
public void onBindViewHolder(final viewHolder holder, final int position) {
holder.cardView.setBackgroundColor(R.color.defaultBackgroundColour);