ScrollView阻止其父级单击侦听器

时间:2014-11-18 10:30:15

标签: android scrollview

我在RelativeLayout中使用滚动视图,我将onClickLIstener分配给RelativeLayout。

我正在使用以下代码。 Layoutfile

<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。但是当我删除滚动视图并单击textview时,它工作正常。任何人都可以告诉我为什么这个奇怪的行为在使用ScrollView ??

2 个答案:

答案 0 :(得分:0)

是否可以尝试为TextView添加以下触摸侦听器,之后,为Textview设置onclick Listener,而不是将其提供给相对布局。

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

答案 1 :(得分: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);
  }

android scrollable textview inside scrollview