I've been trying to make a refreshing and auto updaing list in my app. I use Loaders to get data for that list. I saw a guide for endless ListView here and it really makes your code neat. However, when i add SwipeRefreshLayout, ProgressBarand a view indicating empty list the code becomes a total mess and is not reusable at all. I was wandering if there is a nice way to implement this often used feature.
If you want to see my code, please ask in comments (But there's nothing significant in it..)
Related
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.
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.
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
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
I'm looking for any examples or (even better) tutorials that can help me accomplish this. But I keep coming up short in my google searches, which has me worried that it's not even possible. So that's my first question: Can an expandable listview inflate a fragment when it expands and call it's onDestroy when retracted? and if so, can you please link me to any help with accomplishing this. Thanks
Here's a little visual of what I'm looking to do:
first, the general UI paradigm could be handled with an ExpandableListView. whether this gives you enough to match exactly what you need is TBD.
as for having fragments inside the list ... theoretically, in the list view's adapter's getView() method, you could do a FragmentTransaction.replace(), passing in a container ID that exists in the inflated view. i have not tried this.
that being said, i seriously doubt this will work. list views are smart about managing views. the views in the list are often destroyed and re-created as the user scrolls the list. adding fragments to a view is an expensive operation.
as an alternative, you might consider a ViewPager, which can hold fragments by design. it's not the UI you have spec'd above, but it's a common UI pattern used by many stock Google apps, so you know it's been tested.