简体   繁体   English

滚动时如何将数据加载到 RecyclerView

[英]How to load data into RecyclerView while scrolling

I am using RecyclerView, and its calling all the data items, which are not even seen by user.我正在使用 RecyclerView,它调用所有数据项,用户甚至看不到这些数据项。 So how can i load only that item which are seen by user, or load while scrolling那么我怎样才能只加载用户看到的那个项目,或者在滚动时加载

XML: XML:

  <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/post_ImagesRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:background="@color/grey_bg"
    android:nestedScrollingEnabled="false"/>

Java: Java:

    postAdapter = new PostAdapter(context, false, false, postList, this, 1, loadType);
        LinearLayoutManager postListManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        post.setLayoutManager(postListManager);
        post.setAdapter(postAdapter);

As it's said in official documentation,正如官方文档中所说,

The views in the list are represented by view holder objects.列表中的视图由视图持有者对象表示。 These objects are instances of a class you define by extending RecyclerView.ViewHolder.这些对象是您通过扩展 RecyclerView.ViewHolder 定义的 class 的实例。 Each view holder is in charge of displaying a single item with a view.每个视图持有者负责显示带有视图的单个项目。 For example, if your list shows music collection, each view holder might represent a single album.例如,如果您的列表显示音乐收藏,则每个视图持有者可能代表一个专辑。 The RecyclerView creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra. RecyclerView 只创建显示动态内容的屏幕部分所需数量的视图持有者,以及一些额外的。 As the user scrolls through the list, the RecyclerView takes the off-screen views and rebinds them to the data which is scrolling onto the screen.当用户滚动列表时,RecyclerView 获取屏幕外视图并将它们重新绑定到滚动到屏幕上的数据。

So, if you use RecyclerView you shouldn't worry about this problem所以,如果你使用 RecyclerView 你不应该担心这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM