我可以在一个中使用 2 个以上的回收站视图 activity android

Can i use more than 2 recycler views in one activity android

我有一个要求,我在一个应用程序中有超过 1 个水平滚动(在图像红色、绿色和蓝色中)。是否可以通过新的 Recycler 视图实现此目的,或者我必须简单地使用水平滚动视图。我试图在同一个 xml 中实现两个回收器视图,一个在另一个下面,但只有一个出现,另一个没有出现。如果你能 link 我学习一些教程就太好了。

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>

MainActivityClass

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);

    }

这是一个reported bug。不知何故,wrap_content 它不适用于 RecyclerView。您可以实现自定义 LayoutManager 或以编程方式设置高度。我一直在使用最后一个选项,但我不太喜欢它。希望它能尽快修复。

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

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

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

也许它会对某人有所帮助。我已将 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"/>