0

In my case I want to open PopupWindow by long press on ViewHolder item and process motion event in this window without removing finger. How can I achieve this?

I trying to open CustomPopupWindow by follow:

override fun onBindViewHolder(holder: Item, position: Int) {
    val item = items[position]
    holder.bindView(testItem)
    holder.itemView.view.setOnLongClickListener {    
        val inflater = LayoutInflater.from(parent?.context)
        val view = inflater.inflate(R.layout.popup_window, null)
        val popupMenu = CustomPopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        popupMenu.elevation = 5f
        popupMenu.showAsDropDown(holder.itemView.view)
        true
    }
}

and after that disable scrolling in RecyclerView:

class CustomLayoutManager(context: Context) : LinearLayoutManager(context) {

    var scrollEnabled: Boolean = true

    override fun canScrollVertically(): Boolean {
        return scrollEnabled
    }
}

Here my CustomPopupWindow:

class CustomPopupWindow(contentView: View?, width: Int, height: Int) : PopupWindow(contentView, width, height), View.OnTouchListener {

    init {
        contentView?.setOnTouchListener(this)
        setTouchInterceptor(this)
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.i("Touch", "Touch")
            }
            MotionEvent.ACTION_MOVE -> {
                Log.i("Touch", "Event {${event.x}; ${event.y}}")
            }
            MotionEvent.ACTION_UP-> {
                Log.i("Touch", "Up")
            }
        }
        return true
    }
}

In this case onTouch() event never called in CustomPopupWindow only if I remove finger and tap again.

Thanks advance!

SOLVED

I solved this by adding a touch listener to the anchor view:

holder.itemView.view.setOnLongClickListener {    
    val inflater = LayoutInflater.from(parent?.context)
    val view = inflater.inflate(R.layout.popup_window, null)
    val popupMenu = CustomPopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    popupMenu.elevation = 5f
    it.setOnTouchListener(popupMenu) // solution
    popupMenu.showAsDropDown(it)
    true
}

Thanks @Brucelet

Anonymous
  • 301
  • 3
  • 8

1 Answers1

0

If you can refactor to using a PopupMenu, then I think PopupMenu.getDragToOpenListener() will do what you want. Similar for ListPopupWindow.createDragToOpenListener().

You could also look at the implementation of those methods for inspiration in creating your own.

RussHWolf
  • 3,555
  • 1
  • 19
  • 26
  • Thanks, for answer. It working fine. But I want to use custom view popup created from inflate. Is it possible in PopupMenu or ListPopupWindow? – Anonymous Mar 05 '18 at 15:28
  • Not easily, I don't think, but you could try looking at the implementation of those other methods and see if you can do something similar. – RussHWolf Mar 06 '18 at 01:22
  • Thanks for help, I will try it. But maybe there is some other way to solve this problem without menu? – Anonymous Mar 06 '18 at 06:16