当前位置: 首页 > 知识库问答 >
问题:

回收人员查看加载更多数据哪些最短和最简单的方法?[复制]

姜运珧
2023-03-14

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

评论帖子。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();
}

共有2个答案

冀永寿
2023-03-14

首先,函数generateDataList只能第一次调用,所以我将从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();
}

邢永安
2023-03-14

Google提供了一个用于此目的的库,即分页库,您可以从Google文档中了解它的工作原理,也可以观看此视频,学习如何使用分页库来实现所需的结果。

 类似资料:
  • 我是新的java android dev。我的项目遇到了一个问题。我想加载更多数据以在10个数据加载后显示回收视图。下面的示例。我很累了。我无法理解解决方案。我想要一个简单的解决方案。 评论帖子。Java语言 适配器:

  • 问题内容: 有谁知道一种简单的方法将数据库从一台计算机复制到文件,然后将其导入另一台计算机? 问题答案: 以下是一些选择: mysqldump 做到这一点最简单,可以保证工作的方法是使用。请在此处查看实用程序的手册页: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html 基本上,它转储重建数据库内容所需的SQL脚本,包括创建表,触发器和其他对

  • 我想有一个固定行数的recyclerView,不可滚动,每行有一个固定的高度,以及填充所有屏幕。例如,我有4行,所以行高应该是recyclerView height/4。 行布局。xml 回收人员视图: java代码: 有什么想法吗?

  • 我需要制作一个可扩展的 ,每次点击只能打开一个项目(所有其他项目都必须关闭)。我知道有可能在的帮助下做到这一点,然后使用下一个代码: 但是有没有办法使用来做同样的事情?

  • FolderListAdapter代码 RecyclerView的Mainactive代码 回收器视图的XML代码 folderList的大小为2 我还检查了适配器的大小。它也是2,我不知道我在哪里犯了错误,为什么它现在在活动中什么都没有显示

  • 我正在构建一个聊天应用程序,并使用 RecyclerView 呈现消息。由于这是一个聊天应用程序,最后的消息应显示在列表的底部。为了实现这一点,我通过以下方式使用LinearManager: 如果对话中有很多信息,它也能很好地工作。但是,如果用户之间只有一两条消息,RecyclerView会将它们显示在屏幕底部,并在它们上方留出空白。 在这种情况下,是否可以在屏幕顶部显示回收器项目?