HolderAdapter background

The native BaseAdapter has no support for ViewHolder. Each time you define an inner class, you define an inflater root layout. Set a ClickListener for the view inside the item and forward it to the adapter caller, etc. Write the number of times feel very tedious, so write a simple package, simplify the preparation of Adapter.

Method of use

1, Adapter definitions

public class SampleHolderAdapter extends HolderAdapter<Object> {
    //Defines click events within an Item, if not necessary
    public static final int CLICK_ACTION_ADD = 1;
    public static final int CLICK_ACTION_UPDATE = 2;
    public static final int CLICK_ACTION_DELETE = 3;
    //The constructor configures the data source (optional, or later updated via setData)
    public SampleHolderAdapter(List<Object> data) {
        super(data);
    }

    @Override
    public BaseViewHolder
        createViewHolder(int position) {
        //Create the desired ViewHolder
        return new ViewHolder(a); }class ViewHolder extends BaseViewHolder<Object> {
        Button btn1, btn2, btn3;

        @Override
        protected void onCreate(Context context.ViewGroup parent) {
            //OnCreate is executed once the ViewHolder is created
            setContentView(R.layout.item_test);//SetContent supports View or 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) {
            //OnDataChanged is executed once as adapter getView
            btn1.setText("add " + position);
            btn2.setText("update " + position);
            btn3.setText("delete " + position);
            //
            bindClick(btn1, position, CLICK_ACTION_ADD);//Bind ItemClick each time
            bindClick(btn2, position, CLICK_ACTION_UPDATE);
            bindClick(btn3, position, CLICK_ACTION_DELETE); }}}Copy the code

2, the Adapter is used

//The data source
 private final ArrayList<Object> mData = new ArrayList<>(a);//Create the Adapter and bind the data source
 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);
     //Binding Adapter
     mListView.setAdapter(mAdapter);
     //Set the Click Listener (as CommonAdapter supports, and declared via bindClick)
     mAdapter.setOnItemClickListener(onItemClickListener);
 }

 //ClickListener implementation
 private OnItemClickListener onItemClickListener = new OnItemClickListener() {
     @Override
     public void onItemClick(CommonAdapter
        adapter.View v.int position.int action) {
         switch (action) {//Action is the value defined in our SampleHolderAdapte and bindClick
             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; }}};Copy the code

This is the end of the defined and used code.