在安卓应用开发时,适配器的使用非常频繁,而适配器的代码又有许多是重复的,因此对BaseAdapter进行一个封装,去除其中重复又繁琐的部分,其它的适配器也同样如此
BaseAdapter封装
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public abstract class MyBaseAdapter <T, H> extends BaseAdapter {
protected final List<T> dataList;
public MyBaseAdapter(List<T> dataList) {
this.dataList = dataList;
}
protected abstract Class<H> getHolderClass();
protected abstract CallBack<T, H> getCallBack();
protected abstract int getLayout();
@Override
public int getCount() {
return dataList.size();
}
@Override
public T getItem(int i) {
return dataList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Context context = viewGroup.getContext();
H holder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(getLayout(), null, false);
try {
holder = getHolderClass().getDeclaredConstructor(View.class).newInstance(view);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
view.setTag(holder);
} else
holder = (H) view.getTag();
getCallBack().operate(getItem(i), i, holder, context);
return view;
}
public void addData(List<T> list) {
dataList.addAll(list);
}
public void addData(T t) {
dataList.add(t);
}
public List<T> getDataList() {
return dataList;
}
public T getData(int pos) {
return dataList.get(pos);
}
public void clearData() {
dataList.clear();
}
public interface CallBack <T, H> {
void operate(T item, int pos, H holder, Context context);
}
}
MyBaseAdapter的使用
package com.example.myapplication;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import java.util.List;
public class TestAdapter extends MyBaseAdapter<TestBean, TestAdapter.ViewHolder> {
public TestAdapter(List<TestBean> dataList) {
super(dataList);
}
@Override
Class<ViewHolder> getHolderClass() {
return ViewHolder.class;
}
@Override
CallBack<TestBean, ViewHolder> getCallBack() {
return new CallBack<TestBean, ViewHolder>() {
@Override
public void operate(TestBean item, int pos, ViewHolder holder, Context context) {
holder.tvTest.setText(item.getId() + "");
}
};
}
@Override
protected int getLayout() {
return R.layout.adapter_test;
}
public static
class ViewHolder {
public View rootView;
public TextView tvTest;
public ViewHolder(View rootView) {
this.rootView = rootView;
this.tvTest = (TextView) rootView.findViewById(R.id.tvTest);
}
}
}
class TestBean {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
不使用封装的BaseAdapter
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class T2Adapter extends BaseAdapter {
public List<TestBean> dataList;
public T2Adapter(List<TestBean> dataList) {
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public TestBean getItem(int i) {
return dataList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Context context = viewGroup.getContext();
ViewHolder holder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.adapter_test, null, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
TestBean item = getItem(i);
holder.tvTest.setText(item.getId() + "");
return view;
}
public static
class ViewHolder {
public View rootView;
public TextView tvTest;
public ViewHolder(View rootView) {
this.rootView = rootView;
this.tvTest = (TextView) rootView.findViewById(R.id.tvTest);
}
}
}
|