Create an infinitely scrollable calendar view in Android

This is not a problem with the code, I interpret it as OK.

I'm learning a way to create an infinitely scrollable calendar view on Android, but I'm at a standstill.

My current dilemma is that most of these views have their children arranged in a repeating style relative to each other. By this I mean:

element 4 comes after paragraph 3, which comes after paragraph 2, and there is a constant addition / margin between all elements.

I need a way to create an infinitely long scrollable view that may or may not contain elements. Elements must be placed at different positions in the view. The best way I can describe a similar view is with a one-day calendar view that scrolls endlessly .

So far, my top two bets are using the new RecyclerView with a custom LayoutManager (this seems very complicated and still not fully documented by Google, though). I like this approach because, among other things, it is optimized to display large sets in a limited way.

My other solution would be to create a fully custom View . However, with this solution, I lose the adapter if I do not create a container view (which is probably more complicated than creating a layout manager).

How would you decide to solve such a problem? Hints are welcome, I do not need code examples, just ideas whose path is best to solve this problem.

Thanks.

Sorry if I misunderstood the recommendations

Edit: how I solved this problem My first decision to use RecyclerView with a special Decorator seemed promising, but it remained a β€œhack”, so we decided not to go for this solution, because we were afraid of the complications that it would create on the line.

To solve the problem, I went with SurfaceView instead of an adapter, this means having to rewrite all the adapter functionality for my SurfaceView, but it seemed to be the best way to solve this problem of very individual control of drawings and layouts for my use case.

It would be nice to create a custom Viewgroup that can handle such layout problems.

+5
source share
2 answers

ListView and ListAdapter are based on a fixed list, so the current endless scrollers just add more and more data to the end of the list.

But you need a scroller similar to the Google Calendar application, which has a bi-directional endless scroller. The problem with using ListView and ListAdapter in this case is that if you add data to the top of the list, the index of any one item changes so that the list jumps.

If you really think about it in terms of MVC, you will realize that the ListAdapter does not provide a model that is suitable for this need.

Instead of absolute indexing (i.e. 1, 2, 3, 4, etc.) what you really want is relative indexing, so instead of saying, β€œGive me the element at index 42,” you want to say β€œ here is the item, give me five points before that. " Or do you have something like a calendar date that is absolute; still - unlike your device memory; it’s not actually the beginning or the end, so what you really want here is a β€œwindow” to the data section.

The best data model for this would be a kind of double version of the queue, which is partly the LRU cache. You set a limit on the number of elements in the structure. Then, when the previous elements are loaded (the user scrolls), the elements on the back panel are discarded, and when new elements are added (the user scrolls down), the elements on the front panel are discarded.

In addition, you will have a threshold where, if you hit several elements of one edge of the structure, the loadNext event or the loadPrevious event will trigger and call the callback that you configured to enter more data to the edge of the structure.

So, once you understand that your model is completely different, you realize that even the RecyclerView will not help you here, because it is tied to the absolute indexing model. You need some kind of custom subclass of ViewGroup that processes the element views, such as ListView , but can adapt to a double-ended queue. And when you do a code search for something like this, there is nothing there.

Sounds fun. I will send the link when I start the project launch. (Unfortunately, this will not be done in a timely manner to help you right now, sorry.)

Something that may help you a little earlier: look at the implementation of Google Calendar and see how they did it: Google Calendar Git repo

+6
source

What you can find is a FragmentStatePagerAdapter , where you can implement a swiped view, which means when the user (for example) scrolls to the right, a completely new view is displayed.

Using the FragmentStatePagerAdapter , you can handle a huge number of views without memory overflow, since this particular PagerAdapter only supports view states and is explicitly designed to handle large sets of views.

By storing a calendar example, you can implement relocated navigation between weeks and generate weekly views on demand, storing, for example, only the year and week number as identifiers.

There are many online tutorials for Android, maybe you are looking at this one

0
source

Source: https://habr.com/ru/post/1216163/


All Articles