2

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

Tim
  • 63
  • 1
  • 6
  • It is bad practice to have scroll-in-scroll views - since the behaviour can be strange. Do you really need that scroll view? Anyway, show some of your code, to see what you have tried already. – yennsarah Nov 11 '15 at 07:00
  • @Amy Yes I understand that it is not a good practice. I can't help it as I have data that needs to display in lists, hence I have to use RecyclerViews. I mentioned ScrollView because the data will not be able to be displayed completely in a single screen view. Or, do you have any other suggestions on how should I solve this? Thanks! – Tim Nov 11 '15 at 08:02
  • Why can't you use one RecyclerView with custom rows or a Gridlayout? Is it really necessary to have two fragments? – yennsarah Nov 11 '15 at 08:17
  • @Amy I'm afraid so as both RecyclerView displays different data and each uses different layout manager (first one is Linear, second one is Grid). – Tim Nov 11 '15 at 13:16
  • Ok, then your setup should be ok. But you want it to scroll simultaneously? It would be better to have 2 interfaces in each fragment, telling the other RecyclerView to scroll to a position or by value. Would this match your requirements? – yennsarah Nov 11 '15 at 13:19
  • Post your code and I'll try my best to show you a implementation of my idea. – yennsarah Nov 11 '15 at 13:25
  • @Amy Currently I'm using FrameLayouts to display the fragments, but I can't seem to make the RecyclerViews to not scroll individually. I need the RVs to not appear sticky, so that when user scrolls the screen, the entire screen scrolls. I'm apologize if this is confusing to you, I'm still kinda new. I have added my code, thank you. – Tim Nov 11 '15 at 13:28

1 Answers1

2

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();
Rahul Chaurasia
  • 1,601
  • 2
  • 18
  • 37
  • I'm sorry but this does not actually solve my problem entirely. The fragments do display using FrameLayouts but the RecyclerView in each of the fragment scrolls separately. I want to make the entire screen scrolls as a page :( – Tim Nov 11 '15 at 13:14