简体   繁体   English

如何设置“回收者视图”中所选项目的颜色?

[英]how to set color the selected Item of Recycler View?

I have a RecyclerView with the TextView loaded from array MyData . 我有一个RecyclerView和从数组MyData加载的TextView Actually what I need is when I click any item in RecyclerView that Item must get color and when I click another item I want that the first one become normal. 实际上,我需要的是,当我单击RecyclerView中的任何项目时,该项目必须变色;当我单击另一个项目时,我希望第一个项目变得正常。

    private static class MyOnClickListener implements View.OnClickListener {

    private final Context context;
    String PersonGenerated;

    private MyOnClickListener(Context context) {
        this.context = context;
    }

    @Override
    public void onClick(View v) {
        // removeItem(v);
        int selectedItemPosition = recyclerView.getChildPosition(v);
        RecyclerView.ViewHolder viewHolder
                = recyclerView.findViewHolderForPosition(selectedItemPosition);
        TextView textViewName
                = (TextView) viewHolder.itemView.findViewById(R.id.textview_name);
        String selectedName = (String) textViewName.getText();

  data = new ArrayList<DataModel>();
    for (int i = 0; i < MyData.nameArray.length; i++) {
        data.add(new DataModel(
                MyData.id_[i],
                MyData.nameArray[i]

        ));
    }
    }
}

This is my Adapter Class: 这是我的适配器类:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

private ArrayList<DataModel> dataSet;



public CustomAdapter(ArrayList<DataModel> data) {
    this.dataSet = data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.adapter_persons, parent, false);

    view.setOnClickListener(Persons.myOnClickListener);

    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

    holder.textViewName.setText(dataSet.get(listPosition).getName());

}


@Override
public int getItemCount() {
    return dataSet.size();


}

public void animate(RecyclerView.ViewHolder viewHolder) {
    final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(context, R.anim.anticipateovershoot_interpolator);
    viewHolder.itemView.setAnimation(animAnticipateOvershoot);
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textViewName;

    public MyViewHolder(View itemView) {
        super(itemView);
        this.textViewName = (TextView) itemView.findViewById(R.id.textview_name);
    }
}
}

This is my Array Class: 这是我的数组类:

public class MyData {

    static String[] nameArray = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"};
    static Integer[] id_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
}

Adapter Layout 适配器布局

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/select_row"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">


    <TextView
        android:layout_weight="1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        style="@style/custom_text_oval"
        android:text="1"
        android:textColor="#8F8F8F"
        android:id="@+id/textview_name" />

Recyclerview layout Recyclerview布局

  <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout3"
            android:layout_gravity="center_horizontal">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


        </LinearLayout>

User these selector file :: 使用这些选择器文件::

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true">
       <shape>
             <solid android:color="@color/blue" />
       </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
           <solid android:color="@android:color/transparent" />
        </shape>
    </item>

<item android:state_selected="true">
    <shape>
       <solid android:color="@color/blue" />
    </shape>
</item>
    </selector>

This is an old question but it can help to somebody, I've resolved it in this way: You need to save the selected item and the last selected to update the background. 这是一个古老的问题,但是它可以为某人提供帮助,我已经通过以下方式解决了这个问题:您需要保存所选的项目,最后保存的项目才能更新背景。

private int selectedItem = 0;
private int lastSelected = 0;
public void onBindViewHolder(ViewHolder h, int position) {
    //If is selected the color change
    int backgroundColor = (position == selectedItem) ? R.color.selected : R.color.no_selected;

    layout.setBackgroundColor(ContextCompat.getColor(context, backgroundColor));

    h.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Save the position of the last selected item
            lastSelected = selectedItem;
            //Save the position of the current selected item
            selectedItem = position;

            //This update the last item selected
            notifyItemChanged(lastSelected);

            //This update the item selected
            notifyItemChanged(selectedItem);
        }
    });
}

create a selector drawable as below 创建一个如下所示的选择器可绘制

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true">
       <shape>
             <solid android:color="@color/blue" />
       </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
           <solid android:color="@android:color/transparent" />
        </shape>
    </item>

<item android:state_selected="true">
    <shape>
       <solid android:color="@color/blue" />
    </shape>
</item>
    </selector>

set this drawable as background of your RecyclerView row layout as 将此可绘制对象设置为RecyclerView行布局的背景为

android:background="@drawable/selector"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM