I want to show diffrent view in single Recycler view
How can i do this.
In Kotlin
Your picture took from the article that describes how to merge 3 adapter into one recyclerView. So you can read it first.
You need to use a Recylerview view with Multiple View types.
Each View Type will have its own ViewHolder, all of them having the same adapter.
Have a look at the below article.
https://blog.mindorks.com/recyclerview-multiple-view-types-in-android
Related
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 am already familiar with the basics and am pretty close to what I want to achieve. I want to have two different layouts in total for the rows but don't want it to be a set pattern of alternation. There could be any pattern for the two row layouts.
My question is how do I use two different xml layouts depending on the information that is going to be displayed? Or if possible, use a single layout and work with visibility for simplicity (current implementation).
Is it possible to send another parameter into getViewItemType()?
There is no way you can set another parameter into getItemViewType (int position) as it only takes one parameter and returns the view that is created by getView(int position, View convertView, ViewGroup parent) on a specific item.
I wolud recommend you using the setVisibility() on a specofic xml Layout which you don't want to display.
Or you can just maintain a small SQliteDatabase for storing what all the items you want to make it visible and call the adapter according to that value in the database.
Hope this helps.
In android adapter any time you can call "getItem()" and with help of that you can decide which layout to show.
Don't forget to override getItem() in your adapter class.
I have a recyclerView with two cardView one with plain text and the other with a list of data which is not specified. I have no problem creating two different view holder and set one single text data on first. The problem is how to load a list inside the 2nd cardView. Is it possible to create a list view adapter inside a recyclerView adapter ??
You should implement Custom LayoutManager for that. Refer this answer for an example and explanation of how to do that
Please post your code so that we can better understand your issue.
You can place a recycler view inside the second card view. And you can override getItemViewType() in adapter to handle the two card views.
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
I was wondering how to show the snapshot of my notes, checklists, images in the grid view. I have no trouble creating the grid view but creating a preview of it is the challenge I'm facing right now as I need to add different type of views(text, checklist, imageview) to the grid according to their time of creation. Works fine for a single type of view like image or text only.
Or in other words. How do I add the different views dynamically to the GridView Adapter?
All or any help is appreciated.
Thanks.
They surely do not use a GridView for google Keep - but probably an Adapter nonetheless.
To use different Views with your Adapter you can use different View types:
Override getViewTypeCount() to return the number of View types you support.
Return the appropriate type in getItemViewType(int position) for this specific item.
Create the correct View in getView(int position, View convertView, ViewGroup parent).
Keep in mind that the rules for recycling, ViewHolder and so on still apply. For multiple types recycling only happens for the correct type.
Marcus Körner shared a gist by Jake Wharton on G+ recently that might be useful.
More than likely Google is using something similar to GridLayout, if not actually using a GridLayout.
Chances are the layout is also created programmatically to allow for the widgets to be different sizes based on the content that they each display.
Take a look at this recycle view with stagged
This manage make all what you need StaggeredGridLayoutManager
or look at this sorce Heterogenous Layouts inside RecyclerView
recyclerView= (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new
StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
initData();
MasonryAdapter adapter=new MasonryAdapter(productList);
recyclerView.setAdapter(adapter);
SpacesItemDecoration decoration=new SpacesItemDecoration(16);
recyclerView.addItemDecoration(decoration);