【记录】记录点滴
【需求】从最简单的RecyclerView使用开始记录
RecyclerView和ListView的使用非常相似:1. 导入包;2. 创建Adapter;3. 设置RecyclerView
1. 导入support-v7包,例如
implementation 'com.android.support:recyclerview-v7:27.1.1'
2. 继承RecyclerView.Adater,实现自定义的Adapter
public class SimpleDemoAdapter extends RecyclerView.Adapter {
List data;
public SimpleDemoAdapter (List data) {
this.data = data;
}
/**
* 渲染视图,返回ViewHolder
*/
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//使用系统布局,里面只有一个TextView
View v = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
TypeOne vh = new TypeOne(v);
return vh;
}
/**
* 绑定数据,设置Item内容的地方
*/
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((TypeOne)holder).tvContent.setText("position = " + position);
}
/**
* 返回数据长度
*/
@Override
public int getItemCount() {
return data == null ? 0 : data.size();
}
/**
* 自定义ViewHolder,后续会记录多类型Item的实现,所以命名就叫TypeOne
*/
class TypeOne extends RecyclerView.ViewHolder{
public TextView tvContent;
public TypeOne(View itemView) {
super(itemView);
tvContent = itemView.findViewById(android.R.id.text1);
}
}
}
3. 设置RecyclerView
在Activity或者Fragment等中
adapter = new SimpleDemoAdapter(data);
//以LinearLayoutManager示例,默认垂直方向
//也可以使用new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);来指定方向
LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
rv.setAdapter(adapter);
PS:多种类型 Item 时,需要重写 getItemViewType()
1)先设计一个类,其中的 type 对应 Item 的类型
public class MultiTypeRvBean {
public int type = 0;
public String content;
public MultiTypeRvBean(int type, String content) {
this.type = type;
this.content = content;
}
}
2)重写Adapter中的 getItemViewType
//这里简单些,直接返回MultiType中的type,并依据type判断
@Override
public int getItemViewType(int position) {
MultiTypeRvBean bean = data.get(position);
return bean.type;
}
3)修改onCreateViewHolder与onBindViewHolder
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = null;
RecyclerView.ViewHolder vh;
switch (viewType){
case 0:
v = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
vh = new TypeOne(v);
return vh;
case 1:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tv_center, parent, false);
vh = new TypeTwo(v);
return vh;
case 2:
default:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tv_right, parent, false);
vh = new TypeThree(v);
return vh;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case 0:
((TypeOne)holder).tvContent.setText("样式一");
break;
case 1:
((TypeTwo)holder).tvCenter.setText("样式二");
break;
case 2:
default:
((TypeThree)holder).tvRight.setText("样式三");
}
}
PS:嵌套使用RecyclerView,和普通使用一样
/**
* 重点在这里,0:渲染嵌套的Grid,只含有一个RecyclerView,1:渲染普通的Item,只含有一个TextView
*/
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = null;
RecyclerView.ViewHolder vh;
switch (viewType) {
case 0:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nest_recycler, parent, false);
vh = new TypeNestRecycler(v);
return vh;
case 1:
default:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tv_normal, parent, false);
vh = new TypeNormal(v);
return vh;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case 0:
GridLayoutManager glm = new GridLayoutManager(context, 3, GridLayoutManager.HORIZONTAL, false);
SimpleGridAdapter adapter = new SimpleGridAdapter(gridData);
((TypeNestRecycler) holder).nestGridRv.setLayoutManager(glm);
((TypeNestRecycler) holder).nestGridRv.setAdapter(adapter);
break;
case 1:
((TypeNormal) holder).tvContent.setText("样式一");
break;
}
}
/**
* 只返回两种类型,0 嵌套的RecyclerView,1 简单的TextView
*/
@Override
public int getItemViewType(int position) {
MultiTypeRvBean bean = data.get(position);
return bean.type;
}
嵌套的RecyclerView的布局也非常简单item_nest_recycler.xml
顺便记录下方法,返回当前 item 的跨度大小,就是占用了几行(几列),套用别人博客的图,可以实现圈出的 item 效果,这个 item 占用了2列(https://www.jianshu.com/p/675883c26ef2)
//合并Grid中列(行)
setSpanSizeLookup