一个布局中的两个RecyclerView无法正常工作,因为Android中缺少一个

时间:2016-05-31 20:23:36

标签: android android-recyclerview

我正在开发一款Android应用。在我的应用程序中,我想在一个布局中使用两个RecyclerViews。由于Stack Overflow问题(Two RecyclerViews under each other in one layout),我使用LinearLayout来包装RecyclerViews。正如您所看到的答案所示,要使用LinearLayout并将RecyclerViews height设置为wrap_content。我跟着它。但是,当我只运行一个RecyclerView时,会出现一个丢失。

这是截图:

enter image description here

如您所见,仅在RecyclerView上出现。

这是我的XML布局:

@Override
public void onCreate() {
    super.onCreate();
    if (!FirebaseApp.getApps(this).isEmpty()) {
      FacebookSdk.sdkInitialize(getApplicationContext());
    }
}

我也试过这个。屏幕上没有任何内容:

<LinearLayout
        android:orientation="vertical"
         app:layout_behavior="@string/appbar_scrolling_view_behavior"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
         <android.support.v7.widget.RecyclerView
             android:scrollbars="vertical"
             android:id="@+id/ai_rc_reviews"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
        <android.support.v7.widget.RecyclerView
            android:scrollbars="vertical"
            android:id="@+id/ai_rc_reviews_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
     </LinearLayout>

如何修复我的代码以在单一布局中使用两个RecyclerViews。有没有更好的方法呢?

5 个答案:

答案 0 :(得分:2)

尝试使用layout_weight属性而不是wrap_content。给两个RecyclerView相同的权重,并将高度更改为0dp。

<LinearLayout
     android:orientation="vertical"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <android.support.v7.widget.RecyclerView
         android:scrollbars="vertical"
         android:id="@+id/ai_rc_reviews"
         android:layout_width="match_parent"
         android:layout_weight="1"
         android:layout_height="0dp">
    </android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.RecyclerView
        android:scrollbars="vertical"
        android:id="@+id/ai_rc_reviews_2"
        android:layout_width="match_parent"
         android:layout_weight="1"
        android:layout_height="0dp"> 
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

答案 1 :(得分:0)

一般来说,如果两个滚动容器沿同一轴滚动,那么将它们放在另一个滚动容器中并不是一个好主意。这种行为很可能会引起用户的混淆。相反,如果您100%确定需要垂直堆叠两个滚动容器 - 如果不是100%必要,请尽量不要这样做,您应该为每个容器使用固定高度,因为它会对所创建的整体性能产生积极影响布局。

答案 2 :(得分:0)

    

    <!-- Usual Size -->
    <LinearLayout
        android:id="@+id/usualSize_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical">
        <TextView
            android:id="@+id/usualSize_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="size"
            android:textSize="@dimen/font_15"
            android:textColor="@color/color_999999"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/usualSize_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height"/>
    </LinearLayout>

    <!-- MarkImage  -->
    <LinearLayout
        android:id="@+id/mark_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical">
        <TextView
            android:id="@+id/mark_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mark"
            android:textColor="@color/color_999999"
            android:textSize="@dimen/font_15"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"/>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/mark_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height"/>
    </LinearLayout>
</LinearLayout>

这里我们应该考虑WRAP_CONTENY两次,所以我们应该在文件中:build.gradle。

请在gradle文件中更新库的版本:

编译com.android.support:recyclerview-v7:23.2.1

及更高版本。请检查你的版本。

How do I make WRAP_CONTENT work on a RecyclerView

答案 3 :(得分:0)

<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"
  android:scrollbars="none"
 app:layout_behavior="@string/appbar_scrolling_view_behavior">
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <!-- Usual Size -->
    <LinearLayout
        android:id="@+id/usualSize_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/usualSize_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="size"
            android:textSize="@dimen/font_15"
            android:textColor="@color/color_999999"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"
            />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/usualSize_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height"

            />
    </LinearLayout>

    <!-- MarkImage  -->
    <LinearLayout
        android:id="@+id/mark_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/mark_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mark"
            android:textColor="@color/color_999999"
            android:textSize="@dimen/font_15"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"
            />`
       <android.support.v7.widget.RecyclerView
            android:id="@+id/mark_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height" />
        </LinearLayout>
      </LinearLayout>
    </ScrollView>

这里我们应该三思而后行WRAP_CONTENY,所以我们应该在文件中:build.gradle。

请在gradle文件中更新库的版本:

compile&#39; com.android.support:recyclerview-v7:23.2.1&#39;

和更高版本。请检查您的版本。

How do I make WRAP_CONTENT work on a RecyclerView

答案 4 :(得分:0)

万一有人在一个Srcollview中有两个RecyclerViews和其他视图, 如果你有问题,第一个Recyclerview不会向上滚动或第二个部分丢失,

如果你使用LinearLayout来包含这些RecyclerViews,请尝试使用relativeLayout,这解决了我的问题。