I have a scrollView with lot of elements
ScrollView scroller = (ScrollView)findViewById(R.id.scrollView);
I need to attach an onClickListener to the scrollview so I do
scroller.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // This is all you need to do to 3D flip AnimationFactory.flipTransition(viewAnimator, FlipDirection.LEFT_RIGHT); } });
But this is not getting triggered when I touch. Any Ideas?
In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.
android:fillViewport. Defines whether the scrollview should stretch its content to fill the viewport.
They're completely different. A ScrollView is simple a scrolling container you can use to scroll whatever you put inside it, which might be a list of items, or it might not. A ListView is very specifically designed to hold lists, where items typically look the same (or at least follow a pattern, e.g. section headings).
In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.
The best solution seem to put LinearLayout
into ScrollView
and set the setOnClickListener
on it.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myLayout" android:clickable="true" android:orientation="vertical"> <!-- content here --> </LinearLayout> </ScrollView>
in the Activity :
LinearLayout lin = (LinearLayout) fragment.rootView.findViewById(R.id.myLayout); lin.setOnTouchListener(new setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Whatever } });
It is because the child of the ScrollView
is getting the touch event of the user and not the ScrollView
. You must set android:clickable="false"
attribute to each and every child of the ScrollView
for the onClickListener
to work on ScrollView
.
Or else the alternate could be to set the onClickListener
on each of the ScrollView's children and handle it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With