我可以在一个活动android中使用2个以上的回收站视图吗?

时间:2015-02-10 11:54:59

标签: android horizontal-scrolling android-recyclerview

我有一个要求,我在应用程序中有超过1个水平卷轴(图像为红色,绿色和蓝色)。是否可以通过新的Recycler视图实现这一点,或者我只需使用水平滚动视图。我试图在同一个xml中一个在另一个下面实现两个回收器视图,但只有一个出现,而另一个没有。如果你能把我与一些教程联系起来会很棒。

XML LAYOUT

<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 Class

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

3 个答案:

答案 0 :(得分:7)

它是reported bug。不知何故,wrap_content它不能与RecyclerView一起使用。您可以实现自定义LayoutManager或以编程方式设置高度。我一直在使用这个最后一个选项,但我真的不喜欢它。我们希望很快就能解决这个问题。

更新:此外,this library解决了为您实现自定义LayoutManager的问题。

更新2:现在使用Android支持库23.2及更高版本修复此问题,因为您可以阅读here

答案 1 :(得分:1)

我找到了一个允许wrap_content与RecyclerViews一起使用的库。 Check it out!

答案 2 :(得分: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"/>