Different Layout Managers in an Activity - android

I have two different kinds of data that one of them needs to be displayed in a GridLayout (Red) and the other needs to be displayed LinearLayout(Blue).
The whole page scrolls and the data sources may change so the views should update dynamically.
How can I implement this in Android and probably with RecyclerView?

One way is to only use the GridLayoutManager with 2 Columns.
In case a listitem should wrap both columns set the columnspan for the item to 2 otherwise to 1.

Related

RecyclerView/StaggeredGridLayoutManager: Force group of consecutive views into the same span/column

Currently, I'm using a RecyclerView to display a set of views with different view types, where a group of consecutive items belongs together: a header, a variable set of content items, and a separator/footer.
Now I also want my UI to look good on bigger devices, aka use multiple panes, so I switched to a StaggeredGridLayoutManager. The problem here is that it spreads views evenly between all spans, means one group gets spread over multiple columns.
Thus, I would like to specify that the layout manager puts a consecutive set of views into the same span, and only then put the next group in the next one.
Is this somehow possible with the LayoutManager I use? Can I modify it to work like that without just duplicating and editing it? Support lib devs: are there any plans to introduce such behaviour?
Please tell me whether I should provide any other information, and when something isn't clear enough.
You can achieve your desired layout with the Support Library's StaggeredGridLayoutManager; you need to make each 'group' of items (e.g. header1 + 1-3 + footer) a single view returned by your adapter. This view could itself be a RecyclerView.
That is you'll have one outer RecyclerView with a StaggeredGridLayoutManager and multiple inner RecyclerViews with LinearLayoutManagers. You can likely share a single RecycledViewPool between the inner RVs. You'll have to do some gymnastics to create appropriate adapters for the inner RVs but this is likely simpler than writing your own LayoutManager!!

Should I use GridView or RecyclerView?

I need to create a view where products are compared (min 2, max 5). I had two thoughts:
Create a RecyclerView and each column would be a different item. On init I would have to set number of colums. The bad part is that if one item has more text and would go on another line, the whole column would move down, but others won't.
Create a gridView, but I would have to hardcode or create more cases for every amount of products.
Is there any suggestion on how to implement this view in a better way?
What you need is a GridLayout:
http://developer.android.com/reference/android/widget/GridLayout.html
Here's a useful tutorial:
http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources
A GridView however is completely unsuitable for your needs. It can only show equal width items that overflow to the next row when the width of the GridView has been filled.

How to display more listview items in the same line?

I would like to display 2 listview items in the same line, like favourite contacts in the default phone app. I do not need a full working solution, but only a guideline what to do.
Illustration:
There is a list of favourite contacts and there are 2 contacts (illustrated only by pictures) in 1 line.
Thanks!
You can write your own adapter to handle this, or you can use a GridView.
To use the custom Adapter strategy:
Create a row layout that has space for two elements.
In xml, for whatever you now consider one 'row', make a horizontal LinearLayout that can hold two of them next to each other. That's your new definition of a row.
In your Adapter, fill each row with two elements from your data set instead of the usual 1.
Take care to handle the case where the data set only has one element left.
Your alternative is to use a GridView,

Irregular Gridview, first row with one colum, rest id the rows with two columns

I'm trying to implement an irregular gridview for my Android app. I've defined the gridview to show 2 columns but I need to show just 1 column at the first row. Is it possible using a DataAdapter?
I don't think you can accomplish this with a gridview. The adapter simple provides the data, but the view decides how to lay it out. If your requirements allow you to make the number of columns constant, then maybe you can use the gridlayout instead. I'm thinking you could try to do you layout like the google currents app.
Gridview and ListView are super useful if you have 1000's of items since it reuses views as it scrolls. If you have lots a items in your grid, then I would probably try to use the GridView or ListView to accomplish your goal. Maybe you have to get your requirement changed. Another option is to use a ListView, but sub-divide each row into 2 columns.

Image in a custom List Android

I have created a custom adapter to display a list, there is an image that is displayed in each row ( the image is the same for all rows, except using an array i am assigning it different values depending on the position). An xml file defines the relative layout that i am using. My problem is that i can either get the entire row to be clickable or nothing at all, I only want this image to be clickable, instead of the entire row. How would i be able to do this ? i am new to android and am pretty much following different tutorials trying to create my list. Any help would be appreciated.
layout is like this :
TEXT:
[Image]
TEXT:
thats wat a row looks like...getting two texts from two different arrays and shows it, a third array is used to link to the image. I just want this image to be clickable instead of the entire row.
Thanks
Android's list component manages clicks per row. This makes it very difficult to achieve what you want to do. Two solutions come into mind:
1) If your list is never very long you could simply use linear layout and scroll view to build the list. This approach won't work if you fill in the list dynamically and you can't be sure that there won't be a very large number of rows as it would use too much memory in that case.
2) Other option is to use ListView but make your text components and images different view types in list ie. break you row into three.
That can be achieved overriding list adapter's getItemViewType(int)
http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)
In this approach you can make the image rows clickable but the text rows not.

Categories

Resources