使用分页进行回收查看

时间:2017-12-17 11:18:17

标签: php android retrofit

我正在使用Retrofit API,我的网络服务包含后端分页。响应数据将加载到Recyceview.initial视图我已经请求了10条记录到Recyclerview(限制= 10, offset = 0)。当用户向下滚动循环列表时,我想从后端请求接下来的10条记录。向上滚动,之前的数据应该在那里。

我试过了代码。 但是当我向下滚动列表时,所有请求都会立即完成并最终显示一个空列表。

这是我的代码

int limit=10,offset=0; //set globally
boolean isLastPage=false;



public void filterFunc(){
        OrderArr.clear();

        WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
        Call<AllOrdersResponse> call = apiService.filter("list",token_acces, limit, offset);
        call.enqueue(new Callback<AllOrdersResponse>() {
            @Override
            public void onResponse(Call<AllOrdersResponse> call, Response<AllOrdersResponse> response) {

                try {

                    AllOrdersResponse result = response.body();
                    List<AllOrders> data=result.getData();
                    status=result.isStatus();
                    msg= result.getMessage();
                    count=result.getCount(); // total record count without pagination

                    if(status){
                        for (AllOrders a: data){
                            if (count >= offset) {
                                OrderArr.add(new AllOrders(a.getId_order(),a.getReference(), a.getAddress1(),"LKR "+a.getTotal_products(),a.getStatus()));
                            } else {
                                isLastPage = true;
                            }
                        }
                        progress.dismiss();
                        layoutManager = new LinearLayoutManager(getActivity());
                        RecycleOrderList.setLayoutManager(layoutManager);
                        adapter = new HistoryAdapter(OrderArr);
                        RecycleOrderList.setAdapter(adapter);// set adapter on recyclerview
                        adapter.notifyDataSetChanged();// Notify the adapter
                    }else{
                        progress.dismiss();
                    }

                }catch (NullPointerException e){
                    progress.dismiss();
                    Toast.makeText(getActivity(),"No orders to display",Toast.LENGTH_LONG).show();                    

                }
            }

            @Override
            public void onFailure(Call<AllOrdersResponse> call, Throwable t) {
                Log.d("fragres",""+t.getMessage());

            }
        });

        scrollRecycle(type);

    }



public void scrollRecycle(){
        RecycleOrderList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int visibleItemCount = layoutManager.getChildCount();
                int totalItemCount = layoutManager.getItemCount();
                int firstVisibleItemPosition= layoutManager.findFirstVisibleItemPosition();
                int last= layoutManager.findLastCompletelyVisibleItemPosition();

                try {

                    if (!isLastPage){
                        if ( ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0)) {
                            progress.show();

                            if(last==visibleItemCount-1){
                                offset=offset+limit;
                                filterFunc();
                            }
                        }
                        progress.dismiss();
                    }

                }catch (IndexOutOfBoundsException e){

                }
                Log.d("scroll "," limit "+limit+" offset"+offset+" loading page "+isLastPage);

0 个答案:

没有答案