0

I have a full screen RecyclerView which will have one invisible ViewHolder Item, like below

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
   .... 
   if (position == 6) {
      viewHolder.itemView.setMinimumHeight(Resources.getSystem().getDisplayMetrics().heightPixels);
      viewHolder.itemView.setVisibility(View.GONE);
      viewHolder.setIsRecyclable(false);
   }
   ...

}

Once the position 6 shows up on the screen, I can see the ImageView behind it and I'd like to be able to click on that. I have added an event handler to that ImageView but it is not being triggered. It seems RecyclerView is preventing the click event to bubble down. Is there any way to click a View thru invisible/gone RecyclerView ViewItem?

1

1 Answer 1

0

Since I asked the question, I have tried multiple techniques/methods which could/should pass the click/tap event down to the view hierarchy but nothing worked. The feature I was trying to build in the app was very complex and the app itself became very complicated overtime. Too many views on top of each other and global event handlers made the implementation harder.

So I have decided as last resort that to have an empty/transparent view holder in the RecyclerView which listens the click and touch events and based on the coordinates of the touch event, I fire different action. Here is the code:

private float[] lastTouchDownXY = new float[2];

public MyView getMyView(final Context context) {       
    MyView view = new MyView(context);

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                lastTouchDownXY[0] = motionEvent.getRawX();
                lastTouchDownXY[1] = motionEvent.getRawY();
            }

            return false;
        }
    });

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final float x = lastTouchDownXY[0];
            final float y = lastTouchDownXY[1];
            int[] mLocButton = new int[2];
            // mButton is the button in the background and visible thru transparent viewholder
            mButton.getLocationOnScreen(mLocButton);
            final int left = mLocButton[0];
            final int top = mLocButton[1];

            if (x > (left - mOffset) && x < (left + mOffset + mButtonWidth) &&
                    y > (top - mOffset) && y < (top + mOffset + mMuteUnmuteButtonHeight)) {
                // mButton clicked
            } else {
                // entire view clicked except mButton clickable area
            }
        }
    });

    return view;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.