简体   繁体   English

在 recyclerview 滚动时自动加载下一页的图像

[英]Automatically load images of next page on recyclerview scrolling

I am using pixabay's api to show images in my recyclerview in grid layout.我正在使用 pixabay 的 api 以网格布局在我的 recyclerview 中显示图像。 Everything is working fine,i want to show images of next page in the same activity when user reaches end of images in recyclerview of first page ie when user reaches end of recyclerview make another request and increase page by 1 and show images of next page at the same time i want to keep images of first page as it is,if the user scrolls up again it should see images of first page.Here is my request.一切正常,我想在同一活动中显示下一页的图像,当用户到达第一页的 recyclerview 中的图像末尾时,即当用户到达 recyclerview 的末尾时发出另一个请求并将页面增加 1 并显示下一页的图像同时我想保留第一页的图像,如果用户再次向上滚动它应该看到第一页的图像。这是我的请求。

https://pixabay.com/api/?q=yellow&key=MY_API_KEY&image_type=photo&page=1

How to automatically increase page no when user reaches end of recyclerview.当用户到达回收站视图末尾时如何自动增加页面编号。 I don't want to show buttons at the end of recyclerview for next page images我不想在下一页图像的 recyclerview 末尾显示按钮

I think you'll need to implement addOnSrollListener() in your recyclerview.我认为你需要在你的 recyclerview 中实现addOnSrollListener() You should implement something like that:你应该实现这样的东西:

listRepositories.addOnScrollListener(new 
    RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            int pastVisibleItems, visibleItemsCount, totalItemCount;

            if (dy > 0) {
                visibleItemsCount = layoutManager.getChildCount();
                pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
                totalItemCount = layoutManager.getItemCount();

                if ((visibleItemsCount + pastVisibleItems) >= totalItemCount) {
                    // method where you get your images
                    load(currentPage++);
                }

            }
        }

    });

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

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