【问题标题】:Recyclerview Swipe up pagination AndroidRecyclerview 向上滑动分页 Android
【发布时间】:2017-01-20 17:24:02
【问题描述】:

我在我的应用程序中使用 RecyclerView 和 CardView,当用户滑动到 RecyclerView 的最末端时,我尝试进行分页操作,然后在用户向上滑动时加载更多内容。 (就像向下滑动刷新)。我现在拥有的是当用户到达 RecyclerView 的最后一项时,它将自动加载。如何更改它以便在用户拉起时加载?

    private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView,
                                     int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if(dy > 0) //check for scroll down
        {
            visibleItemCount = gmLayoutManager.getChildCount();
            totalItemCount = gmLayoutManager.getItemCount();
            pastVisiblesItems = gmLayoutManager.findFirstVisibleItemPosition();
            int loadedItems = pastVisiblesItems + visibleItemCount;
            //System.out.println(visibleItemCount + "/n" +totalItemCount + "/n"+pastVisiblesItems + "/n" + loadedItems);
            if (true)
            {
                if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
                {
                    loading = false;
                    Log.v("...", "Last Item !");
                    //Do pagination.. i.e. fetch new data
                    String api = "stores?offset=10";
                    getData(api); //Load more data
                }
            }
        }
    }
};

【问题讨论】:

    标签: android pagination


    【解决方案1】:

    我想你需要在父视图上设置一些onTouchListener。并发送在 RecyclerView 中设置一些变量以指示它是否在底部。所以在监听器中你基本上会做这样的事情:

    if (recyclerView.isAtBottom() && motionEvent.getAction() == MotionEvent.ACTION_MOVE){
     //calculate some offset here
        if (offset>MY_OFFSET){
           String api = "stores?offset=10";
           getData(api); //Load more data
        }
    }       
    

    此外,您可能需要在 RecyclerView 到达按钮时触发一些事件,以便您的触摸侦听器此时记住指针位置。

    【讨论】:

      【解决方案2】:

      Hey Jerryko 查看 Loadmore recyclerview 的示例示例

      https://github.com/Pyush/Android-RecyclerView-Loadmore

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 2023-03-19
        • 1970-01-01
        • 2018-02-06
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多