一个活动滚动问题中的两个回收视图

问题描述 投票:0回答:2

我在一个活动中创建了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”并以编程方式设置了vertical recycleview的高度。像这样

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
2个回答
1
投票

使用NestedScrollView而不是ScrollView,并在FragmentActivity中使用以下线。

ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);

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


1
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.