How to create multilevel recyclerview in Android kotlin? - android

I want to create 3 level RecyclerView like tree view in kotlin. Is there any tutorial and suggestions please let me know.
I already tried so many times with ExpandableListView and 3 RecyclerView, but didn't find any proper solution.

By a multi-level RecyclerView, do you mean a RecyclerView with paths to different lists that branch depending upon which item has been selected? If that's the case, I would honestly recommend using a single adapter to cycle through multiple lists depending upon user input.
If you have a root list containing two items, each of which opens up its own list with its own unique set of data, you can easily implement code that notifies the adapter of which item in the root list was selected. From there, the adapter can update and switch the view accordingly. This can be applied to series of lists ad nauseam if you so choose, though I can't say I would recommend this kind of method for incredibly complex webs of lists that interact with each other.
Like Ircover said in their comment, I don't think a tree is necessary in this situation either, if only because (1) as stated, it isn't really best practice to do so for the kind of application you're making, and (2) it may unnecessarily over-complicate whatever you're trying to achieve with these branching paths in the first place. If you're willing/able to provide more information about what you're trying to do here, that may help others help you more precisely than I can :)
Full disclosure here: the blog post linked above is not a direct match that will solve your problem - it pertains specifically to displaying different types of data sets (from data classes and what have you,) but employs code that shows how different sets of data can be switched between in a single RecyclerView. Even if it isn't a god-sent solution or is only halfway helpful in solving your problem, I think it can provide some useful information to you.

Maybe a slightly dated question but looking into the same concept and I located this page https://blog.usejournal.com/multi-level-expandable-recycler-view-e75cf1f4ac4b .
They have made a single adapter class to take care of all the navigation and so far seems to be the least complicated example of an expandable RecyclerView, though not in kotlin.

Related

Animated search in RecyclerView

I'm trying to create a way to search inside a RecyclerView with animating the items that have been searched (hide the ones that don't meet the condition, and add the ones that do, if they're not already in).
Of course, the simplest way to search a RecyclerView is just filter the list and notifyDataSetChanged(). But most developers know that this call recreates the adapter from scratch, almost like it was just assigned. Also, this call doesn't give any feeling of the items being searched, as all the items appear at once.
I've tried creating some way where the items get added or removed to the list as they meet/not meet the condition, but this loses the order of the items. Unless of course you search where the item is in the main list, and put it where it belongs. Which makes the searching longer since you're searching again every insert.
I looked into using a TreeMap, but trees can't be accessed by index, only by key.
I've also used SparseArray, the only problem was the SparseArray doesn't tell you in what index was the item inserted, you'll have to search for the item again (in order to notify the adapter).
Is there a recommended way of doing it? Some tutorial I have missed? Searching around I could only find the notifyDatasetChanged() solution.
I believe what you're searching for is DiffUtil.
Here's a well explained Medium article explaining it's usage.
Another interesting answer I've come across when looking into this is the SO Answer provided by Xaver Kapeller (Just search for SortedListAdapter in the page, although his answer is a very nice read imo). His SortedListAdapter library makes the code much more simpler and cleaner.

How do I implement staggered views in reading order?

Here is what I want to implement:
I'm trying to display definitions I'm getting from the web (right now they're coming from dummy data, but that's neither here nor there).
My first thought was to use a horizontal StaggeredGridView. There are two problems with this. The views are not laid out in the correct order and the span count is not set dynamically. Also, I found out that calling setSpanCount() in onLayoutChildren() causes an error at run time.
Of course, I could just have a LinearLayout and forego my attempt to recycle views. That would work in most cases, but if the user did happen upon a length definition, it would be very memory inefficient.
With that in mind, I resolved to subclass StaggeredGridLayoutManager. And this is where I need your help. Here are the resources I found in my own search:
Google's Implementation of a LinearLayoutManager
A Guide Implementing a Custom LayoutManager
Another Guide for Implementing a Custom LayoutManager
An Overview of a Few Principles to Follow when Implementing a Custom LayoutManager
I went through each one of these, but I cannot mentally separate out what parts are there for their specific use-cases and what parts are common to all custom implementations. If someone could be so kind as to point me in the direction of a resource or explain what all the methods I have to call in what order and for what purpose, I would be immensely grateful. Thank you!
Your first screenshot looks like a perfect case for Google's FlexboxLayout library. You can use the FlexboxLayoutManager with RecyclerView to get exactly what you showed.

What's the name of this Button?

Which is the name of the method/library which collapses/extends a list triggered by the user action?
Please see the image below to understand my use case.
As far I can see you have a List View with one Item, and on press you want to show a sublist. I guess you do not know Android terms and you are thinking from an abstract design point of view.
In more technical terms you need to expand a ListView when the user touches the item.
Here you can find a tutorial to use ExpandibleListVIew
Please consider that nowdays is really used in place of ListView a library called RecyclerView, and here and maybe even better this stack overflow post where you can find a valid method that explain your UX use case, without recurring to external libraries but capitalizing the method onBindViewHolder, as allegedly recommended at a Google I/O official conference.
RecyclerView offers much more functionalities, animations and should be the best option to go if the app increases in complexity, namely a real life scenario.
you can use expandable recyclerview
eg
Expandable recycler view

Building an infinitely scrollable calendar-like view in Android

This is not a code problem, I interpret the guidelines as that being OK.
I've been researching a way of building an infinitely scrolling calendar-like view in Android, but I've reached an impasse.
Right now my dilemma is that most of the similar views available have their children placed relative each other in a recurring style. With this I mean:
item 4 comes after item 3, which comes after item 2, and there is constant padding/margin between all items.
What I need is a way to produce an infinitely long scrollable view that may, or may not, contain items. The items should be placed at variable positions within the view. The best way I can describe a similar looking view is a one-day calendar-like view that is infinitely scrollable.
So far my best two bets are using the new RecyclerView with a custom LayoutManager (this seems very complex and still not perfectly documented by Google though). I like this approach because, among other things, it is optimized for displaying large sets in a limited view.
My other solution would be to build a completely custom View. However, with that solution I loose the adapter unless I build a container view (which is probably more complex than building a layout manager).
How would you go about solving such a problem? Tips are appreciated, I don't need code examples, just ideas which path is the best to solve this problem.
Thanks.
Apologies if I've misunderstood the guidelines
Edit: How I resolved this problem
My first solution to use RecyclerView with a special Decorator seemed promising, but it remained a "hack" so we decided not to go for that solution since we were afraid of the complications that it would create down the line.
To solve the problem I went with a 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 of solving this issue of very custom drawing and layout managing for my use-case.
It still would be nice to build a custom Viewgroup that can handle this kind of layout problems.
ListView and ListAdapter are based on a fixed list, so the current infinite-scrollers just keep adding more and more data to the end of the list.
But what you want is scroller similar to Google's Calendar app which has a bi-directional infinite scroller. The problem with using ListView and ListAdapter in this case is that if you add data to the front of the list, the index of any one item changes so that the list jumps.
If you really start thinking about this from the MVC perspective, you realize that ListAdapter does not provide a model that fits this need.
Instead of having absolute indexing (i.e. 1, 2, 3, 4, etc), what you really want is relative indexing, so instead of saying "Give me the item at index 42" you want to say "here's an item, give me the five items before it". Or you have something like a calendar date which is absolute; yet — unlike your device's memory — it has effectively no beginning or end, so what you really want here is a "window" into a section of that data.
A better data model for this would be a kind of double-ended queue that is partly a LRU cache. You place a limit on the number of items in the structure. Then as prior items are loaded (user is scrolling up) the items at back end are pushed off, and when subsequent items are added (user is scrolling down), items at the front are pushed off.
Also, you would have a threshold where if you got within a few items of of one edge of the structure, a "loadNext" or "loadPrevious" event would fire and invoke a callback that you set up to push more data onto the edge of the structure.
So once you've figured out that your model is completely different, you realize that even RecyclerView isn't going to help you here because it's tied to the absolute indexing model. You need some sort of custom ViewGroup subclass that recycles item views like a ListView, but can adapt to the double-ended queue. And when you search code repos for something like this, there's nothing out there.
Sounds like fun. I'll post a link when I get a project started. (Sadly, it won't be done in any timely manner to help you right now, sorry.)
Something that might help you a little sooner: look at Google's Calendar implementation and see how they did it: Google Calendar Git repo
What you may be searching for is a FragmentStatePagerAdapter , where you can implement a swiped view, meaning when the user (for example)swipes to the right, a completely new view is displayed.
Using a FragmentStatePagerAdapter , you can handle a huge amount of views without overflowing the memory, because this specific PagerAdapter only keeps the views' states and is explicitly meant to handle large sets of views.
Keeping your example of a calendar, you can implement swiped navigation between for example weeks and generate the week views on demand while only keeping for example the year and the week's number as identifiers.
There are plenty of online tutorials for Android, maybe you have a look at this one

An efficient way to display a lot of items in Android

I would like to know what is the best and most efficient way for both the user and developer(me) to display more than 100 items. Right now I have them all in a ExpandableListView. Every group carry's 11 child items(there are 10 groups). This is not being pulled from any database and is all static information(images textview's). I'm trying to design this so the user can get to information quickly. If anyone knows a better way to do this, please share.
So it sounds like you have a pretty simple set of data you wish to show the user. And seeing that you need to show 100 or such items, there's not many options when it comes to efficiency for both user and you.
ExpandableListView is def a good choice. It'll allow the user to select which portion of the data to view at a time. It's not hard to get one rolling either. Android provides a very basic SimpleExpandableListAdapter which is good for displaying static data. The most difficult part in working with it is assembling your data into the format it requires.
Another alternative is some sort of Tabbed display. Selecting each tab would load a new ListView of data. Basically the tabs here would be equivalent to the groups in the ExpandableListView. This could allow for showing more items within each grouping because the tabs would only span one row across the screen horizontally. You could implement different ways but here's a link which introduces and walks you through one. From the user perspective, this approach is merely a change in look and feel really...it's a bit more involved for the programmer to implement.

Categories

Resources