Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set OnClickListener for ScrollView?

i'm developing an android project which it has a viewpager. i wrote an adapter for my viewpager and my adapter consist of s scrollview and some views inside that.

i want when user click on the scrollview, something happen (i wrote this part too and test it and it works).

i implement onClickListener for my scrollview but it's not triggered when user click on that.

i have already read this but it's not work for me.

my code for onClickListener is 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!!!");
        }
});

and my layout code is this:

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

like image 464
strings95 Avatar asked Jun 30 '14 17:06

strings95


People also ask

What is fillViewport in ScrollView?

fillViewport allows scrollView to extend it's height equals to the full height of device screen's height in the cases when the child of scroll view has less height.

What is the difference between NestedScrollView and ScrollView?

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. It is enabled by default. NestedScrollView is used when there is a need for a scrolling view inside another scrolling view.

Why my setOnClickListener is not working?

You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Show activity on this post.

How does setOnClickListener work?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.


1 Answers

The child Views of your ScrollView are consuming the click events you do on the ScrollView.

The solution to this problem is to set the onClickLIstener to the immediate child of the ScrollView, the FrameLayout in your case.

Don't forget to set the layout to "clickable".

Furthermore, I recommend not to use the FrameLayout at all, since it is just a useless container. Instead, just let the LinearLayout that is currently the child of the FrameLayout be the next child to the ScrollView.

like image 129
Philipp Jahoda Avatar answered Sep 25 '22 10:09

Philipp Jahoda