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

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

我有一个图像应用程序。它从服务器和数据库加载图像。当用户单击特定按钮应用程序时,使用 volley 向服务器发送带有发布数据(类别)的请求。 服务器然后获取发布变量并从数据库中获取数据并将数据作为 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 来完成,但我没有找到任何符合我的情况的脚本。

最佳答案

I know it can be done with AsyncTask

这是最糟糕的方法,千万不要这样做。

Android 现在提供了一个本地分页库 作为Android 体系结构组件 的一部分,使用它阅读更多信息 here .在这种方法之前,人们过去常常使用 onScrollChangedListener 设置为回收站 View ,您仍然可以使用它,但建议使用分页库。

关于android - 在 recyclerview 中显示来自服务器的数据时无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50888520/

相关文章:

android - 将文件上传到 asp.net 服务器的进度条

Android Java AsyncTask isCancelled 问题

android - android 跟踪 View 中显示的 'self' 是什么

ListActivity 中的 android-actionbar

mysql - 我如何实现具有两行的 RecyclerView,其中每行从不同的对话框(DIALOG_A,DIALOG_B)获取数据?

android - 中心选择水平 ScrollView 在 RecyclerView 上无法使用 Notifydatasetchange?

java - 从两个不同的 AsyncTask 更新一个 ListView

android - 可点击的图像 - 安卓

java - NfcAdapter 不可为空,但由@Provides @Singleton @javax.annotation.Nullable android.nfc.NfcAdapter 提供

java - android中recyclerview的分页