0

My HomeBuyersActivity consists of multiple recyclerviews (ex. popular stores, popular products, discover products, discover store and etc.) which are aligned vertically but their item layouts are aligned horizontally. So when I open the activity, the process of retrieving the data is too long because it was loading all the files at the same time. Here is the sample code of my XML file:

XML: (Note: all of the included layouts consists of recyclerviews)

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/a ndroid"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/defaultBackground"
    tools:context=".Activity.HomeBuyersActivity">
       <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <include
             layout="@layout/popular_store_layout"
             android:visibility="visible" />
            <include
             layout="@layout/popular_products_layout"
             android:visibility="visible" />
            <include
             layout="@layout/discover_store_layout"
             android:visibility="visible" />
            <include
             layout="@layout/discover_products_layout"
             android:visibility="visible" />

           //.... and more recyclerviews

       </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

And in HomeBuyersActivity I am populating those recyclerviews with the data that are retrieved from firestore. The only solution that I came out with is to load only the layout that is/are visible to the screen but I don't know how to do it. Is there a way to achieve that process? If not, Is there any better solution or other way around?

Mr. Baks
  • 237
  • 4
  • 18

1 Answers1

0

You said it takes a lot time to load data .To fix that one thing you can do is to add pagination to your query rather than messing with recyclerview's core functionalities.
Read more at : https://firebase.google.com/docs/firestore/query-data/query-cursors

Query firstPage = cities.orderBy("population").limit(25);

limit is what you need and you can always load more data when recyclerview scroll reaches to the end.

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (dy > 0) //check for scroll down
        {
            visibleItemCount = mLayoutManager.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();

            if (loading) {
                if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                    loading = false;
                    if (adapter.countOfShowing < adapter.allChallenges.size()) {
                        Log.e("...", "Last Item Wow !");
                        adapter.increaseCountOfShowing();
                        adapter.notifyDataSetChanged();
                    }
                    loading = true;
                    //Do pagination.. i.e. fetch new data
                }
            }
        }
    }
});

ref: https://stackoverflow.com/a/45909144/3995126

kelvin
  • 1,344
  • 1
  • 12
  • 25
  • Yup! I already included pagination. Sorry I didn't include my source code for my class because it consists of more than 800 lines. BTW thank you for your answer! – Mr. Baks Sep 17 '20 at 08:47