java - addOnScrollListener不起作用(Android)

标签 java android xml

我正在我的应用程序上创建一个recyclerview分页,但是突然addOnScrollListener无法正常工作。我怎样才能解决这个问题?感谢您的任何帮助。

这是我的代码

  private void initView(){
        linearLayoutManager = new LinearLayoutManager(getBaseActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        myAdapter = new MyAdapter(list);
        myAdapter.setOnClickListener((data, position) -> {
            mydata = data;
        });
        recyclerView.setAdapter(myAdapter);
        recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
                isLoading = true;
                EndlessRecyclerViewScrollListener.setLoading(true);
                Map<String, Object> queryMap = new HashMap<>();
                queryMap.put("page",page);
                queryMap.put("item_count","20");
           myApiHelper.getDataUser(mytoken,id,queryMap);
                Log.d("log0","page "+page);
                Log.d("log0","totalItemsCount "+totalItemsCount);
                Log.d("log0","arrayList.size "+list.size());
                Log.d("log0","totalPages "+totalPages);
            }
        });
    }

最佳答案

大概应该是这样的:

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

    int pastVisibleItems = 0, visibleItemCount, totalItemCount;

    StaggeredGridLayoutManager mLayoutManager =
            (StaggeredGridLayoutManager) recyclerView.getLayoutManager();

    visibleItemCount = mLayoutManager.getChildCount();
    totalItemCount = mLayoutManager.getItemCount();
    int[] firstVisibleItems = null;
    firstVisibleItems = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
    if(firstVisibleItems != null && firstVisibleItems.length > 0) {
        pastVisibleItems = firstVisibleItems[0];
    }

    if (loading) {
        if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
            loading = false;
            Log.d(TAG, "LOAD NEXT ITEM");

            loadMoreItem();
        }
    }
}

关于java - addOnScrollListener不起作用(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55901303/

相关文章:

python - 如何使用minidom读取具有属性的XML标签的值

java - JFileChooser中的Mulitype选择

java - 将视频划分为字节

java - 标题不显示在 SlidingTabLayout 中

Android - ADT 更新问题

c# - 如何更改新 XML 文档中的属性值?

java - quickfix.InvalidMessage 即使有有效消息也会抛出异常

java - Play 编译时间过长

android - 网址中带有加号(“+”)的Android

xml - 在 Maven 中设置属性标志时如何跳过下载依赖项