【问题标题】:Two recycleviews in one activity scroll problem一个活动滚动问题中的两个recycleviews
【发布时间】:2019-03-15 12:08:35
【问题描述】:

我在一项活动中创建了 2 个回收视图。一个水平滚动,而另一个垂直滚动。我可以在每个 RecyclerView 内正确滚动,但整个页面不会滚动,即顶部 RecyclerView 始终保持在顶部,底部保持在底部,就像两者都固定在位置一样。

<ScrollView 
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/trending_recipes"
        android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontaltrendingrecycleview"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginStart="15dp"
        android:orientation="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:text="@string/all_recipes"
        android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</ScrollView>

我阅读了这篇文章“Scolling with multiple RecyclerViews in Layout”并以编程方式设置了垂直回收视图的高度。像这样

LinearLayout.LayoutParams params = new     
 LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
 ViewGroup.LayoutParams.WRAP_CONTENT);
// calculate height of RecyclerView based on child count
params.height=1150;
// set height of RecyclerView
recyclerView.setLayoutParams(params);

但问题是如何根据子数计算 RecyclerView 的高度?

【问题讨论】:

标签: android android-recyclerview scrollview android-scrollview android-nestedscrollview


【解决方案1】:

使用NestedScrollView 而不是ScrollView 并在Fragment 中的Activity 中使用以下行。

ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);

这将适用于您的所有 Android API 级别。

【讨论】:

    【解决方案2】:

    ScrollView 替换为NestedScrollView

    然后添加:horizontaltrendingrecycleview.isNestedScrollingEnabled = false

    您的 XML 应如下所示:

    <android.support.v4.widget.NestedScrollView 
      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"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fillViewport="true"
      tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">
    
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="10dp"
          android:layout_marginStart="20dp"
          android:layout_marginTop="20dp"
          android:text="@string/trending_recipes"
          android:textSize="18sp" />
    
        <android.support.v7.widget.RecyclerView
          android:id="@+id/horizontaltrendingrecycleview"
          android:layout_width="match_parent"
          android:layout_height="240dp"
          android:layout_marginStart="15dp"
          android:orientation="horizontal"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="20dp"
          android:text="@string/all_recipes"
          android:textSize="18sp" />
    
        <android.support.v7.widget.RecyclerView
          android:id="@+id/recycleView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="20dp"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
      </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多