您好,登錄后才能下訂單哦!
Android SimpleAdapter使用詳解
HolderAdapter背景
Android的AdapterView用的比較多,ListView,GridView,Spinner等,原生的BaseAdapter對ViewHolder沒有支持,每次都要,定義內部類,inflater根布局,對item內部view設置clicklistener并轉發到adapter的調用者等。寫的次數多了感覺很繁瑣,于是寫了個簡單的封裝,簡化Adapter的編寫,
倉庫連接http://git.oschina.net/phodev/SimpleAdapter.
主要文件介紹
1,CommonAdapter.java從BaseAdapter繼承,添加了常用的函數,通過泛型約定了List數據源,代替子類實現getCount功能,添加bindClick實現Adapter的OnItemClickListener。
2,BaseViewHolder.java和HolderAdapter結合,實現類似Activity的view初始化方方式onCreate->setContentView->findViewById。
3,HolderAdapter.java從CommonAdapter繼承,主要用來驅動BaseViewHolder工作。
使用方法
1,Adapter定義
public class SampleHolderAdapter extends HolderAdapter<Object> { //定義Item內部的點擊事件,如果不需要可以不定義 public static final int CLICK_ACTION_ADD = 1; public static final int CLICK_ACTION_UPDATE = 2; public static final int CLICK_ACTION_DELETE = 3; //構造函數配置數據源(可選,或者之后通過setData更新) public SampleHolderAdapter(List<Object> data) { super(data); } @Override public BaseViewHolder<?> createViewHolder(int position) { //創建期望的 ViewHolder return new ViewHolder(); } class ViewHolder extends BaseViewHolder<Object> { Button btn1, btn2, btn3; @Override protected void onCreate(Context context, ViewGroup parent) { //ViewHolder創建后onCreate會被執行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會被執行一次 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使用
//數據源 private final ArrayList<Object> mData = new ArrayList<>(); //創建Adapter并綁定數據源 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); //設置click listener(由于CommonAdapter支持,并通過bindClick聲明) mAdapter.setOnItemClickListener(onItemClickListener); } //ClickListener實現 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; } } };
到此定義和使用的代碼完畢。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。