在Android开发中,我们总会时不时遇到列表多样式布局,比如商城的不同item 布局样式不一样,或者像打开相册,点击列表最后的添加按钮图片展示到列表上。
现在来实现adapter多布局样式,写个初级简单的效果,理解怎么写后,后面各种复杂的多布局也能在理解后,自行编写出来
先上效果图:
demo展示.gif
本demo 是基于AndroidX开发,activity的布局就是一个简单线性布局包含RecyclerView,就不贴上图了,直接来看adapter 如何编写,这里我直接上整个adapter的代码,再分开讲解每个的作用
public class SelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
//布局标识集合
private static final int TYPE_ONE = 0;//普通头像
private static final int TYPE_TWO = 1;//添加按钮
private List<SelectPerson> personList;
private OnItemAdapterClickListener onItemAdapterClickListener;
public SelectAdapter(Context context, OnItemAdapterClickListener onItemAdapterClickListener) {
this.context = context;
this.onItemAdapterClickListener = onItemAdapterClickListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_ONE) {
View mView1 = LayoutInflater.from(context).inflate(R.layout.recycle_item_head, null);
ViewHolderHead viewHolderHead = new ViewHolderHead(mView1);
return viewHolderHead;
} else {
View mView2 = LayoutInflater.from(context).inflate(R.layout.recycle_item_add, null);
ViewHolderAdd viewHolderHead = new ViewHolderAdd(mView2);
return viewHolderHead;
}
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderHead) {
((ViewHolderHead) holder).tv_name.setText(personList.get(position).getName());
//头像删除按钮点击
((ViewHolderHead) holder).img_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemAdapterClickListener.onItemClickEvent(view, position);
}
});
} else if (holder instanceof ViewHolderAdd) {
//添加按钮点击
((ViewHolderAdd) holder).ly_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemAdapterClickListener.onItemClickEvent(v, position);
}
});
}
}
@Override
public int getItemCount() {
return personList == null ? 0 : personList.size();
}
@Override
public int getItemViewType(int position) {
if (TYPE_TWO == personList.get(position).getType()) return TYPE_TWO;
else return TYPE_ONE;//其它返回默认情况
}
class ViewHolderHead extends RecyclerView.ViewHolder {
private ImageView img_delete;
private TextView tv_name;
public ViewHolderHead(@NonNull View itemView) {
super(itemView);
img_delete = itemView.findViewById(R.id.img_delete);
tv_name = itemView.findViewById(R.id.tv_name);
}
}
class ViewHolderAdd extends RecyclerView.ViewHolder {
private RelativeLayout ly_add;
private ViewHolderAdd(@NonNull View itemView) {
super(itemView);
ly_add = itemView.findViewById(R.id.ly_add);
}
}
public void reFreshData(List<SelectPerson> personList) {
this.personList = personList;
notifyDataSetChanged();
}
public interface OnItemAdapterClickListener {
void onItemClickEvent(View view, int selectPosition);
}
}
SelectAdapter 构造函数传入 上下文和接口变量,其中onItemAdapterClickListener 用于实现外部对内部的头像删除和添加的的接口回调监听,用于处理内部控件点击事件
onCreateViewHolder用于初始化和创建一个holder供RecyclerView使用,holder 我们这边定义了两种,分别是ViewHolderHead 用于展示头像,姓名 和 ViewHolderAdd 添加按钮 ,两者的xml 视图 贴一下,布局代码就不贴了,相信各位小伙伴一眼就瞅出我怎么写,甚至写的比我更好
截图如下:
recycle_item_head.png recycle_item_add.png
getItemViewType 则是返回新视图的的视图类型,用来区分ViewType
在onBindViewHolder 方法中将给定视图绑定到相关位置,我在里面实现了头像的删除和添加按钮的点击相关逻辑,通过接口在外部解决
reFreshData 则是我用于数据新增或者移除后,通知RecyclerView 已经被更改
SelectPerson 是自己定义的bean类
public class SelectPerson {
private int type;//0-默认头像 1-添加按钮
private String name;
public SelectPerson(int type,String name) {
this.type = type;
this.name=name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
现在回到MainActivity,我们在绑定完RecyclerView后,初始化SelectAdapter,并且自己添加一些数据,设置布局管理器为网格布局
代码如下:
private void initAdapter() {
final List<SelectPerson> list = new ArrayList<>();
ry_item = findViewById(R.id.ry_item);
for (int i = 0; i < 7; i++) {
list.add(new SelectPerson(0, "name" + i));
}
list.add(new SelectPerson(1, "添加按钮"));
selectAdapter = new SelectAdapter(this, new SelectAdapter.OnItemAdapterClickListener() {
@Override
public void onItemClickEvent(View view, int selectPosition) {
//此处为内部点击按钮
switch (view.getId()) {
case R.id.img_delete:
Toast.makeText(MainActivity.this, "删除点击", Toast.LENGTH_SHORT).show();
list.remove(selectPosition);
selectAdapter.reFreshData(list);
break;
case R.id.ly_add:
list.add(list.size() - 1, new SelectPerson(0, "新增人物"));
selectAdapter.reFreshData(list);
Toast.makeText(MainActivity.this, "添加", Toast.LENGTH_SHORT).show();
break;
}
}
});
ry_item.setLayoutManager(new GridLayoutManager(this, 5));
ry_item.setAdapter(selectAdapter);
selectAdapter.reFreshData(list);
}
初级的多样式布局就已经完成了 ,希望能对阅读这篇文章的各位帮上一点小忙,最初我刚为一名新手时,也是通过各个网站博客的开发者们提供的技术文章,解决了在我当时看来难度很大的问题,今后会慢慢继续写一些文章,当作对自己的复习,同时又能帮上当初和我一样刚入行的开发者
网友评论