在recyclerview中显示来自服务器的数据时无限滚动

时间:2018-06-16 13:53:38

标签: android android-recyclerview android-asynctask android-volley

我有一个图片应用。它从服务器和数据库加载图像。当用户点击特定按钮应用程序时,使用截击发送带有发布数据(类别)的请求到服务器。 服务器比获取post变量并从数据库获取数据并作为json响应发送数据。然后我在recyclerview显示图片。

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    imagelist = new ArrayList<>();
    adapter = new ImageAdapter(DJPhotos.this, imagelist);

    RecyclerView.LayoutManager mLayoutManager = new 
    LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

    public void getJsonResponsePost() {

    JSONObject json = new JSONObject();
    try {
        json.put("table", table);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, script, json,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    imagelist.clear();
                    JSONArray heroArray = null;
                    try {
                        heroArray = response.getJSONArray("item");
                        for (int i = 0; i < heroArray.length(); i++) {
                            JSONObject jsonObject = heroArray.getJSONObject(i);

                            Photos photos = new Photos();
                            photos.setUrl(jsonObject.getString("image"));
                            photos.setThumb(jsonObject.getString("thumb"));
                            imagelist.add(photos);

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

                    adapter.notifyDataSetChanged();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error: " + error.getMessage());

        }
    });
    jsonObjectRequest.setTag(REQ_TAG);
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance().addToRequestQueue(jsonObjectRequest);

}

我的 PHP 脚本 -

 $sql = "select * from $table  WHERE (dp='1') ORDER BY id DESC LIMIT 10";
 $query = mysqli_query($db, $sql);
 $post_data_array = array();
 while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
         $img     = $row['image'];
         $post_data_array[] = array(
                 'image' => $image
            );
 $post_data = json_encode(array('item' => $post_data_array));
 echo $post_data;

数据库中有很多图像,如果我没有设置限制,那么服务器需要时间来获取数据。它使应用程序变慢。 我有一个网站,我使用分页。在分页中,我使用LIMIT来显示数据。 现在我希望当用户点击按钮时,只有前10个图像来自数据库加载,用户滚动后recyclerview另外10个图像应加载并继续。

我知道可以使用AsyncTask完成,但我找不到符合我情况的脚本。

2 个答案:

答案 0 :(得分:2)

  

我知道可以使用AsyncTask

完成

这是有史以来最糟糕的做法。

Android现在提供原生分页库作为 Android体系结构组件的一部分,使用它阅读更多相关信息here。在此方法之前,人们习惯使用设置为回收器视图的onScrollChangedListener来执行此操作,您仍然可以使用它,但建议使用分页库。

答案 1 :(得分:0)

在适配器上

为例如创建这样的界面。

public interface OnFetchMoreRequested {
    void fetchMore();
}

然后onBindViewHolder你可以根据你的条件触发那个接口,例如

@Override
public void onBindViewHolder(@NonNull final RecipeViewHolder holder, int position) {
    if(position == imagelist.size() - 1 && onFetchMoreRequested != null){
        onFetchMoreRequested.fetchMore();
    }
    ... rest of the code
}

还为OnFetchMoreRequested接口

创建一个setter和一个字段
OnFetchMoreRequested onFetchMoreRequested = null;

public void setOnFetchMoreRequested(OnFetchMoreRequested onFetchMoreRequested){
    this.onFetchMoreRequested = onFetchMoreRequested;
}

关于活动

adapter.setOnFetchMoreRequested(new OnFetchMoreRequested() {
    @Override
    void fetchMore(){
        ... Go ahead and call your server 
    }
});