Java – how to set onclicklistener for Scrollview?
I'm developing an Android project with viewpager I wrote an adapter for my viewpager. My adapter consists of s Scrollview and some views inside
I think something will happen when the user clicks on the scroll view (I also wrote this section and tested it and it works)
I implemented onclicklistener for my Scrollview, but it won't be triggered when the user clicks it
I've read this, but it doesn't work for me
My onclicklistener code is right here
rootView = inflater.inflate(R.layout.level_selector_list_view_adapter,container,false); ScrollView scroll = (ScrollView) rootView.findViewById(R.id.level_selector_view_scroll_view); scroll.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Log.e(MainActivity.WATERMELON_TAG,"Click!!!"); } });
My layout code is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="@dimen/level_selector_values_main_page_view_pager_width" android:layout_height="wrap_content" android:orientation="vertical" > <ScrollView android:id="@+id/level_selector_view_scroll_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" tools:ignore="UselessParent" > <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="false" tools:ignore="UselessParent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:clickable="false" android:orientation="vertical" > <ImageView android:id="@+id/level_selector_view_pager_adapter_level_logo" android:layout_width="@dimen/level_selector_values_view_pager_adapter_level_logo_width" android:layout_height="@dimen/level_selector_values_view_pager_adapter_level_logo_height" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/level_selector_values_view_pager_adapter_level_logo_top_margin" android:clickable="false" android:scaleType="fitXY" tools:ignore="ContentDescription" /> <TextView android:id="@+id/level_selector_view_pager_adapter_level_name" android:layout_width="@dimen/level_selector_values_view_pager_adapter_level_name_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/level_selector_values_view_pager_adapter_level_name_top_margin" android:clickable="false" android:gravity="center" android:textColor="@color/level_selector_values_level_name_font_color" android:textSize="@dimen/level_selector_values_level_name_font_size" /> <TextView android:id="@+id/level_selector_view_pager_adapter_level_score" android:layout_width="@dimen/level_selector_values_view_pager_adapter_level_score_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/level_selector_values_view_pager_adapter_level_score_top_margin" android:clickable="false" android:gravity="center" android:textColor="@color/level_selector_values_level_name_font_color" android:textSize="@dimen/level_selector_values_font_size" /> </LinearLayout> <ImageView android:id="@+id/level_selector_view_pager_adapter_lock" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:alpha="0.75" android:clickable="false" android:scaleType="fitXY" android:src="@drawable/lock" tools:ignore="ContentDescription" /> </FrameLayout> </ScrollView>
Solution
Child views of Scrollview are consuming the click events you performed on Scrollview
The solution to this problem is to set onclicklister as a direct child of Scrollview, that is, FrameLayout. In your case
Don't forget to set the layout to clickable
In addition, I don't recommend using FrameLayout because it's just a useless container Instead, just make the LinearLayout of the child of the current FrameLayout the next child of Scrollview