I use applications for output json in the RecyclerView fragment, at the moment my RecyclerView loads all json, how do I organize a phased download?

  JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { Log.d(TAG, response.toString()); hidePDialog(); // Parsing json for (int i = 0; i < response.length(); i++) { try { JSONObject obj = response.getJSONObject(i); Person person = new Person(); Log.d("json",obj.getString("image")); person.setId(obj.getString("id")); person.setUrl(obj.getString("image")); person.setName(obj.getString("name") + obj.getString("1")); person.setNumberClient(obj.getString("number")); person.setName2(obj.getString("name")); person.setSurname(obj.getString("surname")); person.setRegion(obj.getString("region")); person.setSubject(obj.getString("1")); names.add(person); } catch (JSONException e) { e.printStackTrace(); } } // notifying list adapter about data changes // so that it renders the list view with updated data hidePDialog(); adapter.notifyDataSetChanged(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); hidePDialog(); } }); 

here is my adapter

 public class MyPicassoAdapter extends RecyclerView.Adapter<MyHolder> { Context c; ArrayList<Person> persons; public MyPicassoAdapter(Context c, ArrayList<Person> persons) { this.c = c; this.persons = persons; } @Override public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_items,parent,false); MyHolder holder=new MyHolder(v); return holder; } @Override public void onBindViewHolder(MyHolder holder, int position) { holder.nameTxt.setText(persons.get(position).getName()); holder.id.setText(persons.get(position).getId()); holder.img_url.setText(persons.get(position).getUrl()); holder.region.setText(persons.get(position).getRegion()); holder.subject.setText(persons.get(position).getSubject()); holder.numClient.setText(persons.get(position).getNumberClient()); holder.ns.setText(persons.get(position).getName2() + " " + persons.get(position).getSurname()); PicassoClient.downloadImage(c, "http://www.developer92.16mb.com/mentor/public_html/images/"+persons.get(position).getUrl(), holder.img); } @Override public int getItemCount() { return persons.size(); } public interface ClickListener { void onClick(View view, int position); void onLongClick(View view, int position); } 
  • RecyclerView cannot download all the data transferred to it at once, unless it is immediately placed on the device screen. Download the code, otherwise your problem will be difficult to understand. - ZigZag
  • It can load data all at once. The screen size is limited by the number of viewers that will be displayed in it. The author wants the data to also be downloaded portions - P. Ilyin
  • look please added code - OPTIMIST .KZ
  • Yes, I want json to load in batches, not entirely - OPTIMIST .KZ
  • one
    And here's another tip. If you execute repeated code several times in the same scope (for example, your repeated call to persons.get(position) in the onBindViewHolder method), then it is better to put this code into a separate variable. The code will be cleaner, there will be less potential errors. To quickly do this, copy the repeating construct and paste it before its first call. Move the carriage to this design, press alt + Enter. In the drop-down list, select "introduce local variable", then "replace all N occurrences". Fast, comfortable, beautiful - P. Ilyin

1 answer 1

Method number 1:

Hang a scroll listener on RecyclerView and, when it is scrolled to the last element, load a new piece of data.

Method number 2:

@Override public void onViewAttachedToWindow(final ViewHolder holder) { super.onViewAttachedToWindow(holder); int layoutPosition = holder.getLayoutPosition(); }

In the adapter, you can get the position of the list item that just appeared. If the position of the element that appears is the last, then it’s time to load a new piece of data.

  • You can look at the code pzh - OPTIMIST .KZ
  • Thank you all figured out, right now later, I can throw off someone else will need - OPTIMIST .KZ