RecyclerView适配器的封装及实现

文章目录
  1. 一.封装万能适配器BaseAdapter
  2. 二.实现Adapter

一.封装万能适配器BaseAdapter

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 支持传任意布局和任意实体类
* 支持增删改查
* 支持长按、点击事件
* //public abstract class A<T,VH extends A.ViewHolderJava<T>> extends RecyclerView.Adapter<VH>
*/
public abstract class BaseRecyclerAdapter<T,VH extends BaseRecyclerAdapter.BaseHolder>
extends RecyclerView.Adapter<VH>{

private List<T> datas = null;
private Context context;
protected int itemlayoutid; //行布局id


public BaseRecyclerAdapter(List<T> datas,Context context){
this.datas = datas;
this.context = context;
}
public BaseRecyclerAdapter(List<T> datas){
this.datas = datas;
}

@NonNull
@Override
public abstract VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i);

@Override
public void onBindViewHolder(@NonNull VH baseHolder, int i) {

bindView_Holder(baseHolder,i);
}

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

public abstract int setItemLayout();

public abstract void bindView_Holder(BaseRecyclerAdapter.BaseHolder baseHolder,int position);


/**
* 内部类holder
*/
public abstract class BaseHolder extends RecyclerView.ViewHolder{

public BaseHolder(@NonNull View itemView) {
super(itemView);
bindItem(itemView);
}
public abstract void bindItem(View view);
}

}

二.实现Adapter

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
public class SubAdapter extends BaseRecyclerAdapter{

ArrayList<Bean.T1348647853363Bean> datas;
Context context;
OnClickRecycler clickRecycler;

public SubAdapter(ArrayList<Bean.T1348647853363Bean> datas, Context context,OnClickRecycler onClickRecycler){
super(datas,context);
this.datas = datas;
this.context = context;
this.clickRecycler = onClickRecycler;
}

public SubAdapter(ArrayList<Bean.T1348647853363Bean> datas){
super(datas);
this.datas = datas;

}

public void setContext(Context context){
this.context = context;
}

public void setOnClickRecycler(OnClickRecycler onClickRecycler){
this.clickRecycler = onClickRecycler;
}

@NonNull
@Override
public BaseHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(setItemLayout(),viewGroup,false);
SubAdapter.SubHolder subHolder = new SubAdapter.SubHolder(view);

return subHolder;
}


@Override
public int setItemLayout() {
return R.layout.itemlayout;
}

@Override
public void bindView_Holder(BaseHolder baseHolder, final int position) {
SubAdapter.SubHolder subHolder = (SubAdapter.SubHolder)baseHolder;
Glide.with(context).load(datas.get(position).getImgsrc()).into(subHolder.imageView);

//修改行布局高度
ViewGroup.LayoutParams params = subHolder.textView.getLayoutParams();
int endHeight =(int) (200+Math.random()*(400-200+1));
params.height = endHeight;
subHolder.textView.setLayoutParams(params);

subHolder.textView.setText(datas.get(position).getTitle());
subHolder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
clickRecycler.longClickRecycler(position);
return true;
}
});
subHolder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickRecycler.clickRecycler(position);
}
});
}


public void addData(Bean.T1348647853363Bean data){
if(data!= null){
datas.add(data);
notifyItemInserted(datas.size()-1);
}
}

public void addDatas(ArrayList<Bean.T1348647853363Bean> allData){
if(datas != null && datas.size()> 0){
int startIndex = datas.size();
datas.addAll(allData);
int endIndex = datas.size();
notifyItemRangeInserted(startIndex,endIndex);
}
}

public void removeData(int postion){
if(postion<= datas.size()){
datas.remove(postion);
notifyItemChanged(postion);

}
}

public void removeAll(){
datas.clear();
notifyDataSetChanged();
}


class SubHolder extends BaseHolder{

private TextView textView;
private ImageView imageView;

public SubHolder(@NonNull View itemView) {
super(itemView);
}

@Override
public void bindItem(View view) {
textView = view.findViewById(R.id.txt_item);
imageView = view.findViewById(R.id.img_item);
}
}
}