Android 完美对BaseAdapter进行二次封装
在开发列表的界面的时候经常会重写BaseAdapter,利用网上的知识点技巧,自己对它进行了二次封装方便以后在项目中再次使用。使用起来方便得很。
当然如何你看了代码可以的话,也可以对ExpandableListAdapter同样的封装!
使用自定义BaseAdapter:
package com.cyy.myandroid; import android.content.Context; import android.provider.ContactsContract.Data; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.view.View; import android.widget.TextView; public class RankingListAdapter extends BaseAdapter<Data> { public RankingListAdapter(Context context) { super(context); } @Override public int getContentView() {//item布局 return R.layout.activity_main_item; } @Override public void onInitView(View view, int position) { TextView tvNumb = get(view, R.id.tv); // 直接item布局里的控件 Data data = getItem(position); tvNumb.setText(String.valueOf(position + 1)); } }
自定义BaseAdapter:
package com.cyy.myandroid; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public abstract class BaseAdapter<T> extends android.widget.BaseAdapter { private List<T> list; protected Context context; public BaseAdapter(Context context) { init(context, new ArrayList<T>()); } public BaseAdapter(Context context, List<T> list) { init(context, list); } private void init(Context context, List<T> list) { this.list = list; this.context = context; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public void clear() { this.list.clear(); notifyDataSetChanged(); } public void addAll(List<T> list) { if (list != null) { this.list.addAll(list); notifyDataSetChanged(); } } @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) { if (null == convertView) { convertView = inflate(getContentView()); } onInitView(convertView, position); return convertView; } /** 加载布局 */ private View inflate(int layoutResID) { LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(layoutResID, null); return view; } public abstract int getContentView(); public abstract void onInitView(View view, int position); /** * * @param view * converView * @param id * 控件的id * @return 返回<T extends View> */ @SuppressWarnings("unchecked") protected <E extends View> E get(View view, int id) { SparseArray<View> viewHolder = (SparseArray<View>) view.getTag(); if (null == viewHolder) { viewHolder = new SparseArray<View>(); view.setTag(viewHolder); } View childView = viewHolder.get(id); if (null == childView) { childView = view.findViewById(id); viewHolder.put(id, childView); } return (E) childView; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。