簡體   English   中英

android recyclerview json加載更多項目

[英]android recyclerview json load more items

我有一段時間這個問題,我需要從json的數據庫中加載25個以上的項目。 當我加載所有包含圖像等內容時,該應用程序會花一些時間才能加載所有圖像。 所以我想可以加載前五個,然后滾動到底部時加載第二個,依此類推。 但這行不通。 這是我的代碼:

滾動偵聽器:

rec.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            visible = lin.getChildCount();
            total = lin.getItemCount();
            past = lin.findFirstVisibleItemPosition();

            if ((visible + past) >= total){
                new HttpAsyncTask().loadMore();
            }
        }
    });

AsyncTask:

private class HttpAsyncTask extends AsyncTask<String, Void, List<PostsData>> {
    int next;
    @Override
    protected List<PostsData> doInBackground(String... params) {
        try {
            //get json from url
            JSONObject object = new JSONObject("{'posts':"+GET("http://www.website.com/json")+"}");
            //get json array
            JSONArray array = object.getJSONArray("posts");
                for (int i = 0; i < 5; i++) {
                    next = i;
                    PostsData data = new PostsData();
                    //get object from array
                    JSONObject jsonObject = array.getJSONObject(i);
                    //title
                    data.setTitle(jsonObject.getString("name"));
                    //id
                    data.setId(jsonObject.getInt("id"));
                    JSONObject cat = jsonObject.getJSONObject("category");
                    //category name
                    data.setCatagorie(cat.getString("name"));
                    JSONObject img = jsonObject.getJSONObject("thumbnails");
                    //image url
                    String imgUrl = img.getString("preview_url");
                    //convert url into bitmap
                    data.setImage(getBitmap(imgUrl));

                    //get the post submitter name from array 'makers'
                    String makerFullname = "";
                    JSONArray userArray = jsonObject.getJSONArray("makers");
                    for (int j = 0; j < userArray.length(); j++) {
                        JSONObject user = userArray.getJSONObject(j);
                        //submitter full name
                        makerFullname = user.getString("full_name");
                        data.setUser(makerFullname);
                    }

                    data.setSubmitUser(jsonObject.getJSONObject("submitter").getString("full_name"));

                    //get the like count of the post
                    data.setLikeCount(jsonObject.getString("upvotes_count"));

                    list.add(data);
                }
        }catch (JSONException e){
            e.printStackTrace();
        }
        return list;
    }

    // onPostExecute displays th
    // results of the AsyncTask.
    @Override
    protected void onPostExecute(List<PostsData> result) {
        adapter = new PostsAdapter(result, getContext());
        rec.setAdapter(adapter);
    }

    public void loadMore(){
        try {
            //get json from url
            JSONObject object = new JSONObject("{'posts':" + GET("http://www.materialup.com/api/v1/posts") + "}");
            //get json array
            JSONArray array = object.getJSONArray("posts");
            if (array.length() <= next) {
                for (int i = 0; i < next + 5; i++) {
                    next = i;
                    PostsData data = new PostsData();
                    //get object from array
                    JSONObject jsonObject = array.getJSONObject(i);
                    //title
                    data.setTitle(jsonObject.getString("name"));
                    //id
                    data.setId(jsonObject.getInt("id"));
                    JSONObject cat = jsonObject.getJSONObject("category");
                    //category name
                    data.setCatagorie(cat.getString("name"));
                    JSONObject img = jsonObject.getJSONObject("thumbnails");
                    //image url
                    String imgUrl = img.getString("preview_url");
                    //convert url into bitmap
                    data.setImage(getBitmap(imgUrl));

                    //get the post submitter name from array 'makers'
                    String makerFullname = "";
                    JSONArray userArray = jsonObject.getJSONArray("makers");
                    for (int j = 0; j < userArray.length(); j++) {
                        JSONObject user = userArray.getJSONObject(j);
                        //submitter full name
                        makerFullname = user.getString("full_name");
                        data.setUser(makerFullname);
                    }

                    data.setSubmitUser(jsonObject.getJSONObject("submitter").getString("full_name"));

                    //get the like count of the post
                    data.setLikeCount(jsonObject.getString("upvotes_count"));

                    list.add(data);
                    adapter.notifyDataSetChanged();
                }
            }
            }catch(JSONException e){
                e.printStackTrace();
            }
    }.....

它只是不會在recyclerview中加載接下來的5個項目。 我在Google上進行了搜索,但我並不真正了解如何加載更多作品。

預先感謝,斯文。

這是我遵循的教程: 此處

將下一個變量設為靜態變量,並使用i=next+1,i <next+6開頭語句的loadMore方法,以避免再次加載相同的數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM