0

I want to copy the text content in item of a RecyclerView, so I set a OnLongClickListener on the TextView, while it will show a PopupWindow that contain a copy button.

My problem is that while I am still touching the RecycleView when the PopupWindow has shown and scroll the RecycleView, the RecycleView is unexpected to scroll.

I need that if the PopupWindow has shown, no matter I am still touching the RecyclerView or not, the PopupWindow should have the facus, and I can't do others unless the PopupWindow is dismissed.

enter image description here

my init a PopupWindow code:

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

I use the method showAsDropDown(View anchor, int xoff, int yoff) to show the window.

Need some help, after I have seached google for a long time.

Thanks!

naiyu
  • 745
  • 3
  • 9
  • 25

2 Answers2

1

Pass the object of RecyclerView in constructor of adapter class and initialize it then add this in constructor

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

}

Quick learner
  • 10,632
  • 4
  • 45
  • 55
1

Dispatch a cancel event to the rootView to stop scroll.

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

After show PopupWindow

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