最开始使用listview的时候有大神封装了adapter,现在使用recyclerview,依葫芦画瓢对adapter进行了封装,使用方法如下
如果是一种类型的item
public class StudentAdapter extends RecycleCommenAdapter<Student> {
/**
* @param context
* @param list
*/
public StudentAdapter(Context context, List<Student> list) {
super(context, list, R.layout.item_student);
}
@Override
public void convert(RecycleViewHolder holder, Student student) {
holder.setText(R.id.tv_student_age,String.valueOf(student.getAge())).setText(R.id.tv_student_name, student.getName());
}
}
如上所示,在构造方法里中super方法中添加布局
在covert方法中适配数据就OK啦
如果是多种item的adapter
首先,重写getItemViewType方法
@Override
public int getItemViewType(int position) {
return students.get(position).getAge() %2==0? ITEM_VIEW_TYPE_HEADER
: ITEM_VIEW_TYPE_ITEM;
}
我这里测试根据学生年龄的不同返回两个不同的常量
然后根据VIewType的不同创建不同的viewholder
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View view = null;
if (arg1 == ITEM_VIEW_TYPE_HEADER) {
view = LayoutInflater.from(context).inflate(
R.layout.item_student, arg0, false);
} else if (arg1 == ITEM_VIEW_TYPE_ITEM) {
view = LayoutInflater.from(context).inflate(
R.layout.item_student_mult, arg0, false);
}
return RecycleViewHolder.get(view);
}
最后就是适配数据啦
@Override
public void onBindViewHolder(RecycleViewHolder arg0, int arg1) {
int itemViewType = getItemViewType(arg1);
if (itemViewType == ITEM_VIEW_TYPE_HEADER) {
arg0.setText(R.id.tv_student_name,students.get(arg1).getName());
} else if (itemViewType == ITEM_VIEW_TYPE_ITEM) {
}
arg0.setText(R.id.tv_student_age,String.valueOf(students.get(arg1).getAge()));
}
最后贴上完整的代码
package com.example.master.customadapter.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.master.customadapter.R;
import com.example.master.customadapter.model.Student;
import java.util.ArrayList;
public class Multi_Item_Adapter extends RecyclerView.Adapter<RecycleViewHolder> {
private ArrayList<Student> students;
private Context context;
private static final int ITEM_VIEW_TYPE_HEADER = 0;
private static final int ITEM_VIEW_TYPE_ITEM = 1;
public Multi_Item_Adapter(ArrayList<Student> sutdents, Context context) {
super();
this.students = sutdents;
this.context = context;
}
@Override
public int getItemCount() {
return students.size();
}
@Override
public void onBindViewHolder(RecycleViewHolder arg0, int arg1) {
int itemViewType = getItemViewType(arg1);
if (itemViewType == ITEM_VIEW_TYPE_HEADER) {
arg0.setText(R.id.tv_student_name,students.get(arg1).getName());
} else if (itemViewType == ITEM_VIEW_TYPE_ITEM) {
}
arg0.setText(R.id.tv_student_age,String.valueOf(students.get(arg1).getAge()));
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View view = null;
if (arg1 == ITEM_VIEW_TYPE_HEADER) {
view = LayoutInflater.from(context).inflate(
R.layout.item_student, arg0, false);
} else if (arg1 == ITEM_VIEW_TYPE_ITEM) {
view = LayoutInflater.from(context).inflate(
R.layout.item_student_mult, arg0, false);
}
return RecycleViewHolder.get(view);
}
@Override
public int getItemViewType(int position) {
return students.get(position).getAge() %2==0? ITEM_VIEW_TYPE_HEADER
: ITEM_VIEW_TYPE_ITEM;
}
}
last but not the least 哈哈哈,当然是源码地址了
github 地址 https://github.com/looooker/CustomAdapter
还有一位大神对adapter进行了更进一步的封装,代码逻辑都是一样的,估计都是参考了同一位大神的代码,
同样也贴上地址 https://github.com/mikepenz/FastAdapter