第一次在 RecyclerView 中使用 3 个跨度计数的 GridLayoutManager 加载超过 10 个项目 [英] Load more than 10 items in RecyclerView with GridLayoutManager of 3 span count on first time

查看:27
本文介绍了第一次在 RecyclerView 中使用 3 个跨度计数的 GridLayoutManager 加载超过 10 个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的 recyclerView 中使用 GridLayoutManager 和 3 个跨度计数,问题是当我第一次运行应用程序时,由于来自服务器的项目的限制/分页,当用户使用 <刷新时,它只显示前 10 个项目code>SwipeRefreshLayout 或滚动它会继续加载其他项目,但在第一次加载项目时看起来很难看,所以我想填补这些空白而无需刷新加载其他项目

I using GridLayoutManager with 3 span count in recyclerView on my app, the problem is when I run the app first time it's showing only first 10 items due to the the restriction / pagiation of the items from server, when user refreshing with SwipeRefreshLayout or scrolling it's continue loading other items, but it's look ugly on first load of items so I want fill these gaps without necessary for refreshing to load other items

这是带有滚动侦听器的网格布局

here's the gridlayout with scroll listener

gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    isScrolling = true;
                    progressBar.setVisibility(View.GONE);

                }

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                        currentItems = gridLayoutManager.getChildCount();
                        totalItems = gridLayoutManager.getItemCount();
                        scrollOutItems = gridLayoutManager.findFirstVisibleItemPosition();

                    if (isScrolling && (currentItems + scrollOutItems == totalItems)) {
                        isScrolling = false;
                        if (getItemsByLabelCalled) {
                            for (int i = 1; i < 7; i++) {
                                if (navigationView.getMenu().getItem(i).isChecked()) {
                                    getItemsByLabel(navigationView.getMenu().getItem(i).getTitle().toString());
                                }
                            }
                        } else {
                            getData();
                        }
                    }
                }
            }
        });

SwipeRefreshLayout

SwipeRefreshLayout

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (navigationView.getMenu().getItem(0).isChecked()) {
                    if (Utils.hasNetworkAccess(MainActivity.this)) {
                        getData();
                    } else {
                        Toast.makeText
                                (MainActivity.this, R.string.SwipeRefreshLayout_connect_to_update
                                        , Toast.LENGTH_LONG).show();
                    }
                } else {
                    for (int i = 1; i < 7; i++) {
                        if (navigationView.getMenu().getItem(i).isChecked()) {
                            getItemsByLabel(navigationView.getMenu().getItem(i).getTitle().toString());
                        }
                    }
                }

                new Handler().postDelayed(() -> swipeRefreshLayout.setRefreshing(false), 2000);
            }

        });

用图片描述的问题

推荐答案

问题是您的项目数量不足以填满整个页面.有两种方法可以解决这个问题.

The problem is that your items count is not enough to fill the entire page. There are two ways to overcome this issue.

  1. 您可以在加载第一批后再次调用您的 getData 方法以获取更多数据,这不是一个干净的解决方案,因为您可能需要在平板电脑等更大屏幕上显示更多项目.

  1. You can call your getData method one more time after loading the first batch to get more data which is not a clean solution since you might need more items on larger screens like tablets.

将加载更多过程放在回收器视图适配器的 onBindViewHolder 方法中,而不是它的滚动侦听器中.如果 position == getItemCount()-1 例如,如果您的最后一个项目进入视图(或需要绑定),它会请求服务器提供更多项目,则您可以执行更多加载.请记住,您必须持有一个布尔值,以防止重复调用 getData.

Put your load more procedure in onBindViewHolder method of your recycler view adapter instead of its on scroll listener. You can do the load more if position == getItemCount()-1 for instance which requests server for more items if your last item gets in view (or needs binding). Remember that you have to hold a boolean in order to prevent repetitive calls on your getData.

这篇关于第一次在 RecyclerView 中使用 3 个跨度计数的 GridLayoutManager 加载超过 10 个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆