适配器的封装
程序员文章站
2024-03-17 09:13:58
...
对于适配器的统一封装
介于BaseAdapter的配置在getCount、getItem、getItemId的处理上时一样的,所以可以统一处理
public abstract class ItemAdapter<T> extends BaseAdapter {
protected Context context;
protected List<T> list;
public ItemAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return null == list ? 0 : list.size();
}
@Override
public T getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public abstract View getView(int position, View convertView, ViewGroup parent);
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
notifyDataSetChanged();
}
}
这里布置一个item_classify_layout.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="80dp"
android:src="@drawable/filesystem_grid_icon_app" />
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="应用"
android:textColor="#ff666666"
android:textSize="20sp" />
<TextView
android:id="@+id/item_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="10"
android:textColor="#ff999999" />
</LinearLayout>
使用继承ItemAdapter:
public class ClassifyAdapter extends ItemAdapter<ClassifyItem> {
public ClassifyAdapter(Context context) {
super(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_classify_layout, null);
holder = new ViewHolder();
holder.ivIcon = convertView.findViewById(R.id.item_icon);
holder.tvName = convertView.findViewById(R.id.item_name);
holder.tvNum = convertView.findViewById(R.id.item_num);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//绑定数据
ClassifyItem item = list.get(position);
holder.ivIcon.setImageResource(item.getIcon());
holder.tvName.setText(item.getName());
holder.tvNum.setText(String.valueOf(item.getNum()));
return convertView;
}
class ViewHolder {
ImageView ivIcon;
TextView tvName;
TextView tvNum;
}
}
我们继续看getView这个方法,其实getView中的处理流程也差不多一样,所以可以对其流程进行再度封装,这里我们对应布局的封装,封装为两个过程,一初始化每个 item 的布局,二绑定布局和数据处理显示效果
public abstract class ItemAdapter<T> extends BaseAdapter {
protected List<T> list;
protected Context context;
public ItemAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return list == null ? 0 : list.size();
}
@Override
public T getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//初始化视图
ViewHolder holder;
if (convertView == null) {
//创建每个item的显示布局
holder = createHolder();
convertView = holder.itemView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//绑定数据,显示效果
bindView(getItem(position), holder);
holder.position = position;
return convertView;
}
public class ViewHolder {
View itemView;
int position;
public ViewHolder(View itemView) {
this.itemView = itemView;
}
}
/**
* 产生每个item 的布局(包括各个视图的出初始化)
*
* @return
*/
protected abstract ViewHolder createHolder();
/**
* 绑定数据到Holder上,处理显示效果
*
* @param t
* @param holder
*/
protected abstract void bindView(T t, ViewHolder holder);
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
notifyDataSetChanged();
}
}
可以看到我们定义了两个抽象方法,使用PhotoAdapter继承ItemAdapter会重写这两个方法
public class PhotoAdapter extends ItemAdapter<String> {
public PhotoAdapter(Context context) {
super(context);
}
@Override
protected ViewHolder createHolder() {
//产生每个item 的布局(包括各个视图的出初始化)
View layout = LayoutInflater.from(context).inflate(R.layout.item_photo_layout, null);
return new PhotoHolder(layout);
}
@Override
protected void bindView(String s, ViewHolder holder) {
PhotoHolder h = (PhotoHolder) holder;
//这里是绑定数据到Holder上,处理显示效果,需要自己填充逻辑哦。。。
}
class PhotoHolder extends ViewHolder {
@BindView(R.id.item_photo)
ImageView itemPhoto;
@BindView(R.id.item_checked)
ImageView itemChecked;
public PhotoHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
这里我用了一个butterknife的依赖,有兴趣可以去gitHub官网了解一下https://github.com/search?q=butterknife
上一篇: 在编码之前优先条件选择的重要性
下一篇: yolov3制作数据集遇到的问题
推荐阅读