我想使用Marvel API和Retrofit制作一个应用程序。收到响应后,它只会向我显示前10或20个结果,这是API的基本限制。如何滚动到屏幕底部,以便加载更多数据?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private MyAdapter myAdapter;
private RecyclerView myRecyclerView;
private RequestManager glide;
Date today = new Date();
int ts = 1;
private static final String API_KEY = "c05b97de2b2e597428a65451d0df12a7";
private static final String HASH = "9e36e240cadffa03ecaa206887b23335";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.glide = glide;
//Create a handler for the RetrofitInstance interface//
GetData service = RetrofitClient.getRetrofitInstance().create(GetData.class);
Call<MarvelResponse> call = service.getAllCharacters(Long.valueOf(ts), API_KEY, HASH);
call.enqueue(new Callback<MarvelResponse>() {
@Override
//Handle a successful response//
public void onResponse(Call<MarvelResponse> call, Response<MarvelResponse> response) {
loadDataList(response.body());
}
@Override
//Handle execution failures//
public void onFailure(Call<MarvelResponse> call, Throwable throwable) {
//If the request fails, then display the following toast//
Toast.makeText(MainActivity.this, "Unable to load users", Toast.LENGTH_SHORT).show();
}
});
}
//Display the retrieved data as a list//
private void loadDataList(MarvelResponse usersList) {
//Get a reference to the RecyclerView//
myRecyclerView = findViewById(R.id.myRecyclerView);
myAdapter = new MyAdapter(usersList.getData().getResults());
//Use a LinearLayoutManager with default vertical orientation//
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
myRecyclerView.setLayoutManager(layoutManager);
//Set the Adapter to the RecyclerView//
myRecyclerView.setAdapter(myAdapter);
}
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.CustomViewHolder> {
private static final String TAG = MyAdapter.class.getSimpleName();
private List<MarvelResults> dataList;
public MyAdapter(List<MarvelResults> dataList){
this.dataList=dataList;
}
class CustomViewHolder extends RecyclerView.ViewHolder{
//get a reference to the Views in our layout
public final View myView;
TextView textCharacter;
TextView textDescription;
ImageView imageView;
LinearLayout linearLayout;
CustomViewHolder(View itemView){
super(itemView);
myView = itemView;
textCharacter = myView.findViewById(R.id.character);
textDescription = myView.findViewById(R.id.description);
imageView = myView.findViewById(R.id.imageView);
}
}
//construct a RecyclerView.ViewHolder
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.row_layout, parent, false);
return new CustomViewHolder(view);
}
@Override
//set the data
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.textCharacter.setText(dataList.get(position).getName());
holder.textDescription.setText(dataList.get(position).getDescription());
Uri uri = Uri.parse(dataList.get(position).getThumbnail());
Glide.with(holder.imageView.getContext()).load(uri).into(holder.imageView);
Log.d(TAG, "onBindViewHolder() called with: holder = [" + uri + "], position = [" + position + "]");
}
//calculate the item count for the RecylerView//
@Override
public int getItemCount() {
return dataList.size();
}
}
对不起,我是初学者。
谢谢!
这是Marvel Api文档的link。
答案 0 :(得分:0)
我认为您需要在请求中使用offset
和limit
参数。例如,您可以在每个请求中将offset
增加20,并在limit
0处设置offset
在20上,并在每次到达底部时将其增加。因此,每个请求都应返回下20个项目。
在这里看看:https://developer.marvel.com/documentation/apiresults
要检测何时RecyclerView
到达底部,可以使用addOnScrollListener
这样的东西:
private int firstVisibleItem, visibleItemCount, totalItemCount;
private boolean loading = true;
private int offset = 0;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = layoutManager.getItemCount();
firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (visibleItemCount + firstVisibleItem)
>= totalItemCount) {
// End has been reached
offset += 20;
// send request to load more videos here
loading = true;
}
}
});