6

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>

Community
  • 1
  • 1
strings95
  • 661
  • 11
  • 26

2 Answers2

10

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.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
0

try:

public class ClassName implements OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_child, container, false);
        ScrollView scrollView= (ScrollView) root.findViewById(R.id.my_ScrollView);

        scrollView.setOnClickListener(this);

        return root;
    }
    @Override
    public void onClick(View v) {
        Log.e(MainActivity.WATERMELON_TAG, "Click!!!");
    }
}
user3711421
  • 1,658
  • 3
  • 20
  • 37
  • it's a good idea at the first look! but i try it doesn't either :(( – strings95 Jun 30 '14 at 18:21
  • Okey, please post your java code as well, and lets see if we can figure it out. I recently made an app which does what you want. The only difference is that i have an textview inside the scrollview which stretches the whole page. Than i made the textView clickable. Oh, and you changed the root.findViewById(R.id.my_scrollView) to R.id.level_selector_view_scroll_view . Right? – user3711421 Jun 30 '14 at 18:28
  • Philipp Jahoda is correct. Your ScrollView is hidden behind your other Views, therefore you cannot click it. If you would try to make the picture id = level_selector_view_pager_adapter_level_logo clickable, this would work. Because it is on top of all the views, likewise your TextViews. – user3711421 Jun 30 '14 at 19:26