How to implement Android Multi-item listview like iOS UITableView - android

Now I am implementing chat screen in an Android App.
the use of the adapter pattern of ListView seems to be the best one cell.
You know you need a cell of a different type of user messages, photos, videos, dates, announcements, etc in chat screen.
Conventional methods that I know of is two things.
In getView() method, create each time a new type of cell.
First of all, put all the item and ,in getView() method, adjust the UI layout by VISIBLE/GONE options.
The second method is better i thought, but this method is still a waste of memory and complexity to implement.
For UITableView of iOS generate multiple queue by the number of type of cell. It is efficiently in Multi-Item ListView.
there any easy way to implement as iOS?

Use RecyclerView instead of ListView.
Create xml file for each item view. Create ViewHolder for each file xml.
Inside Adapter of RecyclerView override getItemViewType() return view type correspond with it's position. Override onCreateViewHodlerwhich return ViewHolder correspond with view type. Finally, override onBindViewHolder bind your data to your layout.

Consider using this library JSQMessagesViewController its very flexible and scalable.

Related

Why recyclerview is faster than listview [duplicate]

This question already has answers here:
Android Recyclerview vs ListView with Viewholder
(7 answers)
Closed 3 years ago.
I was reading about the difference b/w recyclerview and listview and found out that recyclerview is faster than listview.
I tried to search online but not found any satisfactory answer I know it is used ViewHolder pattern and Notifying adapter but what does it does intearlly so it is faster?
Recycler View you could say is an efficient way to create list of views.
If you have 1000 items like ur contact list , and If ur visible screen can show only 10 items at once, it will Create only 10+1 (or +2) Views and as u scroll , items/views that left will be reused (not create) to show new data.
Recycler View by default does this, where as List View by default doesn't do.
There are some differences between these two views.
ListView is a bit heavy and it has a lot of responsibilities. Whenever we have to handle the list, such as to configure it in some way, the only way to do this is through the ListView object or inside the adapter.
A lot of bad things in the ListView were fixed or changed in the RecyclerView. It’s more efficient by default, the layout is separated and we have more possibilities over the data set inside the adapter.
These are some crucial differences between ListView and RecyclerView:
1 ViewHolder
The ViewHolder pattern allows us to make our list scrolling act smoothly. It stores list row views references and, thanks to this, calling the findViewById() method only occurs a couple of times, rather than for the entire dataset and on each bind view.
The RecyclerView’s adapter forces us to use the ViewHolder pattern. The creating part (inflating the layout and finding views) and updating the views is split into two methods — onCreateViewHolder() and onBindViewHolder().
The ListView, on the other hand, doesn’t give us that kind of protection by default, so without implementing the ViewHolder pattern inside the getView() method, we’ll end with inefficient scrolling in our list.
2 LayoutManager
The LayoutManager takes responsibility for layouting row views. Thanks to this, RecyclerView doesn’t have to think about how to position the row view. This class gives us the opportunity to choose the way that we want to show the row views and how to scroll the list. For example, if we want to scroll our list vertically or horizontally, we can choose LinearLayoutManager. For grids, it is more suitable to choose the GridLayoutManager.
Previously, with the use of the ListView, we were only able to create a vertical-scrolling list, so it wasn’t that flexible. If we wanted grids on our list, we had to choose the other widget for that — GridView.
3 ItemDecoration
A duty of the ItemDecoration is simple in theory – add some decorations for the list row views – but in practice, it’s that simple to implement if we want to create a custom one. In this case, we should extend the ItemDecoration class and implement our solution. For example, the RecyclerView list has no dividers between rows by default and it’s consistent with the Material Design guidelines. However, if we want to add a divider for some reason, we can use DividerItemDecoration and add it to the RecyclerView. In case we use the ListView, we have to figure out rows decorations by ourselves. There is no helper class like ItemDecoration for this widget.
4 ItemAnimator
The last but not least component of RecyclerView that I want to mention is ItemAnimator. As we can expect, it’s handling row views animations like list appearance and disappearance, adding or removing particular views and so on. By default, RecyclerView’s list animations are nice and smooth. Of course, we can change that by creating our own ItemAnimator, which is also not that easy. To make it easier, we should extend the SimpleItemAnimator class and implement the methods that we need (just add animations to a ViewHolder’s views). To be honest, implementing animations on the ListView was a pain. Again, we had to figure out how we want to handle them.
5 Notifying adapter
We have a couple of cool notifiers on the RecyclerView’s adapter. We are still able to use notifyDataSetChanged() but there are also ones for particular list elements, like notifyItemInserted(), notifyItemRemoved() or even notifyItemChanged() and more. We should use the most appropriate ones for what is actually happening, so the proper animations will fire correctly.
Using ListView, we were able to use just notifyDataSetChanged() on the adapter and then had to handle the rest ourselves, again.
Because of ViewHolder Pattern.
Thats was the simplest answer. Now for some details.
What recycler view does is what it's name indicates "Recycle", yes it recycles items, and it does it with the help of ViewHolder Pattern.
By Using ViewHolder we do-not need to call findViewByID() every time we go through getView()method. The reference for all rows are stored in-memory. This increases the performance significantly, as findViewByID()is a heavy process.
Hope this clears your confusion.

RecyclerView vs Programmatically Created Views: Which one is better?

I want to put a list of items I am extracting from a firebase database. I was using RecyclerView for this but now I came to know that we can create Buttons, TextViews etc programmatically. I was wondering if there are any benefits of recyclerview or any special cases where it is preferable to use recyclerview.
Use RecyclerView, it is memory efficient. It does create view when it is visible and destroys it when it is not visible anymore, this way you can have infinite list without using a lot of resources.
Not bad thing to maintain view dynamically, But you need to know full concept of recycler view before taking the decision because there are lot's of aspect depend on when you are deciding to any component
Key aspects,
- Why you are using view?
- Is it sequential?
- All the data in the list are the same type?
- Also, How many records in the list if there is only tow-three record then no need to take the recyclerview.
Ultimately recyclerview is the optimized view for list out the item as a list,
Also, we suggest if your most of the data same type then take recyclerview don't do at dynamically, Because name self saying how recycler view (Recycle) will work

Android most efficient Table Layout

So I have a few arrays of data I would like to display in an activity without having like 15 text views with unique ids. Is there a code efficient way to make a Table layout or something like it where I could feed in data and it would automatically place it in there respective text views? Thanks!
I think you can achieve that by using RecyclerView (with a GridLayoutManager). Have a look at this answer.
If there are only TextViews and you don't want a specific layout you can use SimpleAdapter, if you want to modify the layout you have to extend RecycleView.Adapter (there is an example in the answer above).
You can add/remove items into/from a List and use DiffUtil that
can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.
There are a lot of tutorials about using this class. Have a look here or here.
Or you can use the notifyItemChanged() method:
If the list needs an update, call a notification method on the
RecyclerView.Adapter object, such as notifyItemChanged(). The layout
manager then rebinds any affected view holders, allowing their data to
be updated.
LE: There are some libraries available. Here is a list:
https://github.com/evrencoskun/TableView
https://github.com/HYY-yu/TableRecyclerView
https://github.com/Cleveroad/AdaptiveTableLayout
https://github.com/celerysoft/TableFixHeaders

Whats the best way to put a customized scrollable list in android studios?

I'm trying to make a android app with a scrollable list like this one...
When I made this app in iOS, I used a UICollectionView. What`s the best way to make this in android studios?
I tried using a list view, but I can't seem to customize it to my needs?
Any Ideas?
ListView was a great way to start and it is customizable to your needs.
However I would recommend to use RecyclerView which works almost on the same principle as ListView but it is a newer concept in Android. It handles the usage of the ViewHolder pattern for you which makes everything super easy.(With ListView, you would've had to implement your own ViewHolder pattern)
All you need to do is to have the RecyclerView in your activity/fragment as the view to hold your data. Then, the key component is to implement the RecyclerView's Adapter which will handle the inflation and setup of each list item.
Here is a really great and short tutorial to get you started with RecyclerView.
If you're done with that here is a bit more advanced video material on RecyclerView from Dave Smith explaining a lot of ways on how to understand and use RecyclerView in your apps.
A ListView fed by an ArrayAdapter of custom objects can do the job.
You might have a class that contains the text and the image of a single item.
Then you can create an ArrayList with items of that class, where you put in all the data.
This ArrayList can be used as dataset for a custom ArrayAdapter, which overrides the getView()-method.
The getView()-method must inflate the xml-layout for each item and attach the data from the ArrayList to the inflated view.
You might want to implement the Viewholder-pattern to achieve a good performance and avoid unnecessary inflations and getViewByID()-calls.
You can also use the new CardView, which can make it a lot easier, since it is made for the new material design in android 5, which looks similar to your item list.

Using a custom Adapter for multiple ListViews

I'm new to Android development and I was coding with ListView with a custom adapter. So far so good, then I wanted to use the same adapter with more than one listview, is that ok?
And when I override the method getView(), then I use the same resource to show the views (eg. R.id.show_view). Can I use different layouts in the same adapter? I don't know how to accomplish that.
I dont have the code here, sorry, it's more a question of whether it's a good practice to use the same adapter (eg. ArrayAdapter) to match various ListViews.
You can reuse class, but no instance of adapter. And you can create different views for every entry. But for sake of performance, you shall reuse view supplied to getView() whenever possible

Categories

Resources