How to handle Touch Events on a Fragment?
In Kotlin:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_img_view, container, false)
view.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_MOVE) {
scaleGestureDetector.onTouchEvent(event)
}
return true
}
})
return view
}
I'm not sure if I understood your problem, but I will try to answer this. So to get touch events on fragment I would do this:
-in your fragment onCreateView:
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 code
return view;
and you can check for different MotionEvents in onTouch, for example:
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP,...