user5060176
user5060176

Reputation:

How to use RecyclerView according to device screensize?

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 staggeredGridViewin 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

Is it good approach? If yes then How can I achieve it?

Upvotes: 6

Views: 753

Answers (1)

Bhargav
Bhargav

Reputation: 8277

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

Upvotes: 1

Related Questions