如何在android recyclerview中进行动态分页

时间:2018-02-14 10:06:17

标签: android android-recyclerview

我有PAGE_SIZE = 10和PAGE_NUM = 1,每页必须显示10条记录,同时再次滚动页面时必须附加10条记录,并且必须更新PAGE_SIZE以及第10条记录的每一端它还必须动态显示ProgressBar。

推荐此链接。 http://blog.iamsuleiman.com/android-pagination-tutorial-getting-started-recyclerview/

我试图更新适配器,但我只获得前10条记录,而不是下一条即将发布的记录。

如果有人指导我解决这个问题,那将非常有帮助。

Activity.java

     recyclerView.addOnScrollListener(new PaginationScrollListener((LinearLayoutManager) mLayoutManager) {
                    @Override
                    protected void loadMoreItems() {
                        isLoading=true;
                        PAGE_NUM = PAGE_NUM + 1;
                        getData();

                    }

                    @Override
                    public int getTotalPageCount() {
                        return PAGE_NUM;
                    }

                    @Override
                    public boolean isLastPage() {
                        return isLastPage;
                    }

                    @Override
                    public boolean isLoading() {
                        return isLoading;
                    }
                });

 public void getData(){
     client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {
                    @Override
                    public void onSuccess(RestRequest request, final RestResponse result) {
                        try {
                            responseJsonObject = result.asJSONObject();
                            runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                jsonArrayList = DataHelper.populateJSONObjectList(responseJsonObject);
                                                result= DataHelper.getList(jsonArrayList);
                                                Log.d("result list>>", result.size() + "");
                                                dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);
                                                recyclerView.setAdapter(dataAdapter);
                                               if(!isLoading){
                                                    /*if(result.size()==PAGE_SIZE)
                                                        progressBar.setVisibility(View.VISIBLE);
                                                    if((result.size()%PAGE_SIZE)!=1){
                                                        progressBar.setVisibility(View.GONE);}*/
                                                       dataAdapter.addData(result);

                                                }

                                                if (result.size() > PAGE_SIZE) {
                                                    isLoading = false;
                                                    dataAdapter .addLoadingFooter();
                                                } else {
                                                    isLastPage = true;
                                                }

        }

Adapter.java

public void addData(List<Item> list) {
        for (int i = 0; i < list.size(); i++) {
            this.list.add(list.get(i));
        }
        notifyDataSetChanged();
    }
 public void addLoadingFooter() {
        isLoadingAdded = true;
        add(new Item());
    }

public void removeLoadingFooter() {
        isLoadingAdded = false;

        int position = list.size() - 1;
        Item item = getItem(position);

        if (item != null) {
            list.remove(position);
            notifyItemRemoved(position);
        }
    }

3 个答案:

答案 0 :(得分:0)

也许您应该添加第二种类型来将页脚加载到Adapter并通过notifyItemAdded()添加项目?

答案 1 :(得分:0)

这里的问题是,只有在第一次必须设置适配器时才进行分页

dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);
recyclerView.setAdapter(dataAdapter);

然后每一个  loadMore function

您必须在jsonArrayList中添加额外的10个项目,而不是每次都设置新的适配器。

如果您每次没有以前的项目时都设置了适配器。

希望您了解loadMore,您必须使用jsonArrayList调用向jsonArrayList.add(result)添加10个新项目的函数,然后您必须在适配器对象上调用notifyDataChanged

//使用此功能 在onCreate方法

dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);                       
recyclerView.setAdapter(dataAdapter);





recyclerView.addOnScrollListener(new PaginationScrollListener((LinearLayoutManager) mLayoutManager) {
                    @Override
                    protected void loadMoreItems() {
                        isLoading=true;
                        PAGE_NUM = PAGE_NUM + 1;
                        getData();

                    }

                    @Override
                    public int getTotalPageCount() {
                        return PAGE_NUM;
                    }

                    @Override
                    public boolean isLastPage() {
                        return isLastPage;
                    }

                    @Override
                    public boolean isLoading() {
                        return isLoading;
                    }
                });

 public void getData(){
     client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {
                    @Override
                    public void onSuccess(RestRequest request, final RestResponse result) {
                        try {
                            responseJsonObject = result.asJSONObject();
                            runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                jsonArrayList = DataHelper.populateJSONObjectList(responseJsonObject);
                                                result= DataHelper.getList(jsonArrayList);
                                                Log.d("result list>>", result.size() + "");

                                               if(!isLoading){
                                                    /*if(result.size()==PAGE_SIZE)
                                                        progressBar.setVisibility(View.VISIBLE);
                                                    if((result.size()%PAGE_SIZE)!=1){
                                                        progressBar.setVisibility(View.GONE);}*/
                                                       dataAdapter.addData(result);

                                                }

                                                if (result.size() > PAGE_SIZE) {
                                                    isLoading = false;
                                                    dataAdapter .addLoadingFooter();
                                                } else {
                                                    isLastPage = true;
                                                }

        }

答案 2 :(得分:0)

我尝试修改你的代码。

声明:

NotificationTexts_fr_FR.properties

Recycler的Scroll Listener

int PAGE_SIZE=10; //Number of items per page
int PAge_NUM=1; // page num with websevices providing accordingly
boolean mIsLoading=false; // fetching more data
boolean isMoreDataAvailable=true; // more data still available

获取数据方法;可能是WS调用

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mIsLoading)
                    return;
                int visibleItemCount = mLayoutManager.getChildCount();
                int totalItemCount = mLayoutManager.getItemCount();
                int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
                if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                    //Scrolled to End of list
                    if (dataAdapter != null && dataAdapter.getItemCount() < mTotalNoOfData && isMoreDataAvailable) {

                        getData();


      }
            }
        }
    });

将此方法添加到适配器

public void getData(){
     client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {
                    @Override
                    public void onSuccess(RestRequest request, final RestResponse result) {
                        try {
                            responseJsonObject = result.asJSONObject();
                            runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                jsonArrayList = DataHelper.populateJSONObjectList(responseJsonObject);
                                                result= DataHelper.getList(jsonArrayList);
                                                Log.d("result list>>", result.size() + "");
                        if(dataAdapter!=null && result.size()>0){
                        dataAdapter.addMoreDataToList(result); // just append more data to current list
                        }else{
                                                dataAdapter = new DataAdapter(MainActivity.this, result, client, jsonArrayList);
                                                recyclerView.setAdapter(dataAdapter);// setAdapter for first time only
                        }

                        if(result!=null){
                            if( result.size()<PAGE_SIZE)}
                            isMoreDataAvailable=false;
                            }
                        }


        }

我修改了我可能删除了一些代码。 希望这可以帮助你...... !!