Home > database >  recyclerView load more data which shortest and easy ways? android studio
recyclerView load more data which shortest and easy ways? android studio

Time:06-18

I'm new java android dev. I have faced a problem with my project. 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();
}

CodePudding user response:

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.

CodePudding user response:

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) {
            if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
                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) {
            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) {
     adapter.getList().addAll(commentsList);
     adapter.notifyDataSetChanged();
     // Or any way you want to add the data to the list
}

  • Related