-1

I have a recyclerView and its items contain a button. And I wanna get the recyclerview item position when I click on the button. Here is the problem, when the button clicked the button clicked event is triggered but the recyclerview item under neath is not, the clicked event can not go through the button on the top to recyclerview item. How can I get this click-through event or how can I bind them together? Please someone tell me if know the answer.

update: I could not find any similar post online. usually recyclerview contains button is register to different OnclickedListener. In my case, they need to handle the same listener, particularly the recyclerview handle it. But the button just block the clicked event of recyclerview. here is the viewholder code: `public class GridViewAdapter extends RecyclerView.Adapter {

    private final LinkedList<Device> mValues;
    private final OnListFragmentInteractionListener mListener;
    private OnLongClickListener mOnLongClickListener = null;

    public GridViewAdapter(LinkedList<Device> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.device, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
//        holder.mIconView.setImageResource(R.drawable.ic_switch);

        holder.mNameView.setText(holder.mItem.device_name());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onDeviceClicked(holder.mItem);
                }
            }
        });
    }

    public void deleteByDeviceCode(int deviceCode) {
        for(int i=0;i<mValues.size();i++) {
            if (mValues.get(i).device_code() == deviceCode) {
                mValues.remove(i);
            }
        }
    }

    public void deleteByPosition(int pos) {
        mValues.remove(pos);
    }

    public int getDeviceCodeByPosition(int pos) {
        return mValues.get(pos).device_code();
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final Button mButton;
        public final TextView mNameView;
        public Device mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mButton = (Button) view.findViewById(R.id.dev_button);
            mNameView = (TextView) view.findViewById(R.id.dev_lable);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mNameView.getText() + "'";
        }

    }
}`
Anson Chan
  • 635
  • 5
  • 5

1 Answers1

0

When creating view holders, try passing them index of element represented by them, or even reference to whole element. Now, when you've receiving click event in view holder, with event itself you should pass the (index of) element that is represented by that holder/row.

SadClown
  • 648
  • 8
  • 16
  • problem is the view holder received no click event if it contains a button in it. the button receives clicking event, the recyclerview holder does not. – Anson Chan Nov 11 '16 at 03:51
  • ViewHolder itself _cannot_ receive click event, only views can do it. Keep the reference to the button inside of the view holder, and you should there receive and handle click events. If you want WHOLE item to receive click, you could hack it by making item's root view clickable, and attaching on click listener as if it were a button. – SadClown Nov 15 '16 at 13:58