如何在recyclerview android中加载更多逻辑

时间:2017-06-19 05:56:07

标签: android android-recyclerview onscrolllistener

我已经在footerview reclerview中创建了用户界面listview和progressbar,现在我真的卡住了如何加载更多我的数据JSON?

这个类获取第一页的数据json:

private void firstPage() {

        final ProgressDialog progressDialog = new ProgressDialog(Feed_Activity.this);
        progressDialog.setTitle("Mengambil data");
        progressDialog.show();
        Log.d(Status.TAG, "dam login fungsi goOAuth");

        Call<ResponseBody> token_request;
        Retrofit retrofit;
        retrofit = new Retrofit.Builder()
                .baseUrl(Status.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService apiService = retrofit.create(ApiService.class);
        token_request = apiService.feed();

        token_request.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                Log.d(Status.TAG, "dam  error code = " + response.code());


                if (response.code() == 200) {
                    try {

                        JSONObject responseObject = new JSONObject(response.body().string());
                        JSONArray arrayDocs =responseObject.getJSONArray("docs");


                    for (int i = 0; i < arrayDocs.length(); i++) {

                         //parsing json proccess

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

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
                progressDialog.dismiss();
            }

        });
    }

现在我必须在nextPage()函数中做什么才能加载更多?

private void nextPage() {

//what must i do in here for load more my json data 

}

和我的api使用这样的端点

https://penawaran.8villages.com/activities/filter?challengeId=*********&limit=10&offset=0

page 1: limit=10&offset=0
page 2: limit=10&offset=10
page 3: limit=10&offset=20
请某人帮助我,我真的坚持工作:(

1 个答案:

答案 0 :(得分:1)

ArrayList<JSONObject> jsonArrayList=new ArrayList<>(); 
   boolean isLoadMore;
    int offset=0;   
      @Override
        public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) {

        if(isLoadMore && position==jsonArrayList.size()-1)
    {
    offset+=10;
    isLoadMore =false;
    nextPage();//your logic here to hit web service
    }

        }
    ////////////////////////////

        private void firstPage() {

            final ProgressDialog progressDialog = new ProgressDialog(Feed_Activity.this);
            progressDialog.setTitle("Mengambil data");
            progressDialog.show();
            Log.d(Status.TAG, "dam login fungsi goOAuth");

            Call<ResponseBody> token_request;
            Retrofit retrofit;
            retrofit = new Retrofit.Builder()
                    .baseUrl(Status.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            ApiService apiService = retrofit.create(ApiService.class);
            token_request = apiService.feed();

            token_request.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                    Log.d(Status.TAG, "dam  error code = " + response.code());


                    if (response.code() == 200) {
                        try {

                            JSONObject responseObject = new JSONObject(response.body().string());
                            JSONArray arrayDocs =responseObject.getJSONArray("docs");

    /*here if lenght of arrayDocs  will be more then or equal to limit i.e. 10 in your case then isLoadMore will be true*/

        isLoadMore=arrayDocs.length()>=10;//same condition you should be check in nextpage() method each time


                        for (int i = 0; i < arrayDocs.length(); i++) {

                             //parsing json proccess 
                             /*add jsonObject into your jsonArrayList and notify your adapter with the same list*/
                             jsonArrayList.add(arrayDocs.get(i));

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

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    t.printStackTrace();
                    progressDialog.dismiss();
                }

            });
        }