问题
I have a backend API endpoint which returns JSON
in some sets. Each set contain 10 lines of Json
.
For example:
curl http://www.something.com?getData=0
will give first 10 elements
and curl http://www.something.com?getData=1
will return next set and so on.
I am using RecyclerView
and StaggeredGridView
to load data from given endpoints
. Right now I am only fetching first set and it's working perfectly fine.
What I am looking for?
How can I load data in RecyclerView
as per screen sizes in Android
. FOr example:
Let's say device hdpi
can able to load 25 staggeredGridView
in RecyclerView
. So , here the endpoints
request in sequence:
http://www.something.com?getData=0 // will fetch 0-10
http://www.something.com?getData=10 // will fetch 11-20
http://www.something.com?getData=10&skip=5 // will fetch 21-25
How can I make above process dynamically. So that it will work for all screen sizes?
BTW this is how I am loading the data into StaggeredGridView
adapter:
mRecyclerView = (RecyclerView) mActivity.findViewById(R.id.staggering_grid);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
mAdapter = new StaggeredGridAdapter(mContext);
mAdapter.addItems(response);
mRecyclerView.setAdapter(mAdapter);
Possible Solution
- If I got to know that How much
Grid
I need to fill according to device then I can simply make a request till I get the data to fill expected number. - I can also use
Event Bus
to send notification toRetrofit
to make another call till device screen filled.
Is it good approach? If yes then How can I achieve it?
回答1:
what you can do is load data in excess say 50 at a time instead of 10, that way you have enough data to populate the whole display, if your api only lets you get 10 items per request, then you fire multiple requests from different threads and add them to your adapter, and refresh the view once all the requests were done successfully. So this way you wont be needed to request for all the items and have enough items for display in any device screen setting
来源:https://stackoverflow.com/questions/33345909/how-to-use-recyclerview-according-to-device-screensize