当前位置: 代码迷 >> Android >> Android 完善对BaseAdapter进行二次封装
  详细解决方案

Android 完善对BaseAdapter进行二次封装

热度:18   发布时间:2016-04-28 02:31:19.0
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;	}}


  相关解决方案