【问题标题】:PopupWindow has been shown but I still can scroll the recyclerviewPopupWindow 已显示,但我仍然可以滚动 recyclerview
【发布时间】:2017-03-21 07:14:09
【问题描述】:

我想复制 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)的方法来显示窗口。

在我搜索谷歌很长时间之后需要一些帮助。

谢谢!

【问题讨论】:

  • mPopupWindow.setOutsideTouchable(false);使用这个
  • mPopupWindow.setOutsideTouchable(true);使其为假 mPopupWindow.setOutsideTouchable(false);禁用背景元素触摸事件
  • @quicklearner 我试过了,但我仍然可以滚动我的 RecyclerView
  • 你可以在这里发布你的适配器类以便我可以帮助你吗?
  • @quicklearner 这是一个很长的代码。但是itemHold.setCommentClick(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mCommentPopDialog.show(v, item.getGroupId(), item.getContent(), item.getUserName()); return true; } });

标签: android android-recyclerview popupwindow android-popupwindow


【解决方案1】:

在适配器类的构造函数中传递RecyclerView的对象并初始化 然后在构造函数中添加这个

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

}

【讨论】:

    【解决方案2】:

    向 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多