【问题标题】:Android scrollview inside another scrollview doesn't scroll另一个滚动视图中的Android滚动视图不滚动
【发布时间】:2014-02-17 08:55:07
【问题描述】:

我正在尝试将 ScrollView 放在另一个 ScrollView 中,并尝试了在此站点上找到的答案,但它似乎仍然无法完全正常工作。这是 XML:

 <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewDirectoryDetailName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/red" />

        <LinearLayout
            android:id="@+id/linearLayoutDirectoryDetailContainImage"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:background="@drawable/general_image"
            android:gravity="center"
            android:orientation="vertical" >
        </LinearLayout>

        <ScrollView
            android:id="@+id/scrollView2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:focusableInTouchMode="false" >

            <TextView
                android:id="@+id/textViewDirectoryDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:autoLink="phone"
                android:text="098 123 45 678"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </ScrollView>

这里是 Java 代码:

parentScrollView = (ScrollView) findViewById(R.id.scrollView1);
    childScrollView = (ScrollView) findViewById(R.id.scrollView2);

    parentScrollView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View p_v, MotionEvent p_event) {
            childScrollView.getParent().requestDisallowInterceptTouchEvent(
                    false);
            // We will have to follow above for all scrollable contents
            return false;
        }
    });
    childScrollView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View p_v, MotionEvent p_event) {
            // this will disallow the touch request for parent scroll on
            // touch of child view
            p_v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });
    // We will have to follow above for all child scrollable contents

似乎发生了这样的事情:当我在 TextView 中有文本时,它似乎根本不允许我滚动它。它只是滚动父级。但是当我没有文本并且我触摸子 ScrollView 时,它不会滚动父级,所以我猜它正在工作。但是你有什么想法为什么当我有文本时它不起作用?

【问题讨论】:

  • 我正在尝试将 ScrollView 放在另一个 ScrollView 中 - 不要这样做,当您有两个不同的滚动区域时,如何期望滚动工作?跨度>
  • 这实际上是个坏主意。
  • 移除scrollView2。你为什么把它放在首位,你想达到什么目的?
  • 我正在为一个客户开发这个应用程序,他已经在 IOS 中实现了这个应用程序。问题是某些项目的描述可能太长,在这种情况下,我使用子滚动视图向下滚动。我见过人们这样做,所以我也想这样做。
  • 这可能对你有帮助grishma102.blogspot.in/2014/01/…

标签: android scrollview


【解决方案1】:

你应该使用NestedScrollView

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <!-- -->
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 我有另一个项目有同样的问题,这工作。谢谢你!
  • 这是一个简单的解决方案。我只将它用于内部滚动视图;无需更改外部滚动视图。
  • 顺便说一句,iOS 和 Android 处理嵌套滚动有点不同。在 iOS 上,触摸内部滚动视图内部只会滚动内部视图,而触摸内部滚动视图外部但在外部滚动视图内部会滚动外部视图。在带有 NestedScrollView 的 Android 上,触摸内部滚动视图内部将滚动内部视图,直到达到任一方向的滚动限制,然后开始滚动外部视图。
【解决方案2】:

虽然老问题, 我发布了对我有用的东西,我理解这个问题,因为我也经历过它,我只是删除了 XML 中 TextView 的 ScrollView,而是在 java 代码中实现了 TextView 的滚动功能。因此, XML 将是:

    <TextView
        android:id="@+id/textViewDirectoryDetailName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/red" />

    <LinearLayout
        android:id="@+id/linearLayoutDirectoryDetailContainImage"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="@drawable/general_image"
        android:gravity="center"
        android:orientation="vertical" >
    </LinearLayout>   
            <TextView
            android:id="@+id/textViewDirectoryDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:autoLink="phone"
            android:text="098 123 45 678"
            android:textAppearance="?android:attr/textAppearanceLarge" />

Java 代码是

 TextView extViewDD =  
    (TextView)findViewById(R.id.textViewDirectoryDescription);    
    //for Scrolling of textView       
    textViewDD.setMovementMethod(new ScrollingMovementMethod());
//to stop parent linearlayout scrollview when touched on textview
    textViewDD.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

【讨论】:

  • 很好的答案。干净简单。
  • 谢谢!这是解决这个问题的最简单和最干净的解决方案。
【解决方案3】:

在另一个 Scroll View 中包含一个 Scroll View 不是一个好习惯。但是,您使用的代码在某些情况下可以工作。但是当你有多个元素时它可能会失败。在这种情况下,它可能无法识别手势。

但是,如果对 ScrollView Inside ScrollView 有帮助,您可以尝试此答案。当识别到孩子的触摸时,它会禁用父 ScrollView 的触摸。如果有帮助,也看看这个post

【讨论】:

  • 我已经尝试过这些方法,但我总是在日志中得到 PARENT TOUCH 而从来没有 CHILD TOUCH。
  • 对不起,我听不懂你在说什么。您能否展示您尝试过的方法以及您面临的问题。
  • 我的意思是大多数方法都是一样的。例如,我用过这个:link,当我触摸子滚动视图时,它仍然说我已经触摸了父级,它根本不起作用。
【解决方案4】:

我使用 RectF 来测试你的手指是否在你的 child_view 的范围内。

我已经将它用于滚动视图和 viewpager 之间的混合(包含两个带有 recycleviews 的页面)的案例。

试试这些代码,你会得到你想要的。

findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            View childV = findViewById(R.id.child_view);
            if (childV != null) {
                int[] l = new int[2];
                childV.getLocationOnScreen(l);
                RectF rect = new RectF(l[0], l[1], l[0] + childV.getWidth(), l[1] + childV.getHeight());
                if(rect.contains(  event.getX(), event.getY())) {
                    childV.getParent()
                            .requestDisallowInterceptTouchEvent(false);
                    childV.onTouchEvent(event);
                    return true;
                }
            }
            childV.getParent()
                    .requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

【讨论】:

    【解决方案5】:

    检查this 链接,该链接显示如何在同一活动中使用两个具有滚动功能的小部件。检查下面的代码。

    parentScroll.setOnTouchListener(new View.OnTouchListener() {
    
                public boolean onTouch(View v, MotionEvent event) {
                    Log.v("PARENT", "PARENT TOUCH");
                    findViewById(R.id.child_scroll).getParent()
                            .requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            });
            childScroll.setOnTouchListener(new View.OnTouchListener() {
    
                public boolean onTouch(View v, MotionEvent event) {
                    Log.v("CHILD", "CHILD TOUCH");
                    // Disallow the touch request for parent scroll on touch of
                    // child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
    

    【讨论】:

    • 这与我上面的代码相同,除了日志,但我也会尝试一下,只是为了 100% 确定。
    • 它根本不工作。而且日志总是说“PARENT TOUCH”而不是“CHILD TOUCH”,所以我想我不能触摸子滚动视图。
    【解决方案6】:

    你不能在 Scrollview OS 中使用两个 Scrollview 会混淆哪个必须滚动,所以你的活动挂起所以不要这样使用

    【讨论】:

      【解决方案7】:

      我几乎可以肯定这是不可能的:

      android如何处理触摸事件:

      父母获得事件并决定是否必须处理它或者是否应该让孩子处理它;

      所以,如果滚动视图处理触摸,处理它的总是父视图

      http://developer.android.com/training/gestures/viewgroup.html

      我个人不知道是否有办法了解父滚动视图后面触摸的内容,以查看它是否是另一个滚动视图并让它处理移动

      【讨论】:

        【解决方案8】:

        这是一个可能的解决方案,它适用于 ScrollView 和 ScrollView 中的 ListView。 ScrollView Inside ScrollView

        【讨论】:

          【解决方案9】:

          将此自定义滚动视图用作内部滚动视图:

          public class TouchMeScrollView extends ScrollView {
          public TouchMeScrollView(Context context) {
              super(context);
          }
          
          public TouchMeScrollView(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
          
          public TouchMeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
          }
          
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          public TouchMeScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
              super(context, attrs, defStyleAttr, defStyleRes);
          }
          
          @Override
          public boolean onTouchEvent(MotionEvent ev) {
              requestDisallowInterceptTouchEvent(true);
              return super.onTouchEvent(ev);
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多