我有一个API响应,我需要在listView上显示。一开始它以15个项目开始,我想在用户向下滚动并到达列表末尾时添加更多项目。 使用Retrofit自动翻页API的最佳策略是什么,以便默认下载所有可用数据?不幸的是,我是初学者,这是我能得到的。请帮忙!任何帮助将不胜感激。
这些是我需要使用的响应中的参数:
"total": 181,
"per_page": "15",
"current_page": 1,
"last_page": 13,
"next_page_url": "URL",
"prev_page_url": null,
"from": 1,
"to": 15,
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://nmc.no-ip.org:8888/")
.addConverterFactory(GsonConverterFactory.create())
.build();
StreamingOfferService service = retrofit.create(StreamingOfferService.class);
Call<ResponseResult> response = service.getStreamingOffers();
response.enqueue(new Callback<ResponseResult>() {
@Override
public void onResponse(Call<ResponseResult> result, Response<ResponseResult> response) {
createList(response);
}
@Override
public void onFailure(Call<ResponseResult> result, Throwable t) {
}
});
}
public void createList(final Response<ResponseResult> response)
{
final ListView listview = (ListView) findViewById(R.id.listview);
final StreamingAdapter adapter = new StreamingAdapter(this,R.layout.listofusers,response);
listview.setAdapter(adapter);
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
ResponseResult r = new ResponseResult();
int currentPageNumber = r.getCurrentPage();
boolean loading = true;
String next_page_url = r.getNextPageUrl();
if (listview.getLastVisiblePosition() == listview.getAdapter().getCount() - 1
&& listview.getChildAt(listview.getChildCount() - 1).getBottom() <= listview.getHeight()) {
// r.getNextPageUrl();
// currentPageNumber++;
loading = true;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(r.getNextPageUrl()) // this is not working
.addConverterFactory(GsonConverterFactory.create())
.build();
StreamingOfferService service = retrofit.create(StreamingOfferService.class);
Call<ResponseResult> response = service.getStreamingOffers();
response.enqueue(new Callback<ResponseResult>() {
@Override
public void onResponse(Call<ResponseResult> result, Response<ResponseResult> response) {
// StreamingAdapter adapter = new StreamingAdapter(this,R.layout.listofusers, response);.
}
@Override
public void onFailure(Call<ResponseResult> result, Throwable t) {
}
});
}}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
适配器:
public class StreamingAdapter extends BaseAdapter {
private Context context;
private int resource;
private LayoutInflater inflater;
private List<StreamingOffer> streamingOfferList;
public StreamingAdapter(Context context, int resource, Response<ResponseResult> response) {
this.context=context;
this.resource=resource;
this.inflater=LayoutInflater.from(context);
streamingOfferList = response.body().getData();
}
void addToList(List<ResponseResult> updatedlist)
{
this.streamingOfferList.addAll(updatedlist); // how do I add the new response here
this.notifyDataSetChanged();
}
public class Holder extends User {
private TextView full_name;
private TextView location;
private TextView price;
private TextView currency_symbol;
private TextView total_streamed;
private ImageView offer;
private ImageView profile;
private ImageView iconUser;
private ImageView iconStar;
private ImageView iconStar1;
private ImageView iconStar2;
private ImageView iconStar3;
private ImageView iconStar4;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View itemView = convertView;
StreamingOffer streamingOffer = getItem(position);
User u = streamingOffer.getUser();
if(itemView==null) {
itemView = inflater.inflate(resource, parent, false);
holder.full_name = (TextView) itemView.findViewById(R.id.full_name);
holder.full_name.setText(u.getFullName());
holder.location = (TextView) itemView.findViewById(R.id.location);
holder.location.setText(streamingOffer.getLocation());
holder.price = (TextView) itemView.findViewById(R.id.price);
holder.currency_symbol = (TextView) itemView.findViewById(R.id.currency_symbol);
if(streamingOffer.getIs_free()==0)
{
holder.price.setText(streamingOffer.getPrice());
holder.currency_symbol.setText(streamingOffer.getCurrencySymbol());
}
else
holder.price.setText("FREE");
holder.total_streamed = (TextView) itemView.findViewById(R.id.total_streamed);
holder.total_streamed.setText(u.getTotalStreamed());
holder.offer = (ImageView) itemView.findViewById(R.id.offer);
holder.profile = (ImageView) itemView.findViewById(R.id.profile);
holder.iconUser = (ImageView) itemView.findViewById(R.id.iconUser);
holder.iconStar = (ImageView) itemView.findViewById(R.id.iconStar);
holder.iconStar1 = (ImageView) itemView.findViewById(R.id.iconStar1);
holder.iconStar2 = (ImageView) itemView.findViewById(R.id.iconStar2);
holder.iconStar3 = (ImageView) itemView.findViewById(R.id.iconStar3);
holder.iconStar4 = (ImageView) itemView.findViewById(R.id.iconStar4);
Images im = new Images();
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
String jsonRepresentation = gson.toJson(im);
if(jsonRepresentation.contains("75x75")){
Picasso.with(context).load(im.getImage_75x75()).into(holder.profile);
}
//Picasso.with(context).load(jsonRepresentation).into(holder.offer);
Picasso.with(context).load(im.getImage_154x154()).into(holder.profile);
Picasso.with(context).load(im.getImage_154x154()).into(holder.offer);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar);
Picasso.with(context)
.load(R.drawable.half_red_star).into(holder.iconStar1);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar2);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar3);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar4);
// Picasso.with(context)
// .load("http://nmc.no-ip.org:8888/assets/images/offer-2.png").into(holder.offer);
// Picasso.with(context)
// .load("http://nmc.no-ip.org:8888/assets/images/profile-1.png").into(holder.profile);
if(u.getIs_online()==1) {
Picasso.with(context)
.load(R.drawable.icon_user_online).into(holder.iconUser);
}
else
Picasso.with(context)
.load(R.drawable.icon_user_offline).into(holder.iconUser);}
if(u.getRating()!=null) {
if (u.getRating() == "1.0" && u.getRating() == "1.5") {
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar1);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar2);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar3);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar4);
}
if (u.getRating() == "2.0" && u.getRating() == "2.5") {
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar1);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar2);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar3);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar4);
}
if (u.getRating() == "3.0" && u.getRating() == "3.5") {
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar1);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar2);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar3);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar4);
}
if (u.getRating() == "4.0" && u.getRating() == "4.5") {
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar1);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar2);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar3);
Picasso.with(context)
.load(R.drawable.icon_black_star).into(holder.iconStar4);
}
if (u.getRating() == "5.0") {
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar1);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar2);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar3);
Picasso.with(context)
.load(R.drawable.icon_red_star).into(holder.iconStar4);
}
}
itemView.setTag(holder);
return itemView;
}
@Override
public int getCount() {
return streamingOfferList.size();
}
@Override
public StreamingOffer getItem(int position) {
return streamingOfferList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}