Android:如何使用RecyclerView在一个屏幕上显示两个 fragment

标签 android android-fragments scrollview android-recyclerview

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

谢谢。

编辑: 我的布局文件

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

最佳答案

你可以这样做 -

在您的 Activity 的布局文件中,您应该有两个框架布局来包含两个 fragment 。

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

关于Android:如何使用RecyclerView在一个屏幕上显示两个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645429/

相关文章:

java - 广告不可见。不刷新广告。阿德莫布

android - 无法从总数中减去 SharedPreference 编号

android - 在 fragment 中隐藏操作栏抛出 NullPointerException?

android - 我们如何在 ScrollView 中设置滚动页眉和页脚?

Android 如何添加ScrollView? ScrollView 乱七八糟的布局

Android ExoPlayer 更改选定的轨道

java - 如何将图像设置为壁纸,这些图像将一张一张地出现或像 Android 中的幻灯片一样?

java - TabLayout 在 TabLayout 内请求权限后不更新用户位置

javascript - 带有固定标题的scrollIntoView()

Java:由子字符串生成的字符串不被视为硬编码字符串