简体   繁体   English

如何修复分页以在 RecyclerView 中从服务器加载更多数据?

[英]How to fix pagination to load more data from server in RecyclerView?

I want to load more data from the server.我想从服务器加载更多数据。 I should get firstItemId form the item is in RecyclerView .我应该从项目在RecyclerView获取firstItemId In listView I just did it on this way.listView我就是这样做的。

listMain.setOnScrollListener( new AbsListView.OnScrollListener() {

    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {

            if (lasttotal[getArguments().getInt( ARG_SECTION_NUMBER )] != totalItemCount) {
                lasttotal[getArguments().getInt( ARG_SECTION_NUMBER )] = totalItemCount;
                lastpage[getArguments().getInt( ARG_SECTION_NUMBER )]++;

                try {

                    JSONObject js_id = new JSONObject( view.getItemAtPosition( firstVisibleItem - 2 ).toString() );

                    static_id_news = js_id.getInt( "Id" );

                    if (getArguments().getInt( ARG_SECTION_NUMBER ) != 0)//"page=0&tagid="

                        new ApiNews( getActivity(), (ArrayAdapter) listMain.getAdapter(), rootView, ly_dialog ).execute( "id=" + static_id_news + "&tagid=" + newsnum[getArguments().getInt( ARG_SECTION_NUMBER )] + "&page=" + scroll_last_page + 1, 2 );
                    else
                        new ApiNewsPaper( getActivity(), (ArrayAdapter) listMain.getAdapter(), rootView, ly_dialog ).execute( lastpage[getArguments().getInt( ARG_SECTION_NUMBER )] );


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
});

Now I want just like example which will take the id of row and send it to load more data.现在我想要一个例子,它将获取row的 id 并发送它以加载更多数据。 Thanks.谢谢。

Try this solution for recyclerviewrecyclerview尝试此解决方案

You should use recyclerview , its better than listview because its fast , less complex and customization is available like staggerd view您应该使用recyclerview ,它比listview更好,因为它速度快,复杂度低,并且可以像交错视图一样进行自定义

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

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

                if (loading) {
                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        loading = false;
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
        }
    });

and set loading to true in response of your api like in success and failure case并将加载设置为 true 以响应您的api就像在成功和失败的情况下一样

loading = true;

where gridLayoutManager1 is layout manager, it could be linearlayout and gridlayout also , depends on your recyclerview其中gridLayoutManager1是布局管理器,它也可以是linearlayoutgridlayout ,这取决于您的recyclerview

 recyclerViews.setLayoutManager(gridLayoutManager1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM