PopupWindow 已显示,但我仍然可以滚动 recyclerview

PopupWindow has been shown but I still can scroll the recyclerview

我想复制RecyclerView的项目中的文本内容,所以我在TextView上设置了一个OnLongClickListener,同时它会显示一个包含复制按钮的PopupWindow。

我的问题是,当 PopupWindow 显示并滚动 RecycleView 时我仍在触摸 RecycleView,但 RecycleView 无法滚动。

我需要的是,如果PopupWindow已经显示出来,不管我是否还在触摸RecyclerView,PopupWindow都应该有焦点,除非PopupWindow被关闭,否则我不能做其他事情。

我初始化一个 PopupWindow 代码:

mPopupWindow = new PopupWindow(context);
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
View contentView = LayoutInflater.from(context).inflate(R.layout.comment_popup_layout, null);
mPopupWindow.setContentView(contentView);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

我使用方法showAsDropDown(View anchor, int xoff, int yoff)来显示window。

我搜索了google很久了,需要一些帮助。

谢谢!

在adapter的构造函数中传递RecyclerView的对象class并初始化 然后在构造函数中添加这个

if(mPopupWindow.isShowing()){
          recyclerView.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          return true;
      }
  });

}

向 rootView 发送取消事件以停止滚动。

//Record an ACTION_DOWN event which is just used to obtain an ACTION_CANCEL event.
var mDownEvent: MotionEvent? = null

//itemView is the root view of the Holder
itemView.setOnTouchListener { _, event -> 
    if(event.action == MotionEvent.ACTION_DOWN)
        mDownEvent = event
    if(event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL)
        mDownEvent = null
}

显示弹出窗口后

if(mDownEvent != null) {
    try {
        val cancelEvent = MotionEvent.obtain(mDownEvent)
        cancelEvent.action = MotionEvent.ACTION_CANCEL
        itemView.rootView.dispatchTouchEvent(cancelEvent)
    } catch (e: Exception) {
        //log the exception
    }
}