0

I am trying to add pagination to my code. As the first step, I am trying to fetch only 3 records for page = 1. When scrolling the page, I am trying to increase the page number using onScroll() method. But when I am scrolling page count is not increased. Why?

This is the code how I call onScroll method inside the onCreate method of my activity.

recyclerView = findViewById(R.id.taskList);
        loadingPB = findViewById(R.id.idPBLoading);
        nestedSV = findViewById(R.id.idNestedSV);
        page = 1;


        nestedSV.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                // on scroll change we are checking when users scroll as bottom.
                if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
                    // in this method we are incrementing page number,
                    // making progress bar visible and calling get data method.
                    page++;
                    loadingPB.setVisibility(View.VISIBLE);
                }
            }
        });

This is how I fetch data from database.

dailyTaskViewModel.getAllTaskTemplatesByStatus(status).observe(this, new Observer<List<TaskTemplateWithTasks>>() {
                @Override
                public void onChanged(List<TaskTemplateWithTasks> templateWithTasks) {
                    taskCount = templateWithTasks.size();
                    if(templateWithTasks.size() == 0){
                        messageText.setVisibility(View.VISIBLE);
                    }
                    else{
                        messageText.setVisibility(View.GONE);
                    }
                    if (page == 1 && templateWithTasks.size() > 3) {
                        List<TaskTemplateWithTasks> limitedList = templateWithTasks.subList(0, 3); // Get the first 3 elements
                        dailyTaskListAdapter.setTaskTemplates(new ArrayList<>(limitedList));
                    } else {
                        dailyTaskListAdapter.setTaskTemplates(templateWithTasks);
                    }
                    dailyTaskListAdapter.notifyDataSetChanged();
                    EditText search = findViewById(R.id.search);
                    search.setText("");
                    startAdapterFilter(searchText);

                }
            });

This is activity.xml code.

<androidx.core.widget.NestedScrollView
            android:id="@+id/idNestedSV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".view.activity.dailyTask.DailyTaskListActivity">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/single_padding"
                android:background="@drawable/outline"
                android:orientation="vertical"
                android:padding="@dimen/content_padding">


                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical">


                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/taskList"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        app:layoutManager="LinearLayoutManager"
                        tools:context=".view.activity.dailyTask.DailyTaskListActivity"
                        tools:listitem="@layout/task_item" />


                    <TextView
                        android:id="@+id/message_text"
                        android:layout_marginTop="30dp"
                        android:layout_width="match_parent"
                        android:gravity="center"
                        android:textSize="20sp"
                        android:visibility="gone"
                        android:text="@string/can_t_find_records"
                        android:layout_height="wrap_content"/>

                    <ProgressBar
                        android:id="@+id/idPBLoading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </RelativeLayout>



            </LinearLayout>

        </androidx.core.widget.NestedScrollView>

1 Answer 1

0

I think you made a mistake. You add scroll listener to NestedScrollView, it should be add with RecyclerView.

Try this

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (isLastItemDisplaying(recyclerView)) {
                //Calling the method getdata again
                page++;
                loadingPB.setVisibility(View.VISIBLE);
            }
        }
    });

You can check this answare

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.