android - recyclerview如何给item设置onclicklistener 同时给item中的一个button设置onclick
迷茫
迷茫 2017-04-18 09:18:52
0
3
586

如题所述,最好不要每次都new一个listener

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
黄舟

Get the component in ViewHolder and set up the monitoring

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TestHolder(View view){
            super(view);
            view.setOnClickListener(this);
            View button = view.findViewById(R.id.recycler_item_button);
            button.setOnClickListener(this);

        }
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.recycler_item_button){
                Toast.makeText(context,"button"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context,"item"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
            }
        }
    }
阿神

Set an onclicklistener for the layout of the item's view in the adapter's onbindviewholder, and set a listener for the Button

黄舟

Recommend https://github.com/CymChad/Ba...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template