ScrollView阻止其父视图的点击监听器

3

我正在RelativeLayout内使用ScrollView,并为RelativeLayout分配了onClickLIstener。

我正在使用以下代码。 Layout文件

<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 时会出现这种奇怪的行为吗?


你有为RelativeLayout设置任何子点击监听器吗? - Haresh Chhelana
你尝试过在你的RelativeLayout中添加android:clickable="true"吗? - peguerosdc
@HareshChhelana:不是的 - Rohit
@peguerosdc:我尝试添加了那个,但仍然不起作用。 - Rohit
2个回答

0
请尝试为TextView添加以下触摸监听器,然后将点击监听器设置为TextView,而不是将其赋给相对布局。
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;
    }
};

我想为我的应用程序添加RelativeLayout的点击监听器,所以我不能将其用作TextView的监听器。 - Rohit

0
尝试从ViewParent中使用requestDisallowInterceptTouchEvent方法:http://developer.android.com/reference/android/view/ViewParent.html#requestDisallowInterceptTouchEvent(boolean)
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      this.getParent().requestDisallowInterceptTouchEvent(true);
    }
    if (ev.getAction() == MotionEvent.ACTION_UP) {
      this.getParent().requestDisallowInterceptTouchEvent(false);
    }

    return super.onTouchEvent(ev);
  }

在ScrollView内部实现可滚动的TextView


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接