1

I need to intercept on Touch Event, so I created a CustomSupportMapFragment, but never get the onTouchEvent, I have the same code for the GMS version and goes fine. So I don't know. Here is my code:

class TCSupportMapFragment: SupportMapFragment() {
    private var mListener: OnTouchListener? = null

    override fun onCreateView(inflater: LayoutInflater, parent: ViewGroup?, savedInstanceState: Bundle?): View {
        val layout = super.onCreateView(inflater, parent, savedInstanceState)

        val frameLayout = TouchableWrapper(requireContext())
        frameLayout.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.transparent))
        (layout as ViewGroup).addView(frameLayout, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        return layout
    }

    fun setListener(listener: OnTouchListener?) {
        mListener = listener
    }

    interface OnTouchListener {
        fun onTouch()
    }

    inner class TouchableWrapper(context: Context) : FrameLayout(context) {
        override fun dispatchTouchEvent(event: MotionEvent): Boolean {
            when (event.action) {
                MotionEvent.ACTION_DOWN -> mListener?.onTouch()
                MotionEvent.ACTION_UP -> mListener?.onTouch()
            }
            return super.dispatchTouchEvent(event)
        }
    }
}

In Activity:

override fun onTouchListener() {
        this.binding.nestedScroll.requestDisallowInterceptTouchEvent(true)
    }

1 Answers1

0

You should intercept the touch where you are doing the logic instead of building the new fragment type using the following:

View view = inflater.inflate(R.layout.fragment_test, container, false);

    view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    //do something
                }
                return true;
            }
    });

//here the rest of your codereturn view;

You can also refer to some similar topics here.

Zinna
  • 1,947
  • 2
  • 5
  • 20