android - OnClickListener在回收器视图中第一次点击不起作用

我正在Android的聊天应用程序中工作,并且我正在使用RecyclerView来列出消息。

我已经编写了适配器,但是我在检测何时单击布局中的元素(在本例中为TextView)时遇到问题。

这是我的适配器:

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();
    }
}

当前的 onClick 工作正常,但我必须单击两次才能触发 onClick。为了解决这个问题,我一直在寻找并尝试无休止的解决方案3天,但到目前为止,互联网上的解决方案都没有奏效。message


答案 1

确保在按钮上同时禁用了两者。第一次单击将获得焦点,第二次单击将执行 onClickListener。focusableInTouchModefocusable

.


答案 2

滚动后,回收站视图项目不可单击,并且问题仍 https://issuetracker.google.com/issues/66996774

找到了一种在滚动状态仍为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
  }
}

推荐