您好,登錄后才能下訂單哦!
Android SimpleAdapter使用詳解
HolderAdapter背景
Android的AdapterView用的比較多,ListView,GridView,Spinner等,原生的BaseAdapter對ViewHolder沒有支持,每次都要,定義內(nèi)部類,inflater根布局,對item內(nèi)部view設(shè)置clicklistener并轉(zhuǎn)發(fā)到adapter的調(diào)用者等。寫的次數(shù)多了感覺很繁瑣,于是寫了個簡單的封裝,簡化Adapter的編寫,
倉庫連接http://git.oschina.net/phodev/SimpleAdapter.
主要文件介紹
1,CommonAdapter.java從BaseAdapter繼承,添加了常用的函數(shù),通過泛型約定了List數(shù)據(jù)源,代替子類實現(xiàn)getCount功能,添加bindClick實現(xiàn)Adapter的OnItemClickListener。
2,BaseViewHolder.java和HolderAdapter結(jié)合,實現(xiàn)類似Activity的view初始化方方式onCreate->setContentView->findViewById。
3,HolderAdapter.java從CommonAdapter繼承,主要用來驅(qū)動BaseViewHolder工作。
使用方法
1,Adapter定義
public class SampleHolderAdapter extends HolderAdapter<Object> { //定義Item內(nèi)部的點擊事件,如果不需要可以不定義 public static final int CLICK_ACTION_ADD = 1; public static final int CLICK_ACTION_UPDATE = 2; public static final int CLICK_ACTION_DELETE = 3; //構(gòu)造函數(shù)配置數(shù)據(jù)源(可選,或者之后通過setData更新) public SampleHolderAdapter(List<Object> data) { super(data); } @Override public BaseViewHolder<?> createViewHolder(int position) { //創(chuàng)建期望的 ViewHolder return new ViewHolder(); } class ViewHolder extends BaseViewHolder<Object> { Button btn1, btn2, btn3; @Override protected void onCreate(Context context, ViewGroup parent) { //ViewHolder創(chuàng)建后onCreate會被執(zhí)行1次 setContentView(R.layout.item_test);//setContent支持View或者layout id btn1 = (Button) findViewById(R.id.btn1);//findViewById btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); } @Override protected void onDataChanged(int position, Object data) { //adapter getView的時候onDataChanged會被執(zhí)行一次 btn1.setText("add " + position); btn2.setText("update " + position); btn3.setText("delete " + position); // bindClick(btn1, position, CLICK_ACTION_ADD);//綁定ItemClick,每次都需bind bindClick(btn2, position, CLICK_ACTION_UPDATE); bindClick(btn3, position, CLICK_ACTION_DELETE); } } }
2,Adapter使用
//數(shù)據(jù)源 private final ArrayList<Object> mData = new ArrayList<>(); //創(chuàng)建Adapter并綁定數(shù)據(jù)源 private final SampleHolderAdapter mAdapter = new SampleHolderAdapter(mData); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listview); //綁定Adapter mListView.setAdapter(mAdapter); //設(shè)置click listener(由于CommonAdapter支持,并通過bindClick聲明) mAdapter.setOnItemClickListener(onItemClickListener); } //ClickListener實現(xiàn) private OnItemClickListener onItemClickListener = new OnItemClickListener() { @Override public void onItemClick(CommonAdapter<?> adapter, View v, int position, int action) { switch (action) {//action就是我們SampleHolderAdapte中定義并且bindClick的value case SampleHolderAdapter.CLICK_ACTION_ADD: toast(getContext(), position + " add click"); break; case SampleHolderAdapter.CLICK_ACTION_UPDATE: toast(getContext(),position + " update click"); break; case SampleHolderAdapter.CLICK_ACTION_DELETE: toast(getContext(), position + " delete click"); break; } } };
到此定義和使用的代碼完畢。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。