Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - OnClickListener not working for first click in recyclerview

I am working in a chat application for Android and I am using RecyclerView for listing the messages.

I have written the adapter, but I am having a problem with detecting when an element(TextView in this case) inside the layout is clicked.

This is my adapter:

public class ChatRoomThreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView message, timestamp;

        private ViewHolder(View view) {
            super(view);

            message = (TextView) itemView.findViewById(R.id.message); 
            timestamp = (TextView) itemView.findViewById(R.id.timestamp); 
        }

    }

    public ChatRoomThreadAdapter(Context mContext, ArrayList<Message> messageArrayList, String userId) {
        this.mContext = mContext;
        this.messageArrayList = messageArrayList;
        this.userId = userId;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        ((ViewHolder) holder).message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((ViewHolder) holder).timestamp.getVisibility() == View.GONE) {
                    ((ViewHolder) holder).timestamp.setVisibility(View.VISIBLE);
                } else {
                    ((ViewHolder) holder).timestamp.setVisibility(View.GONE);
                }

            }
        });

    }

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

The current onClick works but I have to click twice on the message in order for the onClick to trigger. I have been searching and trying endless solutions for 3 days in order to fix this, but none of the solutions on the internet have worked so far.

like image 502
zeeks Avatar asked Sep 18 '17 22:09

zeeks


3 Answers

Make sure you have both focusableInTouchMode & focusable disabled on the button. The first click will get the focus and the second click executes the onClickListener.

.

like image 101
king_abu1918 Avatar answered Oct 20 '22 20:10

king_abu1918


After scroll, recycler view items are not clickable and the issue is still open https://issuetracker.google.com/issues/66996774

Found a way to force click if the scroll state is still SCROLL_STATE_SETTLING

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView

class WrapperRecyclerView(context: Context, attributeSet: AttributeSet?, defStyle: Int) :
  RecyclerView(context, attributeSet, defStyle) {

  constructor(context: Context) : this(context, null, 0)

  constructor(context: Context, attributeSet: AttributeSet) : this(context, attributeSet, 0)

  override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
    val eventConsumed = super.onInterceptTouchEvent(event)

    when (event.actionMasked) {
      MotionEvent.ACTION_DOWN -> if (scrollState == SCROLL_STATE_SETTLING) {
        parent.requestDisallowInterceptTouchEvent(false)
        // only if it touched the top or the bottom.
        if (!canScrollVertically(-1) || !canScrollVertically(1)) {
          // stop scroll to enable child view to get the touch event
          stopScroll()
          // do not consume the event
          return false
        }
      }
    }

    return eventConsumed
  }
}
like image 39
Rocky Avatar answered Oct 20 '22 21:10

Rocky


The following class will fix this issue :

class WrapperRecyclerView(context: Context, attributeSet: AttributeSet?, defStyle: Int) :
    RecyclerView(context, attributeSet, defStyle) {

  constructor(context: Context) : this(context, null, 0)

  constructor(context: Context, attributeSet: AttributeSet) : this(context, attributeSet, 0)

  override fun onScrollStateChanged(state: Int) {
    super.onScrollStateChanged(state)
    if (state == RecyclerView.SCROLL_STATE_SETTLING) {
      this.stopScroll();
    }
  }
}
like image 1
Badhrinath Canessane Avatar answered Oct 20 '22 19:10

Badhrinath Canessane