首页 > 解决方案 > 单击 recyclerView 中的项目以更改其颜色时的错误行为?

问题描述

我正在尝试从适配器更改 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();

            }
        });

}

编辑:这是 cardView :

    <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>

标签: androidandroid-recyclerviewandroid-cardviewrecyclerview-layout

解决方案


如果对 recyclerview 的每一行实施选项(例如最喜欢的图标、复选框、突出显示或...),我认为最好的方法是使用您的任意参数创建一个对象。例如对于喜欢的boolean参数是最佳选择。

在您的情况下,您应该使用它们的 setter 和 getter 创建一个带有字符串和布尔参数的对象,如下所示:

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;
        }
}

然后将您的数据设置在此对象的列表中,然后将其传递给适配器。在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();

                }
            });
        }

推荐阅读