我在应用程序中使用它向片段输出json RecyclerView
,此时我RecyclerView
正在加载所有json,我该如何组织分阶段加载?
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();
}
});
这是我的适配器
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 上挂一个滚动监听器,当它滚动到最后一个元素时,加载一条新数据。
方法 2:
@Override public void onViewAttachedToWindow(final ViewHolder holder) { super.onViewAttachedToWindow(holder); int layoutPosition = holder.getLayoutPosition(); }
在适配器中,您可以获取新出现的列表项的位置。如果出现的元素的位置是最后一个,那么是时候加载新的一部分数据了