与API数组大小不同的无尽Scroll RecyclerView [英] Endless Scroll RecyclerView with dissimilar array sizes from API

查看:105
本文介绍了与API数组大小不同的无尽Scroll RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用scrollListener启用无限滚动-您可以在这里找到我的问题和Vilen的答案:

I'm currently using the scrollListener to enable endless scroll - you can find my question and Vilen's answer here:

使用ProgressBar通过以下位置向Endless Scroll RecyclerView添加项目底部

当您在滚动时每次激活适配器时,是否具有相同数据集大小时,这种方法就很奇怪.例如,在Vilen的代码中,他正在将数据集大小增加相同金额,每次他滚动时都为15.

This works wonders when you have the same dataset size each time you activate your adapter when you scroll For example, in Vilen's code, he is increasing the size of the dataset by the same amount, 15, each time he scrolls.

但是,在现实世界中,情况往往并非如此.您一直具有不相似数据集大小.在我的示例中,我想执行以下操作:

However, in the real world, this is often not the case. You have dissimilar dataset sizes all the time. In my example, I would like to do the following:

我想实施地理空间搜索,以便它返回离我最近的地方列表.现在,如果我坐在沙漠中,我的REST API调用将返回距离我10公里以内的地方,而这可能只是4个地方(骆驼小屋,金字塔,水坑和椰子树).

I would like to implement a geospatial search so that it returns a list of places closest to me. Now, if I'm sitting in a desert, my REST API call would return the places within 10km of me and this could just be 4 places (the camel hut, the pyramids, the watering hole and the coconut tree).

在recyclerview中填充4个项目不会使recyclerview足够长,无法使其进一步滚动,因此我的应用将再次触发API调用以查找距离我更近的地方.接下来的10公里(距离我当前位置20公里)可能是一个城镇,还有很多地方可以返回,但是由于recyclerview的滚动时间不够长,因此无法进行API调用.

Populating 4 items in a recyclerview will not make the recyclerview long enough to allow the it to scroll further so that my app will trigger the API call again to find more places closer to me. The next 10km (20km from my current location) could be a town with lots more places to return, but because the recyclerview is not long enough to scroll, the API call is not made.

我还做了一个小小的存储库,以说明当recyclerview太短时,不会触发滚动侦听器:

I have also made a tiny repo to demonstrate that the scroll listener is not triggered when the recyclerview is too short:

https://github.com/Winghin2517/DissimilarDataSetSizeRV.git

如何解决这个问题?

我几乎感觉到应该在recyclerview填满屏幕之前触发滚动侦听器,但是据我所知,我如何确定何时将屏幕填满,因为没有针对recyclerview-fill-up屏幕的回调

I almost feel like the scrolllistener should be triggered until the recyclerview fills up the screen but how can I determine when the screen will be filled up as there is no callback for recyclerview-fill-up-screen, as far as I know.

我试图停止更改boolean loading,这将有所帮助,但是如果没有布尔值检查加载状态,则不一定会删除progressBar导致这种类型的效果:

I had tried to stop changing the boolean loading to see that would help but without the boolean checking the status of the load, the progressBar will not necessarily be removed leading to this type of effect:

推荐答案

您可以尝试这种方法(下面的代码来自我的一个应用程序,因此您将不得不填补空白,我只添加了需要解决的部分您的问题)>

You could try this approach (The code below is from one of my apps, so you will have to fill the gaps, I just added the parts that shall solve your problem)>

public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener 
// Or use yourListView.setOnScrollListener(this);
{

    int preLast = -1;

    @Override
    public void onScroll(AbsListView lw, final int aFirstVisibleItemIndex, final int aVisibleItemCount, final int aTotalItemsCount) {
        int id = mListView.getId(); // Or use case android.R.id.list:     

        if(lw.getId() == id) {
                     // Sample calculation to determine if the last item is fully visible.
            final int lastItem = aFirstVisibleItemIndex + aVisibleItemCount;
            if(lastItem > 0 && lastItem == aTotalItemsCount) // lastItem > 0 helps to avoid continuous call if the list is empty.
            {
                if(preLast != lastItem) { //to avoid multiple calls for last item
                    preLast = lastItem;

                }
              }
        }
      }
}

@Override //it will be called before any calls to Adapter.getView(.....)
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

现在,每当您决定没有更多数据要从服务器中获取(以填充列表中的更多项目)并且想要避免Listview继续调用/激活您的(正在加载动画时)时,只需将preLast值设置为lastItem值即可.

And now and whenever you decide that there is no more data to fetch from your sever (to populate more items in your list) and you want to avoid the listview to keep calling / activating your (on going loading animation), you can simply set preLast value to lastItem value.

这篇关于与API数组大小不同的无尽Scroll RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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