如何为Recycler View的所选项目设置颜色?

时间:2016-10-01 10:24:25

标签: android android-recyclerview adapter android-arrayadapter

我有一个RecyclerViewTextView从数组MyData加载。实际上我需要的是当我点击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]

        ));
    }
    }
}

这是我的适配器类:

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

这是我的数组类:

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

适配器布局

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

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

3 个答案:

答案 0 :(得分:1)

使用这些选择器文件::

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

答案 1 :(得分:1)

这是一个老问题,但它可以帮助某人,我已经用这种方式解决了它: 您需要保存所选项目和最后选择的项目以更新背景。

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

答案 2 :(得分:-3)

创建一个可绘制的选择器,如下所示

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

将此drawable设置为RecyclerView行布局的背景

android:background="@drawable/selector"