Clicking a scroll view

2019-07-29 05:59发布

问题:

I have a scroll view that has a Linearlayout child and this linearlayout has a drawingview.

the problem : I tried almost all the available answers on this topic and none of them worked, I cant understand why click is not fired for (scrollview, linear layout and even drawingview).

note : I tried setting on click for each of the three and none worked.

the xml code:

<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@id/scrollview"
android:clickable="true"
>

<LinearLayout
android:setorientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@id/linear"
android:clickable="false">


<Customview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@id/draw"
android:clickable="false"/>

</LinearLayout>
</ScrollView>

回答1:

First your ScrollView ,layout_height is wrapped! and for children you gave match_parent i doubt your view won't even display. Isn't it?

Then no idea why you use setorientation and android:id="@id/draw" instead of android:orientation and android:id="@+id

If you want to check scrolling works use

   scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                Log.d("TAG", "onScrollChanged: ");
                // DO SOMETHING WITH THE SCROLL COORDINATES
            }
        });

You are using unnecessary attributes check this example :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#8768"
    android:orientation="vertical">

    <ScrollView

        android:id="@+id/scrollIndicatorDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#823">

        <LinearLayout
            android:id="@+id/first_child"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#900"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linear_layout"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="#000"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/web_view"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:background="#987"
                android:orientation="vertical"></LinearLayout>


        </LinearLayout>
    </ScrollView>

</LinearLayout>

If you write an onClickListner to the id linear_layout it just work normally!


Answer :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(It's child).

So according to my example if i want to find out the ScrollView onClick i write my listener to its child.

 scrolChild =(LinearLayout) findViewById(R.id.first_child) ;


        scrolChild .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(YourActivityName.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

Read here when you need androidclickable-true android:clickable="true" mean's that it's not clickable?

This is my activity

   public class MainActivity extends AppCompatActivity {


    private ScrollView scrollView;

    private LinearLayout scroolChild;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);

        scrollView = (ScrollView) findViewById(R.id.scrollIndicatorDown);
        scroolChild =(LinearLayout) findViewById(R.id.first_child) ;


        scroolChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You Clicked Me OH yeaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                Log.d("TAG", "GotAScrollChanged: ");
                // DO SOMETHING WITH THE SCROLL COORDINATES
            }
        });


    }
}


回答2:

You have set android:clickable="false" for both make it true it will work for you

<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@id/scrollview"
    android:clickable="true"
    >

    <LinearLayout
    android:setorientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@id/linear"
    android:clickable="true">


    <Customview
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@id/draw"
    android:clickable="true"/>

    </LinearLayout>
    </ScrollView>