出现需求
多布局的话使用RecyclerView更为合理,但是目前项目使用的是eclipse,普遍使用的是ListView,若为一个界面而引入RecyclerView不划算不合理。所以将Adapter简单修改后让ListView具备多布局能力。
分装Adapter
public abstract class AbsListAdapter<T> extends BaseAdapter {
protected Context mContextInAbsAdapter;
List<T> mDataList;
public AbsListAdapter(Context mContext) {
super();
this.mContextInAbsAdapter = mContext;
this.mDataList = new ArrayList<T>();
}
public void setData(@NonNull List<T> tempDatas) {
this.mDataList.clear();
this.addDatas(tempDatas);
}
public void addDatas(@NonNull List<T> tempDatas) {
this.mDataList.addAll(tempDatas);
this.notifyDataSetChanged();
}
public void addData(@NonNull T data) {
this.mDataList.add(data);
this.notifyDataSetChanged();
}
public void addData(int index, @NonNull T data) {
this.mDataList.add(index, data);
this.notifyDataSetChanged();
}
public void removeData(int index){
if (index != ParamUtil.checkInt(index, 0, this.mDataList.size())) {
return ;
}
this.mDataList.remove(index);
this.notifyDataSetChanged();
}
public void removeData(T t){
if (t == null) {
return;
}
int index = this.mDataList.indexOf(t);
removeData(index);
}
public void clearData() {
this.mDataList.clear();
this.notifyDataSetChanged();
}
public List<T> getDatas() {
return new ArrayList<T>(this.mDataList);
}
@Nullable
public T getData(int index) {
if (index != ParamUtil.checkInt(index, 0, this.mDataList.size())) {
return null;
}
return this.mDataList.get(index);
}
@SuppressLint("ViewTag") @Override
public View getView(int position, View convertView, ViewGroup parent) {
T data = null;
// getCount() 可能被覆写
if (position < this.mDataList.size()) {
data = this.mDataList.get(position);
}
int itemLayoutResId = getItemLayoutResId(position, data);
if (convertView != null && (Integer) convertView.getTag() != itemLayoutResId) {
convertView.setTag(((Integer) convertView.getTag()), null);
convertView = null;
}
ViewHolder holder = null;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag(itemLayoutResId);
}
if (holder == null) {
convertView = LayoutInflater.from(mContextInAbsAdapter).inflate(itemLayoutResId, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(itemLayoutResId);
convertView.setTag(itemLayoutResId, holder);
}
onBindData(position, data, holder);
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public Object getItem(int position) {
return null;
}
public abstract int getItemLayoutResId(int position, T data);
public abstract void onBindData(int position, T data, ViewHolder holder);
public static class ViewHolder {
private SparseArray<View> viewCache;
private View itemView;
public ViewHolder(View containerView) {
this.itemView = containerView;
this.viewCache = new SparseArray<View>();
}
@SuppressWarnings("unchecked")
public <T extends View> T getView(int resId) {
View view = this.viewCache.get(resId);
if (view == null) {
view = this.itemView.findViewById(resId);
this.viewCache.put(resId, view);
}
return (T) view;
}
public View getConvertView() {
return this.itemView;
}
public void setText(int resId, CharSequence text) {
TextView textView = this.getView(resId);
textView.setText(text);
}
}
}
使用例子
private class MyAdapter extends AbsListMultiAdapter<String>{
public MyAdapter(Context mContext) {
super(mContext);
}
@Override
public int getItemLayoutResId(int position, String data) {
// 可以根据position或data判断
return position % 2 == 0 ? R.layout.item_voice_assistant : R.layout.v_textview;
}
@Override
public void onBindData(int position, String data, com.elife.mobile.base.AbsListAdapter3.ViewHolder holder) {
// TODO Auto-generated method stub
holder.setText(R.id.tv_textView, data);
}
}
完整代码在github,里面还有更多功能哦