Tim
Tim

Reputation: 63

Android: How to display two fragments with RecyclerView in one screen

I have two fragments, each with its own respective RecyclerView list. The RecyclerView from each of the fragments may contain many items. So, scrolling will mostly be necessary. My problem is, how do I combine and display these two fragments into one screen using ScrollView, but at the same time, making the scrolling of the RecyclerViews behave as normally (non-sticky, follows the entire screen's scroll).

Thanks.

EDIT: My layout file

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

Screenshot: (Notice the RecyclerView at the top and bottom scrolls individually) https://drive.google.com/file/d/0B-glHmJbVcwiVU41WVRHU3g2bkE/view

Upvotes: 2

Views: 1093

Answers (1)

Rahul Chaurasia
Rahul Chaurasia

Reputation: 1641

You can do like this one -

In your activity's layout file, you should have two framelayout for containing your two 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>

Then in your Activity's onCreate() method, you can use FragmentManager class to add Fragment to the FrameLayout.

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

Upvotes: 2

Related Questions