You can position the OnTouchListener at the top of the Fragment by adding it to the topmost view in the Fragment's layout file.
For example, if you have a Fragment layout file that contains a LinearLayout, you can add the OnTouchListener to the LinearLayout by setting its OnTouchListener attribute in the XML layout file:
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onTouchListener="@{viewModel.onTouchListener}"
... >
...
</LinearLayout>
This tells the LinearLayout to use the OnTouchListener specified in the ViewModel to handle touch events.
Alternatively, you can programmatically add the OnTouchListener to the topmost view in the Fragment's onCreateView() method:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_layout, container, false)
view.setOnTouchListener { _, event ->
// handle touch events here
true
}
return view
}
This adds an OnTouchListener to the topmost view in the Fragment and handles touch events there.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-10-27 11:00:00 +0000
Seen: 11 times
Last updated: Aug 15 '22