【问题标题】:ScrollView inside RecyclerView won't scrollRecyclerView 中的 ScrollView 不会滚动
【发布时间】:2018-01-27 14:58:18
【问题描述】:

我想在可滚动的RecyclerView 中创建一个可滚动的LinearLayout(我正在向其中动态添加项目)。在启动时,我最初可以看到一个小滚动条(表明它是一个可滚动的视图,在几秒钟内消失)。尝试了 appCompat NestedScrollView 以及启用nestedScrollingEnabled,但没有成功。如何使LinearLayoutRecyclerView 内滚动?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
  <ScrollView
      android:layout_width="wrap_content"
      android:layout_height="300dp"
      android:fillViewport="true"
      android:nestedScrollingEnabled="true">
      <LinearLayout
          android:id="@+id/playerlist_linear"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="left|center_vertical"
          android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="5dp"
          android:layout_marginBottom="7dp"
          android:orientation="vertical">
      </LinearLayout>
  </ScrollView>

</FrameLayout>

回收站查看代码

<android.support.v7.widget.RecyclerView
    android:id="@+id/messagesListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:nestedScrollingEnabled="false"/>

更新:我发现滚动确实有效,但前提是我向鼠标发送垃圾邮件或在滚动视图内快速点击。只要按住鼠标,它就会变得专注并允许滚动。真奇怪。

【问题讨论】:

  • 添加你的 RecylerView 布局代码...
  • 添加了 RecyclerView 代码。

标签: android android-layout


【解决方案1】:

尝试在scrollview 被触摸时禁用回收器视图的触摸。

ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView1);
scrollView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                     // Disallow the touch request for parent scroll on touch of child view
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    return true;
                }
            }); 

【讨论】:

  • 它可以防止 RecyclerView 在 ScrollView 被触摸时滚动。但是ScrollView 本身仍然不会滚动。
  • 我猜你必须从onTouch返回false
  • 也许吧。这可以解释为什么我必须这样做view.onTouch(motionEvent)
【解决方案2】:

我通过在 rafsanahmad007 的解决方案中添加一行来解决它:

    ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView1);
    scrollView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                         // Disallow the touch request for parent scroll on touch of child view

                   view.getParent().requestDisallowInterceptTouchEvent(false);
                   view.onTouch(motionEvent);
                        return true;
                    }
                }); 

我不确定在 Java 上如何调用 View.onTouch,因为我正在使用 Xamarin.Android (C#) 进行开发。应该很相似。

【讨论】:

  • 您应该标记正确答案并投票赞成对您有帮助的答案...这就是 stackoverflow 社区的用途...谢谢
【解决方案3】:

我终于找到了另一种在 RecyclerView viewHolder 中滚动 ScrollView 的方法。我最初发布了我的答案here,所以你可以查看我的答案,了解如何在 RecyclerView 中启用 ScrollView 的滚动。希望这对查找另一个答案的人有所帮助。

但我也想从 OP 的问题中回答为什么 ScrollView 上有一个小滚动条。您可以通过在 xml 布局中的 ScrollView 标记中添加 android:scrollbars="none" 来隐藏滚动条,也可以删除 android:nestedScrollingEnabled="true",因为在这种情况下它没有用。

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:fillViewport="true"
    android:scrollbars="none">
    <LinearLayout
        android:id="@+id/playerlist_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="7dp"
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>

【讨论】:

    猜你喜欢
    • 2016-01-13
    • 2020-09-07
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多