How to use RecyclerView according to device screensize?
Asked Answered
G

1

6

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

  • 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 to Retrofit to make another call till device screen filled.

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

Gui answered 26/10, 2015 at 12:34 Comment(11)
Is it an option to always get as many items as needed to fill the biggest screen? Like if xxxhpdi needs 50 items to fill screen, always fetch in batches of 50? Smaller screens will need to fetch data less often and bigger screens will have enough items to fill the view.Know
No, it's not possible . Endpoint will always return 10 item at a time.Gui
@TimCastelijns But what about If I make 5 request to xxxhdpi in sequence and load the data at a time?Gui
It's difficult to see how your question relates to android-layout, android-activity, or android-recyclerview. Are you asking about how to determine how many items will fit on-screen, given a particular layout/layout-manager and a device configuration?Bobbie
@Bobbie I was trying to explain it :) Yes, you are right I just wanted to know How many items do I need to fetch for what device to get fit on-screen. I don't have a layout it's just a RecyclerView with StaggeredGridView adapterGui
So I think you'll get better answers if you remove all the stuff from your question about API - at the end of the day, you just want a single number (at runtime): the number of elements that will fit in the viewport using a StaggeredGridLayoutManager.Bobbie
@Bobbie How can I get to know these number as per device screen?Gui
If you're using a StaggeredGridLayoutManager I don't think it will be possible to determine ahead of time (before you know the dimensions of each item view) how many items will fit on screen.Bobbie
@Bobbie What If I were using ListView or GridView. Will it possible then?Gui
The issue is that the item dimensions won't be known if you want to display a staggered grid. If the item dimensions are known for each item (or the dimensions are fixed), then you can calculate, given you know the column count and the dimensions of the viewport.Bobbie
I put it into a new questionGui
Q
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

Quantitative answered 26/10, 2015 at 13:26 Comment(4)
I am little bit confused here: Let's say I have two devices xhdpi and xxxhdpi. In xxxhdpi we can load 50 items in one display (without scrolling). According to your solution I will fetch first 50 items by making 5 request sequentially (I can do that because it is not too much time consuming) and If user try to scroll down then I will fetch next 50 items. But let's take a scenario for xhdpi who can only show 15 or 18 items at a time (Just giving a guess). How do I get to know that how many items do I need to fetch for what devices and every scroll I need to exact same amount of itemsGui
no when you fire all requests in separate threads ideally you want them to happen asynchronously in parallel you keep a counter for successful responses once it reaches 5 successful responses you refresh ur view with new data and reset counter. AND it doesnt matter what the screen size of the device is once the ADAPTER reaches to display like itemposition % 50-5 or something you just start requesting for the next 50 items and you can also show indeterminate progress dialog like most server side paginated list views doQuantitative
Finally i am going with your solution fetching 50 items in an every call. Can you please suggest me or give me any link for tutorial How to enable Load more functionality ?Gui
you can use http libraries like okhttp or ion etc that take care of doing network requests in their own threads. its pretty simple with a http libraryQuantitative

© 2022 - 2024 — McMap. All rights reserved.