RecyclerView条目点击背景变色
2019-07-31 本文已影响1人
Ad大成
这个问题一直困扰好多小伙伴,今天来给大家做一个超级简单的小demo来示范一下!
先口述一下思路,毕竟聪明的小伙伴不看代码也能理解!在适配器里设置接口回调并刷新适配器,再定义一个int的变量 设置set get方法,在界面调用接口
把点击的下标利用int变量的set方法传送回适配器,并刷新适配器.再设置数据的onBindViewHolder方法里进行下标的判断如果相同设置颜色,如果不同设置初始化颜色.这样一来也不必考虑条目复用的问题了 下面代码展示
适配器代码
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 lenovo on 2019/7/31.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
private ArrayList<String> list;
private Context context;
private HashMap<Integer, Boolean> map;
public MyAdapter(ArrayList<String> 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;
}
}
界面代码:
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<String> 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("测试"+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();
}
});
}
}