【问题标题】:Android: How to display two fragments with RecyclerView in one screenAndroid:如何在一个屏幕上显示两个片段与 RecyclerView
【发布时间】:2015-11-11 06:38:33
【问题描述】:

我有两个片段,每个片段都有自己的 RecyclerView 列表。 每个片段的 RecyclerView 可能包含许多项目。因此,滚动将主要是必要的。 我的问题是,如何使用 ScrollView 将这两个片段组合并显示到一个屏幕中,但同时,使 RecyclerViews 的滚动行为正常(非粘性,跟随整个屏幕的滚动)。

谢谢。

编辑: 我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.easyuni.courserecommender.ResultsActivity">

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

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

                <FrameLayout
                    android:id="@+id/fragment_container_results_career"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <View
                    android:id="@+id/divider"
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_below="@+id/fragment_container_results_career"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:background="@color/divider" />

                <FrameLayout
                    android:id="@+id/fragment_container_results_courses"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

            </LinearLayout>

        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:itemIconTint="#333"
            app:itemTextColor="#333" />

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

截图: (注意顶部和底部的 RecyclerView 分别滚动) https://drive.google.com/file/d/0B-glHmJbVcwiVU41WVRHU3g2bkE/view

【问题讨论】:

  • 滚动视图是不好的做法——因为这种行为可能很奇怪。你真的需要那个滚动视图吗?不管怎样,展示你的一些代码,看看你已经尝试过什么。
  • @Amy 是的,我知道这不是一个好习惯。我无能为力,因为我有需要在列表中显示的数据,因此我必须使用 RecyclerViews。我提到 ScrollView 是因为数据将无法在单个屏幕视图中完全显示。或者,您对我应该如何解决这个问题有任何其他建议吗?谢谢!
  • 为什么不能使用带有自定义行或 Gridlayout 的 RecyclerView?真的有必要有两个片段吗?
  • @Amy 恐怕 RecyclerView 都显示不同的数据并且每个都使用不同的布局管理器(第一个是线性的,第二个是网格)。
  • 好的,那么您的设置应该没问题。但是你想让它同时滚动吗?最好在每个 Fragment 中有 2 个接口,告诉另一个 RecyclerView 滚动到某个位置或按值滚动。这符合您的要求吗?

标签: android android-fragments scrollview android-recyclerview


【解决方案1】:

你可以这样做 -

在你的活动布局文件中,你应该有两个框架布局来包含你的两个片段。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/first_fragment_container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/second_fragment_container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </FrameLayout>
</LinearLayout>

然后在你的Activity的onCreate()方法中,你可以使用FragmentManager类将Fragment添加到FrameLayout中。

 getSupportFragmentManager().beginTransaction().add(R.id.first_fragment_container, new YourFirstFragment()).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.second_fragment_container, new YourSecondFragment()).commit();

【讨论】:

  • 很抱歉,但这实际上并不能完全解决我的问题。片段确实使用 FrameLayouts 显示,但每个片段中的 RecyclerView 单独滚动。我想让整个屏幕滚动为一个页面:(
猜你喜欢
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
相关资源
最近更新 更多