OnScrollListener在My RecyclerView中不起作用-AndroidX

时间:2019-11-14 05:30:10

标签: java android android-recyclerview onscrolllistener

我在RecyclerView中添加了onScrollListener,当我尝试向下滚动直到RecycleView的最底部不能激活OnScrollListener时,我想知道为什么

RVHomeLatest = v.findViewById(R.id.RV_HomeLatest);
        adapterHomeLatest = new AdapterHomeLatest(getActivity(), modelHomeLatests, listenerHomeLatest);
        final LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        RVHomeLatest.setLayoutManager(llm);
        RVHomeLatest.setAdapter(adapterHomeLatest);
        //Optimized
        RVHomeLatest.setHasFixedSize(true);
        RVHomeLatest.setItemViewCacheSize(20);

        // Pagination
        final boolean[] loading = {true};
        final int[] pastVisiblesItems = new int[1];
        final int[] visibleItemCount = new int[1];
        final int[] totalItemCount = new int[1];

        RVHomeLatest.addOnScrollListener(new RecyclerView.OnScrollListener()
        {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy)
            {
                if(dy > 0) //check for scroll down
                {
                    visibleItemCount[0] = llm.getChildCount();
                    totalItemCount[0] = llm.getItemCount();
                    pastVisiblesItems[0] = llm.findFirstVisibleItemPosition();

                    if (loading[0])
                    {
                        Toast.makeText(getActivity(),"YYY", Toast.LENGTH_SHORT).show();
                        if ( (visibleItemCount[0] + pastVisiblesItems[0]) >= totalItemCount[0])
                        {
                            loading[0] = false;
                            Log.v("...", "Last Item Wow !");
                            //Do pagination.. i.e. fetch new data
                        }
                    }
                }
            }
        });

0 个答案:

没有答案
相关问题