2015-02-10 58 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"/> 
相关问题