Issue
I have a image app. It load images from server and database. When user click on specific button app send a request with post data(Category) to server using volley.
Server than get post variable and fetch data from database and send data as a json response. Then i show images in 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);
}
My PHP script -
$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;
There are many images in database and if i not set limit then server takes time to fetch data. It make app slow .
I have a website where i use pagination. In pagination i use LIMIT to show data.
Now i want that when user click on button only first 10 images from database get load and after user scroll the recyclerview
another 10 images should load and continue.
I know it can be done with AsyncTask
, but i don't find any script that match with my situation.
Solution
I know it can be done with AsyncTask
This is the worst approach ever don't do it.
Android now provides a native paging Library as part of the Android architecture components, use that read more about it here. Before this approach people used to do it using onScrollChangedListener
set to the recycler view you can still use that but the paging library is recommended.
Answered By - Aniruddha K.M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.