-1

I want to load more data to show recycle view after 10 data loads. example below. I'm tired to find it. I can't understand the solution. I want an easy solution.

Example: enter image description here

CommentPost.java

 public void showData(){
    //view Comment
    progressDoalog = new ProgressDialog(CommentPost.this);
    progressDoalog.setMessage("Loading....");
    progressDoalog.show();
    /*Create handle for the RetrofitInstance interface*/
    Call<List<Comments>> call = service.getComment(audioPath);
    call.enqueue(new Callback<List<Comments>>() {
        @Override
        public void onResponse(Call<List<Comments>> call, Response<List<Comments>> response) {
            progressDoalog.dismiss();
            generateDataList(response.body());
        }
        @Override
        public void onFailure(Call<List<Comments>> call, Throwable t) {
            progressDoalog.dismiss();
            Toast.makeText(CommentPost.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}
/*Method to generate List of data using RecyclerView with custom adapter*/
private void generateDataList(List<Comments> CommentsList) {
    recyclerView = findViewById(R.id.recyclerView);
    adapter = new CommentAdapter(this,CommentsList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(CommentPost.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
    //latest message
    recyclerView.scrollToPosition(adapter.getItemCount() - 1);
}

Adapter:

@SuppressLint("RecyclerView")
@Override
public void onBindViewHolder(CommentAdapter.CustomViewHolder holder, int position) {
    holder.txtTitle.setText(dataList.get(position).getComment());
    holder.txttime.setText(dataList.get(position).getUpdated_at());
    holder.userNametitle.setText(dataList.get(position).getuser_name());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Toast.makeText(view.getContext(), "" + dataList.get(position).getId(), Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public int getItemCount() {
    return dataList.size();
}
Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

1

First thing the function generateDataList must call first time only So I'm going to call it from the onCreat

@Override
protected void onCreate(Bundle savedInstanceState){
     ...
     generateDataList();
}
private void generateDataList() {
    recyclerView = findViewById(R.id.recyclerView);
    adapter = new CommentAdapter(this,new ArrayList<>());
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(CommentPost.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

You can create your layout like this:

<androidx.core.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nestedSV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
          
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:nestedScrollingEnabled="false"
            tools:listitem="@layout/user_rv_item" />
          
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
      
</androidx.core.widget.NestedScrollView>

And we going to set the scroll listener for NestedScrollView

@Override
protected void onCreate(Bundle savedInstanceState){
    ...
    progressBar = findViewById(R.id.progressBar);
    nestedSV = findViewById(R.id.nestedSV);
    nestedSV.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            // When the Scroll of NestedScrollView in bottom
            if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
                // show the progressbar and then get more data to the recyclerview
                progressBar.setVisibility(View.VISIBLE);
                showData();
            }
        }
    });
    generateDataList();
}

And We going to change showData():

public void showData(){
    Call<List<Comments>> call = service.getComment(audioPath);
    call.enqueue(new Callback<List<Comments>>() {
        @Override
        public void onResponse(Call<List<Comments>> call, Response<List<Comments>> response) {
            // here instead of generateDataList we going to add the data with addToList
            addToList(response.body());
            progressBar.setVisibility(View.GONE);
        }
        @Override
        public void onFailure(Call<List<Comments>> call, Throwable t) {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(CommentPost.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}


private void addToList(List<Comments> commentsList) {
     // this is add commentsList data to the adapter list
     adapter. dataList.addAll(commentsList);
     adapter.notifyDataSetChanged();
}

Aiman Alyosofi
  • 625
  • 1
  • 13
0

Google provides a library for that purpose which is paging library, you can know how it works here from google documentation, or you can follow this video where you will learn how to use paging library to achieve the result that you want.

Mohamed Rejeb
  • 2,281
  • 1
  • 7
  • 16