Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener on scrollView

Tags:

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?

like image 365
Geo Paul Avatar asked May 27 '13 15:05

Geo Paul


People also ask

Can we set OnClickListener on TextView?

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.

What is fillViewport in ScrollView?

android:fillViewport. Defines whether the scrollview should stretch its content to fill the viewport.

What is the difference between ListView and ScrollView?

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).

What is the use of ScrollView?

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.


2 Answers

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         } }); 
like image 87
JulienGenoud Avatar answered Sep 30 '22 00:09

JulienGenoud


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.

like image 33
bakriOnFire Avatar answered Sep 29 '22 23:09

bakriOnFire