RecyclerView之点击item变色

在适配器里设置接口回调并刷新适配器,再定义一个int的变量 设置set get方法,在界面调用接口,把点击的下标利用int变量的set方法传送回适配器,并刷新适配器.再设置数据的onBindViewHolder方法里进行下标的判断如果相同设置颜色,如果不同设置初始化颜色.可以不考虑条目复用问题

RecyclerView适配器代码示例:

package com.example.lenovo.itembackupdata;

import android.content.Context;


import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by abc on 2019/08/05.
 */

public class MyAdapter extends RecyclerView.Adapter {

    private ArrayList list;
    private Context context;
    private HashMap map;


    public MyAdapter(ArrayList list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
        return new MyHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        holder.tv.setText(list.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                一定要刷新适配器 当条目发生改变这是必须的
                getListener.onClick(position);
                notifyDataSetChanged();

            }
        });
//        如果下标和传回来的下标相等 那么确定是点击的条目 把背景设置一下颜色
        if (position == getmPosition()) {
            holder.itemView.setBackgroundColor(Color.BLUE);
        }else{
//            否则的话就全白色初始化背景
            holder.itemView.setBackgroundColor(Color.WHITE);
        }
    }

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

    class MyHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public MyHolder(View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv_item);

        }
    }


    public interface GetListener {

        void onClick(int position);
    }

    private GetListener getListener;

    public void setGetListener(GetListener getListener) {
        this.getListener = getListener;
    }
    private  int mPosition;

    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }
}

ACtivty代码示例:

package com.example.lenovo.itembackupdata;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  {

    private com.jcodecraeer.xrecyclerview.XRecyclerView recyc;

    private MyAdapter myAdapter;
    private ArrayList list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.recyc = (XRecyclerView) findViewById(R.id.recyc);

        init();

    }

    private void init() {
        recyc.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
         list.add("RecyclerView测试"+i);
        }
        myAdapter = new MyAdapter(list, this);
        recyc.setAdapter(myAdapter);
        recyc.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        myAdapter.setGetListener(new MyAdapter.GetListener() {

            @Override
            public void onClick(int position) {
//                把点击的下标回传给适配器 确定下标
                myAdapter.setmPosition(position);
                myAdapter.notifyDataSetChanged();
            }
        });


    }


}

比较简单的代码示例

你可能感兴趣的:(Android)