Android - recyclerView.addOnScrollListener trigger last item in recyclerview

隐身守侯 提交于 2019-12-13 07:49:59

问题


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();
                }
            }
        }
    });

回答1:


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...



来源:https://stackoverflow.com/questions/31863060/android-recyclerview-addonscrolllistener-trigger-last-item-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!