Android Listview with multiple views - android

I am working on a Android project having a list view where every row shows up like a card. Hence I created separate views for each of its row.
How do I handle these multiple views in adapter Class?
Is there any code design pattern that help separately handling these views?
Thank in advance

you should use typeCount inside your adapter. here is android documentation

Your adapter needs to implement getItemViewType() and getViewTypeCount() methods to let listview handle different types of layouts per row properly. See docs: http://developer.android.com/reference/android/widget/BaseAdapter.html

I would use a recycler view. A good example of one is here

Related

How to implement a recycler view in Kotlin that can handle cells of different sizes?

I'm a swift developer and I was able to build this layout very easily using a UICollectionView. However, I'm struggling to do the same in Kotlin. I've been trying to use a Recycler View to do this but it's not working out. Any advice on how I can do this would be highly appreciated.
I can help you with a few suggestions as to how I would be making this layout. Check if it's of any help:
First, follow this post for implementing multiple viewtypes in your recyclerview by overriding getItemViewType() method in your recyclerview adapter. You can set the item's viewtype based on its position in the datalist or a value in your datasource etc. Based on this viewtype returned you can set what your viewholder should look like in the adapter's onCreateViewHolder() and the funtionality in onBindViewHolder() method.
Next your recyclerview will need to make use of a GridLayoutManager with vertical orientation and 2 columns.
With these 2 steps you can have multiple views in different columns as you require, BUT in order to have a viewholder occupy whole width like the 2nd row in your sample image you will need to make use of setSpanSizeLookup() method of your grid layout manager. You can use this post here for reference.
Basically this is all you need to achieve your layout. Make use of multiple viewtypes for recyclerview with a gridlayout manager having a custom spansize lookup.
The references i mentioned are mostly in java but converting them to kotlin should be fairly easy. Have a look and let us know if this helps.

I want to load different layouts with fragments in recyclerview. is that possible?

I want to a load different layouts with fragments in recyclerview. is that possible? if possible is that a good practice to use different layout fragments in recyclerview. I have idea of like using different card layouts in Recyclerview by overriding the getItemViewType() method from RecyclerView Adapter. Could anyone provide a sample for loading different layouts using fragments as row items in RecyclerView android
Thanks in advance
I want to load different layouts with fragments in recyclerview. is that possible?
of course it is possible, but a bad practice. Instead try to use child layouts.
I have idea of like using different card layouts in Recyclerview by overriding the getItemViewType()
ya, this is the right place to get started. cheers (Y)
You can try to define a item layout for your recyclerview with a FrameLayout as fragment container.
You will need a reference to your fragment manager in your adapter , set with a function or put into the adapter constructor.
In the onBindViewHolder function of your adapter you manage which fragment should be created in function of the position.
This is a suggestion at your issue but it is against good practice.
Hope this helps.
Sorry for my english .

Staggered collections in Android app

Hi I just watch the last video of Android Design in Action: Collections and I would like how do you approach the following view:
The first thing that I though was use a GridView, but how do you achieve the first item fill two items size?
The other solution that I think is use a normal ListView, but in each row return two items view. And for the first row return a single item.
What do you think about these two approach? There are a better options?
UPDATE:
I want to inflate the collection dynamically from a Cursor or a List.
Check out the link..
http://www.androidviews.net/2013/01/pinterest-like-adapterview/
in that link have Staggeredgridview example with source code try that..
https://github.com/maurycyw/StaggeredGridViewDemo
Not exactly the same scenario, but you may find this post useful.-
Create gridlayout.
Let me help you my friend. Use the following library https://github.com/Utopia-Developers/StaggeredGridView Its an implementation of StaggeredGridView that allows you to set column span on each view in the getView() method of your adapter.

Implement a list with section divider in android

Hi how can i implement a list with section divider like the one on android's building blocks lists
Can you point any articles or tutorials to achieve this?
I implemented a possible version here:
http://steprobe.wordpress.com/2013/03/29/google-building-blocks-style-listview-for-android/
There are many tutorials to get grouped lists. The trick is to look for "adapter" and not "list". For instance, this one: http://android.cyrilmottier.com/?p=440
Each row in your list can be totally separated in terms of layout from others. So if you got 3 rows on your list, you can have each one can look totally different. You need to write your own adapter (i.e. extending ArrayAdapter), override getView(), getViewTypeCount() and getItemViewType(). Then for each row your getView() shall do any logic you want, inflate any layout you want and return that View to the 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