Click/long press event of ListView and click/long press event of control in item

The click/long press event of listview is very simple. It is the same as the click/long press event of ordinary controls. The difference is that because it is a list, an item will be added to the event, which refers to the click/long press of each item in the listview. by event

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtils.showShort("点击"+list.get(position));
            }
        });
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtils.showShort("长按"+list.get(position));
                return false;
            }
        });

But each click/long press represents the whole of the item. If we want to click a control in the item, we can use the following method to write a click event interface in the adapter, and then implement this in the activity interface on the line

The interface is defined in the adapter

    private onItemListener mOnItemListener;
    public void setOnItemClickListener(onItemListener mOnItemListener) {
        this.mOnItemListener = mOnItemListener;
    }
    public interface onItemListener {
        void onSelectClick(int i);
    }

Bind to the control you want to click in the item

         holderView.mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemListener.onSelectClick(position);
            }
        });

Then implement it in the activity

        main3Adapter.setOnItemClickListener(new Main3Adapter.onItemListener() {
            @Override
            public void onSelectClick(int i) {
                ToastUtils.showShort("点击"+i);
            }
        });

This method is also applicable in recyclerview

Guess you like

Origin blog.csdn.net/lanrenxiaowen/article/details/108219510