android - ScrollView 阻止了它的父点击监听器

标签 android scrollview

我在 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/

相关文章:

android - ScrollView 内具有可扩展 textView 的垂直居中 ConstraintLayout (滚动/居中问题)

android - 处理网络断开的最佳方式

android - 是否可以始终在 ListView 中启用快速滚动?

android - 自定义首选项以 ClassCastException 结束

android - 顶部 View 在 ScrollView 中消失

ios - 更改 ScrollView SwiftUI 的背景颜色

android - 如何使用 Kotlin 访问 LiveData<List<MVoice>> 的每个元素?

android - environment.getExternalStorageDirectory() 显示甚至不存在的路径

qt - 在 ScrollView 中编辑 TextInput

xcode - 如何使 scrollView 自动滚动?