2015-02-10 29 views
3

我有一個要求,我在一個應用程序中有超過1個水平滾動(圖像紅色,綠色和藍色)。是否有可能通過新的Recycler視圖實現這一點,或者我只需簡單地使用水平滾動視圖。我試圖在同一個XML中實現兩個回收器視圖,但只有一個出現,另一個沒有。如果你能把我和一些教程聯繫起來,那將是非常好的。我可以在一個活動中使用2個以上的回收站視圖android

XML佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:scrollbars="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/second_recycler_view" 
     android:scrollbars="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/recycler_view" 
     /> 


</RelativeLayout> 

MainActivity類別

public class MainActivity extends ActionBarActivity { 

    private RecyclerView recyclerView,secondRecyclerView; //myRecyclerView was its name 
    private RecyclerView.Adapter adapter,adapter2; 
    private RecyclerView.LayoutManager layoutManager,layoutManager2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     secondRecyclerView = (RecyclerView) findViewById(R.id.second_recycler_view); 

     layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false); 
     layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setHasFixedSize(true); 
     secondRecyclerView.setLayoutManager(layoutManager2); 
     secondRecyclerView.setHasFixedSize(true); 

     // DaTA to be populated in the app 
     // same data is used 
     Constants.demoDatas = new ArrayList<DemoData>(); 
     for (int i = 0;i < Constants.backgroundImages.length ;i++){ 

      Constants.demoDatas.add(new DemoData(Constants.backgroundImages[i])); 
     } 

     adapter = new RecyclerAdapter(Constants.demoDatas,this); 
     adapter2 = new SecondRecyclerAdapter(Constants.demoDatas,this); 
     recyclerView.setAdapter(adapter); 
     secondRecyclerView.setAdapter(adapter2); 

    } 

question

+0

請,包括爲了一些代碼,以幫助更好地理解這個問題 – 2015-02-10 11:56:48

+0

顯示'XML'文件也許你忘了'某處wrap_content' – hrskrs 2015-02-10 11:58:04

+0

什麼有關行佈局? – 2015-02-10 11:59:35

回答

7

這是一個reported bug。不知何故,wrap_content它不適用於RecyclerView。您可以實現自定義LayoutManager或以編程方式設置高度。我一直在使用這最後一個選項,但我不太喜歡它。希望它很快就會解決。

更新:另外,this library解決了爲您實現自定義LayoutManager的問題。

更新2:這是現在採用Android支持庫23.2固定及以上,你可以閱讀here

+0

但其他應用程序有不止一個scolling意見,所以他們在Horizo​​ntalScollView的幫助下做? 供參考app:FLIPKART應用主屏幕有5個水平滾動,所以他們使用水平滾動視圖? – 2015-02-10 12:10:41

+1

您可以使用多個RecyclerView,但由於wrap_content不起作用,這就像您使用的是match_parent,不會爲其他RecyclerView留出空間。如果你將每個RecyclerView的高度固定爲200dp,你應該看到所有這些。 – 2015-02-10 12:14:33

+0

修復爲我工作的高度。所以這是一個錯誤,這是我可以繪製的推論 – 2015-02-10 12:17:52

0

也許這對別人有幫助。我已將2個回收器視圖放在一個下方,結果良好。它顯示了兩個回收商的整個屏幕尺寸的圖片。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:id="@+id/row1" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/images1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="horizontal" 
    android:scrollbarStyle="outsideInset" 
    android:layout_weight="1" 
    android:layout_gravity="center" /> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/images2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="horizontal" 
    android:scrollbarStyle="outsideInset" 
    android:layout_weight="1" 
    android:layout_marginTop="-30dp" 
    android:layout_gravity="center"/> 
相關問題