Android的 - 在recyclerview recyclerView.addOnScrollListener触发最后一个项目 [英] Android - recyclerView.addOnScrollListener trigger last item in recyclerview

查看:2978
本文介绍了Android的 - 在recyclerview recyclerView.addOnScrollListener触发最后一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code中,我得到使用YouTube数据API,现在我想以填充使用nextPageTolen的记录,因为我有我的JSON 5000左右的记录数据。
这里是我的code里面我是用填充到RecyclerView。

我的问题是,当我运行的应用程序,并滚动到底它加载一个页面,并触发下一个页面的最后一个项目,然后将它没有超越第2页和表演

  Toast.makeText(getActivity(),totalItemCount< pastVisiblesItems,Toast.LENGTH_SHORT).show();

吐司消息不断结果
有人可以指导我在哪里,我不得不在我的code的变化?

我想填充更多的记录,只要用户不滚动到底

 私人布尔加载=真实的;
        私人INT pastVisiblesItems = 0;
        INT visibleItemCount,totalItemCount,firstVisiblesItem;
        listVideos.addOnScrollListener(新RecyclerView.OnScrollListener(){
        @覆盖
        公共无效onScrolled(RecyclerView recyclerView,诠释DX,DY INT){
            super.onScrolled(recyclerView,DX,DY);
            visibleItemCount = listVideos.getChildCount();
            totalItemCount = linearLayoutManager.getItemCount();
            firstVisiblesItem = linearLayoutManager.findFirstVisibleItemPosition();
            INT lastVisibleItemCount = linearLayoutManager.findLastVisibleItemPosition();            如果(装载){
                Log.v(...,totalItemCount< pastVisiblesItems);                ** Toast.makeText(getActivity(),totalItemCount&下; pastVisiblesItems,Toast.LENGTH_SHORT).show(); **                如果(totalItemCount> pastVisiblesItems){
                    加载= FALSE;
                    pastVisiblesItems = totalItemCount;
                }
            }            如果(!加载){
                如果(totalItemCount大于0){
                    如果((totalItemCount - 1)== lastVisibleItemCount){
                        Log.v(...,最后的项目哇!);
                        Toast.makeText(getActivity(),最后的项目,Toast.LENGTH_SHORT).show();
                        loadMoreVideos lMVideos =新loadMoreVideos();
                        lMVideos.execute();
                        加载=真实的;
                    }
                }
                其他{
                    Log.v(...,加载=真正的);
                    Toast.makeText(getActivity(页),=真,Toast.LENGTH_SHORT).show();
                }
            }
        }
    });


解决方案

这是我如何得到这个问题的解决方案,我发布这个答案对于那些谁拥有相同的pronlems ...

如果您使用的是LinearLayoutManager或StaggeredGridLayoutManager,他们每个人都有一个scrollToPositionWithOffset方法既需要的位置,并从RecyclerView,这似乎是它的启动项目的启动也偏移会完成你所需要的(设置偏移为0,应与顶部对齐)。

例如:

  //滚动项目2到20从顶部像素
    linearLayoutManager.scrollToPositionWithOffset(2,20);

这每一次对我的作品......现在,当新的页面加载它触发第一项...

I have written a code in which i am getting data using youtube data api, now i would like to populate records using nextPageTolen, because i have around 5000 records in my JSON. Here is my code which i am using to populate into RecyclerView.

my problem is when i run app and scroll to bottom it loads next page and trigger last item of next page and then it does not go beyond 2nd page and shows

    Toast.makeText(getActivity(), "totalItemCount < pastVisiblesItems", Toast.LENGTH_SHORT).show();  

Toast message continuously
Can someone guide me where i have to make changes in my code ?

I want to populate more records whenever user do scroll to bottom

        private boolean loading = true;
        private int pastVisiblesItems = 0;
        int visibleItemCount, totalItemCount, firstVisiblesItem;


        listVideos.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = listVideos.getChildCount();
            totalItemCount = linearLayoutManager.getItemCount();
            firstVisiblesItem = linearLayoutManager.findFirstVisibleItemPosition();
            int lastVisibleItemCount = linearLayoutManager.findLastVisibleItemPosition();

            if (loading) {
                Log.v("...", "totalItemCount < pastVisiblesItems");

                **Toast.makeText(getActivity(), "totalItemCount < pastVisiblesItems", Toast.LENGTH_SHORT).show();**

                if (totalItemCount > pastVisiblesItems) {
                    loading = false;
                    pastVisiblesItems = totalItemCount;
                }
            }

            if (!loading){
                if (totalItemCount > 0){
                    if ((totalItemCount - 1) == lastVisibleItemCount){
                        Log.v("...", "Last Item Wow !");
                        Toast.makeText(getActivity(), "Last Item", Toast.LENGTH_SHORT).show();
                        loadMoreVideos lMVideos = new loadMoreVideos();
                        lMVideos.execute();
                        loading = true;
                    }
                }
                else {
                    Log.v("...", "loading = true");
                    Toast.makeText(getActivity(), "loading = true", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });

解决方案

this is how i get the solution for this question, i post this answer for those who have same pronlems...

If you are using the LinearLayoutManager or StaggeredGridLayoutManager, they each have a scrollToPositionWithOffset method that takes both the position and also the offset of the start of the item from the start of the RecyclerView, which seems like it would accomplish what you need (setting the offset to 0 should align with the top).

For instance:

    //Scroll item 2 to 20 pixels from the top
    linearLayoutManager.scrollToPositionWithOffset(2, 20);

this works for me... now every time when new page loads it triggers first item...

这篇关于Android的 - 在recyclerview recyclerView.addOnScrollListener触发最后一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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