Shouldn't I be using a RecyclerView? - android

I want to implement a schedule-like overview as List and as Grid View. Currently this works using a RecyclerView and swapping out the LayoutManager at Runtime. This works fine in general, but as soon as I tuned the grid cells to have a fixed height, I realised that the RecyclerView does not respect the wrap_content layout property and instead introduces whitespace between my grid items.
I then stumbled over more or less the same question for the linear LayoutManager and the third-party provided specialisations of the LayoutManager. A quick searched revealed nothing similar for the GridLayoutManager and I am beginning to suspect I am going down the wrong road:
Apart from the easy switching between layouts, there seems to be no reason for me to work with a recycler view:
I dont't require any special animation stuff.
My adapter returns a fixed list without loading more items on demand.
Actual recycling of items doesn't even take place.
So I am currently evaluating to simply switch to separate Fragments using an ordinary ListView and GridView. But this does feel a little like working against the intentions of Android ... After all, they introduced a single view that (in theory) should exactly do what I need.
Am I missing something obvious that could turn out to be painful when using those "legacy" Views to get the job done? Or are there maybe other, more specific Views that allow switching from linear to grid layouts apart from the RecyclerView?
If this should be of importance: I am using Xamarin.Android, not native Java. But I would be willing to port some code from Java to C#.

Follow this ticket:
https://code.google.com/p/android/issues/detail?id=74772
It is not merged yet but soon should be merged and WRAP_CONTENT support should be available in the next version (23.1.1+) or the one after.

Related

Multiple recyclerviews (gridlayout and horizontal linearlayout) inside one view

I am trying to achive this:
First I tried by putting all my recyclerviews (with WRAP_CONTENT) inside a nestedscrollview. That worked, but the performance was awful. Then I tried to set a height for my recyclerviews, that was a lot better (especially the first gridlayout and the horizontal linearlayout loaded very fast), but still had the problem with the dynamic "category" part.
Now I am trying to put all my recyclerviews inside a single recyclerview with different viewtypes. Since that is a pretty big deal (I need to refactor a lot of code because I have diveded every area from the screenshot inside a single fragment and now I need to put all that code inside an adapter) I wanted to ask if I can actually expect any gain from this, because in the end its again a "nestedscrollview" (made by myself, but...). Or if there is some other "best practice" way to achive this layout.
Thank you
Edit:
As expected this didnt do the trick neither. When just the two first recyclerviews are added as viewtype it scrolls and loads smoothly. But as as soon as I try to add the category items (below the category), I notice a lag and especially when selecting multiple categories and scrolling fast up, there is noticable lag. I guess I will have to change my layout and move the category selection part inside a separate view, just need to come up with a user friendly solution. But its acutally quite dissapointing that, in my opinion such trivial task, laying out multiple tables, is such a pain in the ass on android.
I didn't manage to get it working with standard android stuff.
Now I am using epoxy from airbnb ,and I have converted all my views from nestedscrollview to the epoxy recyclerview. Its a great library, and airbnb use it too for all their views.
Nevertheless it's sad that the android dev team doesn't address this problem and provide a solution besides the info "don't nest multiple scrollviews(recyclerviews) that scroll into the same direction".
You can use Recyclerview in recyclerview.
https://irpdevelop.wordpress.com/2016/02/10/horizontal-recyclerview-inside-a-vertical-recyclerview/
And make sure to use multiple view types.

Android layout: scrolling list with header

This is not a design question, I have the item designed. I am confused on how to pull it off.
I am tasked with designing a view for Android that has a view of a user's post and the comments under it. The post contains some extra information and widely different design from the comments, but all of them need to scroll in tandem, like a web page would (Oh, how I am spoiled by my years of web dev...).
My current solution is to nest a LinearLayout (at the top of the view to contain the user's post) and a RecyclerView (below the post to display the comments) inside a vertical ScrollView. The information is actually displayed, but the RecyclerView, of course, scrolls independently of the LinearLayout above it and ruins the functionality of the view.
I wish, if possible, to keep the RecyclerView in use
The best case scenario would be to have the LinearLayout with the post scroll a certain amount, and then the RecyclerView take over. However, I don't want to poison my codebase with 200+ ugly lines of code to achieve this, so if this is a laborious task to complete, I would rather look for alternatives.
The first thing to understand is: do you really need a RecyclerView, or even better, do you really need recycling?
If the answer is yes, the way to go is two different item types in the RecyclerView's Adapter (for more details, see here or here). This concept was already present in the ListView: the main difference is that RecyclerView enforce the use of the View Holder pattern. It is not so complex, and, more importantly, is the way the RecyclerView is supposed to solve that problem. Depending on your UI design, you may also want to have different view types for different types of comments (plain text, images, ...). Remember that, when the RecyclerView is included in a ScrollView, the recycling won't work, because all the items in it will be drawn at once to compute the height of the content of the parent ScrollView.
If the answer is no, then you could just create your views at runtime and add them to a parent LinearLayout in a ScrollView. It is really nothing more than a for loop.
A more fancy approach would be to use an ItemDecoration for the user's post, but I don't see any specific advantage in this case.

Embed elements of a RecyclerView inside CardViews

I have a RecyclerView that display the list of modifications per day for a document. Items are TextView with values like "Line 3 updated to ...", "Line 5 removed", etc. The ViewHolder handle 2 types of views :
Headers : which are the days. These are simple TextView too that are in bold, larger, etc.
Logs: that's what I was talking about : "Line 3 updated ...", etc.
What I would like is that each "day", with its corresponding logs are embedded inside a CardView. But a document can have huge number of modifications per day (>100). So programmatically creating a layout with a CardView as the root, calling 100 times addView() on it to add each logs and then passing this layout to the RecyclerView does not seems a good idea to me.
Is their any way to delimit between a "positionStart" and a "positionEnd" views that will be embedded inside a CardView? It seems to me that this isn't possible or by adding each TextView programmatically inside a CardView but it will then slow down the binding of the views and break the ViewHolder pattern. Am I right or is their a solution I didn't think about ?
You have 3 options to achieve this "grouped in a card" behavior.
(as mentioned) you create the layout yourself and put the whole thing into the recyclerview. This is probably the worst solution since it negates the idea of a recyclerview in the first place.
You just wrap each of your items in a CardView (and set the corner radius to 0dp).
On < 21 devices (I think) there will be some additional padding and every item will appear as its own card, but on higher API versions those cards will lie next to each other and just have some "seam" between them. The shadow on the corner is also a bit buggy, but this is probably the easiest and cheapest solution.
Alternatively you can also create a custom view that fixes the errors mentioned above (margins between and shadow) and use your own to wrap the views. (I believe this is what the Inbox app does if I recall correctly, which also features lists in cards.)
You use an ItemDecoration. For this approach you need a kind of stable setup of your dataset, but if its just the headers and logs, you can draw a shadow above the header, draw borders to the left and right of every item, and draw a shadow beneath the last log. This will also require some setup, and if you introduce further view types you will also have to modify this code (it's highly dependent on your data set)
The 1. method is probably the worst idea. It will work for small lists.
The 2. method can work, but you either will have to create your own custom view or live with a "bugged" version on lower api levels.
The 3. method is something I tried once for fun, and will work, but you will have some additional dependency between your data, your adapter, and your decoration. You can see an example of this decoration here on GitHub. It just draws a shadow around all of the items.

GridView with dynamic number of items in each row

I am trying to achieve a gridview with a different number of items in each row, depending on the width of each item. I want it to look like the one on the Medium app, as shown below:
How can i achieve this?
You can approach the problem in two main ways:
Use ScrollView with a Linear- or RelativeLayout inside. Then you would add the items in your code, measuring each and putting them into new rows when required. This will require some logic to be created, but is achievable. The ScrollView will take care of the scrolling in case the contents exceed the screen height. The problem with this solution is that you will not have a recycling mechanism, which can lead to problems with memory when the list gets long.
Use RecyclerView! :-D One of the awesome features of the RecyclerView is the fact that while providing the standard Adapter-based concept of the list of items, it also allows you to implement a custom LayoutManager. Create your own implementation of LayoutManager which positions the items based on their measured with and you are done, the RecyclerView will take care of scrolling and recycling the items for you! :-D This will take some getting used to, especially if you have no experience with RecyclerView, but the whole thing should take no more than several hours and is, in my humble opinion, really worth it.
You are free to choose either, but the second approach is more modern and, in general, easier to implement. In general, you should learn to use the RecyclerView, it helps a lot and is quicker to implement than the good old List- and GridViews, once you get the hang of it.

GridLayout and dynamic views

I want to use a GridLayout to reproduce a layout similar to what we currently se on the Android Market (I mean Play Store !).
Now I managed to do something relatively similar :
This looks nice, but I do not think this would scale well with many Views if I used bitmaps instead of colors for the backgrounds of the Views. As I understand GridLayout cannot use an adapter, so I would have to manage myself the management of Views when they are shown/hidden.
Any suggestions ? I don't have to use a gridlayout if this layout is possible with another view that I can plug to an adapter.
No there is no magic solution that will auto-populate your views. This is a pretty custom case. Your best bet will be to set a few timers that will periodically load/update the content of each of the sections. You will have to create/destroy/animate all of the views yourself. But I like the idea and glad to see that you are trying to make attractive dynamic content.

Categories

Resources