RecyClerview加载更多最短和简单的方法?

发布于 2025-02-08 22:35:17 字数 2408 浏览 2 评论 0 原文

我想加载更多数据以显示10个数据加载后的回收视图。下面的示例。我很累。我无法理解解决方案。我想要一个简单的解决方案。

示例:

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);
}

适配器:

@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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2025-02-15 22:35:19

Google提供了一个用于该目的的库,即 paging 库,您可以知道它的工作原理概述“ rel =” nofollow noreferrer”>在此处从Google文档中,您可以关注此

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.

萌化 2025-02-15 22:35:19

第一件事函数生成的atatalist 必须仅首次致电
因此,我将从 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);
}

您可以这样创建您的布局:

<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>

我们将为 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();
}

,然后我们将更改 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();
}

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();
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文