单击滚动视图

Clicking a scroll view

我有一个滚动视图,它有一个 Linearlayout 子视图,这个 linearlayout 有一个 drawingview

问题:我尝试了关于这个主题的几乎所有可用答案,并且 none 有效,我不明白为什么没有触发点击(滚动视图、线性布局甚至绘图视图)。

注意:我尝试为三个中的每一个设置点击,none 有效。

xml代码:

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

您已为两者设置 android:clickable="false" 使其适用于您

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

首先你的ScrollView,layout_height包好了!对于 children 你给了 match_parent 我怀疑你的视图甚至不会显示。不是吗?

那么不知道你为什么使用 setorientationandroid:id="@id/draw" 而不是 android:orientationandroid:id="@+id

如果您想检查滚动效果,请使用

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

您正在使用不必要的属性检查此示例:

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

如果你写一个 onClickListner 到 id linear_layout 它就可以正常工作了!


回答:您的 ScrollView 的 child 视图正在消耗您在 ScrollView 上执行的点击事件。

这个问题的解决方法是将onClickLIstener设置为ScrollView的立即数child(是child)。

因此,根据我的示例,如果我想找出 ScrollView onClick,我会将我的侦听器写入其 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();
            }
        });

需要时阅读这里androidclickable-trueandroid:clickable="true" mean's that it's not clickable?

这是我的 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
            }
        });


    }
}