0

enter image description here 我想將兩個回收站視圖添加到一個佈局中,該佈局位於Fragment。但我能看到的是第一個RecyclerView。 是否有可能像上圖中那樣添加兩個回收站視圖(兩者都需要同時可見),還是在Android中存在某種限制,只有一個回收站視圖可以添加到一個完整屏幕?同時在一個片段中同時顯示多個回收站視圖

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/test_recycle1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:divider="#FFCC00" 
     android:dividerHeight="4px" 
     android:background="#e0e0e0"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/test_recycle2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:divider="#FFCC00" 
     android:dividerHeight="4px" 
     android:background="#e0e0e0"/> 
     </LinearLayout> 

</LinearLayout> 
+0

顯示您的XML。這可以通過LinearLayout和權重來完成。 – yennsarah

+0

添加了版面 – user3477811

回答

0

避免使用過多的佈局容器,如Linera稱佈局在一個XML文件,透支最終將導致內存問題。 您的問題可以通過使用一個LinearLayout並通過設置您的RecyclerViews0dp的高度和layout_weight爲 「1」 來解決:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/test_recycle1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:divider="#FFCC00" 
     android:dividerHeight="4px" 
     android:background="#e0e0e0"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/test_recycle2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:divider="#FFCC00" 
     android:dividerHeight="4px" 
     android:background="#e0e0e0"/> 
</LinearLayout> 
+0

我會試試這個,回覆你 – user3477811

+0

它不能正常工作:( 當我在一個版面上使用了兩個回收器視圖時,第二個回收器視圖根本沒有加載,只有一個空格 – user3477811

+0

你有沒有在你的第二個RecyclerView中的任何數據?你有沒有添加任何東西到你的佈局旁邊的兩個RecyclerViews? – yennsarah

0

設置的LinearLayout重量

 <android.support.v7.widget.RecyclerView 
      android:id="@+id/test_recycle1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="#FFCC00" 
      android:dividerHeight="4px" 
      android:layout_weight="1" 
      android:background="#e0e0e0"/><!----> 



     <android.support.v7.widget.RecyclerView 
      android:id="@+id/test_recycle2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="#FFCC00" 
      android:layout_marginTop="20dp" 
      android:dividerHeight="4px" 
      android:layout_weight="1" 
      android:background="#e0e0e0"/> 


</LinearLayout> 
0

使用機器人:weightSum解決你的問題:

檢查下面的佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="2"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/test_recycle1" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:background="#e0e0e0" 
      android:divider="#FFCC00" 
      android:dividerHeight="4px" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/test_recycle2" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:background="#e0e0e0" 
      android:divider="#FFCC00" 
      android:dividerHeight="4px" /> 
    </LinearLayout> 

</LinearLayout> 

看一看http://developer.android.com/guide/topics/ui/layout/linear.html

+0

問題是否解決了?@ user3477811 –

+0

nope問題沒有解決,當我在一個佈局上使用兩個回收器視圖時,第二回收ler視圖根本沒有加載。只有一個空白 – user3477811

相關問題