RecyclerView 中的 ScrollView 不会滚动

Nic*_*Ory 1 android android-layout

我想让一个LinearLayout(我正在动态添加项目)在 scrollable 内滚动RecyclerView。在启动时,我最初可以看到一个小滚动条(表明它是一个可滚动视图,在几秒钟内消失)。尝试了 appCompat NestedScrollView 以及 enable nestedScrollingEnabled,但没有成功。我怎样才能在LinearLayout里面滚动RecyclerView

<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>
Run Code Online (Sandbox Code Playgroud)

RecyclerView 代码

<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"/>
Run Code Online (Sandbox Code Playgroud)

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

raf*_*007 5

尝试在触摸时禁用回收器视图的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;
                }
            }); 
Run Code Online (Sandbox Code Playgroud)


Nic*_*Ory 5

我通过在 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;
                    }
                }); 
Run Code Online (Sandbox Code Playgroud)

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

  • 您应该标记正确答案并为对您有帮助的答案点赞……这就是 stackoverflow 社区的用途……谢谢 (4认同)