我在 RelativeLayout 中使用 ScrollView ,并将 onClickLIstener 分配给 RelativeLayout。
我正在使用以下代码。 布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/row2_lnr_layout_ver">
<ScrollView
android:id="@+id/row2_scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:id="@+id/sa_sub_txt_view_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="0.4"
android:paddingLeft="10dp"
android:text="hjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"
android:textColor="@android:color/black">
</TextView>
</ScrollView>
</RelativeLayout>
OnCreate() 中的 Java 文件
((RelativeLayout)findViewById(R.id.row2_lnr_layout_ver)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("MainActivity", "onSmartActionClicked");
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
所以当我点击TextView或者scrollview的时候,并没有显示Toast。但是当我删除 ScrollView 并单击 textview 时,它工作正常。谁能告诉我为什么在使用 ScrollView 时会出现这种奇怪的行为??
最佳答案
您能否尝试为 TextView 添加以下触摸监听器,然后为 Textview 设置 onclick 监听器,而不是将其提供给相对布局。
private OnTouchListener textViewTouchListener= new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
v.onTouchEvent(event);
return true;
}
};
关于android - ScrollView 阻止了它的父点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992049/