单击滚动视图 [英] Clicking a scroll view
问题描述
我有一个滚动视图,它有一个 Linearlayout
子元素,这个 linearlayout
有一个 drawingview
.
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.
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>
推荐答案
首先你的 ScrollView
,layout_height
被包裹了!对于您给 match_parent
的孩子,我怀疑您的视图甚至不会显示.不是吗?
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?
那么不知道为什么你使用 setorientation
和 android:id="@id/draw"
而不是 android:orientation
和 >android:id="@+id
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>
如果您将 onClickListner
写入 id linear_layout
它就可以正常工作!
If you write an onClickListner
to the id linear_layout
it just work normally!
答案:ScrollView
的子视图正在消耗您在 ScrollView
上执行的点击事件.
Answer :The child Views of your ScrollView
are consuming the click events you do on the ScrollView
.
解决这个问题的方法是将onClickLIstener
设置为ScrollView
的直接子元素(它的子元素).
The solution to this problem is to set the onClickLIstener
to the immediate child of the ScrollView
(It's child).
所以根据我的例子,如果我想找出 ScrollView
onClick 我写我的听众给它的孩子.
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();
}
});
当您需要 androidclickable-true 时阅读此处 android:clickable=true";意思是它不可点击?
Read here when you need androidclickable-true android:clickable="true" mean's that it's not clickable?
这是我的活动
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
}
});
}
}
这篇关于单击滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!