【问题标题】:Can i use more than 2 recycler views in one activity android我可以在一项活动中使用超过 2 个回收站视图吗?
【发布时间】:2015-04-10 10:44:16
【问题描述】:

我有一个要求,即我在应用程序中有超过 1 个水平滚动条(在红色、绿色和蓝色图像中)。是否可以通过新的 Recycler 视图来实现这一点,或者我必须简单地使用水平滚动视图。我试图在同一个 xml 中实现两个回收器视图,一个在另一个之下,但只有一个出现,另一个没有。如果您能将我链接到一些教程,那就太好了。

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

    }

【问题讨论】:

  • 请包含一些代码以帮助更好地理解问题
  • 显示XML文件也许你忘了wrap_content某处
  • 行布局怎么样?
  • @Williams 它是一个相对布局。我应该使用表格布局吗??
  • 你也能发一下吗

标签: android horizontal-scrolling android-recyclerview


【解决方案1】:

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

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

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

【讨论】:

  • 但是其他应用程序有不止一个 scolling 视图,所以他们是在 Horizo​​ntalScollView 的帮助下做的吗???供参考应用:FLIPKART 应用主屏幕有 5 个水平滚动,所以他们使用水平滚动视图吗?
  • 你可以使用多个 RecyclerView,但是由于 wrap_content 不起作用,就像你在使用 match_parent,没有为其他 RecyclerView 留下空间。如果您将每个 RecyclerView 的高度固定为 200dp,您应该可以看到所有这些。
  • 固定高度对我有用。所以这是我可以得出的推断的错误
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多