"Nested" RecyclerViews - java

Hi everyone.
I've come up with an idea for an Android App, and I was thinking how to turn my thoughts into something working. I know how to program for Android even though I'm not that advanced as you might see, so I wanted some tips from you guys who I'm sure will be able to help.
Idea
I was thinking about an App to organize stuff, see your objects on a list and be able to add, move or remove them from the list.
Thoughts
I first thought I needed a RecyclerViewto display each item. Then I thought every item itself might be a box, and so be containing other items inside: this brought me to think of a sort of "nested" system of RecyclerViews. Before going too deep into this system I had to clarify each item should have been a Class, each of which should have had a RecyclerView assigned.
Question
I was wondering how I could make this "nested" system of RecyclerViews. I thought about making a RecyclerView an object, because I need each RecyclerView to display, function and be always the same in any screen of any item. But I don't know what the best way is.
Should I make it an object? How do I create a new RecyclerView through a button so the user can first tap an item and then, eventually, tap a button (inside the item details view for instance) to make it a box item and so create and open a RecyclerView when tapped?
P.S.
It may seem like multiple questions but it actually is only one: how to add and display a RecycerView when I tap a button inside a details screen of an item, of course automatically (have a reference to that RecyclerView).

You have to create the RecyclerView that holds your items list, let's call it rv_list, this RecyclerView has an adapter that takes each item and adapts it to an xml layout row_item.xml that you should define. Now to make an item itself display its own recyclerview you should put a recyclerview inside row_item.xml and populate it when adapting that item to rv_list inside the method onBindViewHolder.

Related

How to put diffrent values depending on the item clicked in the recyclerview

when I click on each item of the recyclerview I want to go to another activity but the values in the activity will be different depending on the item clicked in the recyclerview.
For example : If I have a list of TV shows and when I click on each item it will go to the same activity and show me different episodes list.
Anyone can help how to achieve this?
Android Studio provides you with various project templates when creating a new project, If you have a look at the one described as Master/Detail Flow.
This will give you some working code that should cover your use case, which when comfortable that you understand it, you can then begin to implement into your own project.

Android studio: How to dynamically display and refresh a custom list which is horizontally and vertically oriented at the same time?

So I've been developping an app on Android Studio and at some point I need to display couples of values composed of texts and images but I have some issues with the display.
Let's say here that I want a list of fruit names associated with a picture, I'd like to display it that way :
Example
Each image and its related text is in linearlayout, so it's a list of linearlayouts.
The "add fruit" button open an alerdialog where the text and the image can be chosen, by clicking the 'ok' button in the alertdialog the name and the image are saved in a sqlLite database (which I already have programmed), and the linearlayout has to be added in the current list as the dialog is dismissed.
So the issue here is that:
I don't really know how to dynamically display each one of these linearlayouts in this specific order.
I don't know how to make it so that when a new fruit is added and the dialog dismissed, the list in the activity is refreshed directly.
I've been thinking of using a listview but listviews are only horizontal OR vertical but in this case I kind of need a mix of both.
Another idea was to delete the layout containing all the linearlayouts (all the list) when a new fruit was added, and then take my database to create all the linearlayouts again. But dynamically I don't think it will work, the refresh won't be done (?)
My last idea was to create a fragment and use my database in the onCreateView method to display every elements. And to refresh it, I'll just detach and attach again the fragment to the activity each time I add a new element.
I don't think my ideas are the optimal or even a correct way to do it, I've been struggling with it for a while. If you have any suggestions or ideas to help me, it would be great :)

Expandable Items in ListView

I am writing my self a ListView that will contain multiple types of items. I have done that using ArrayAdapter and a ListView now my issue is that I need to have some Items inside my Adapter that is gonna expand and that will have inner items of any type. Now the issue is how can I do this? I know I can use ExpandableListView but I don't need some items to be expanded. I saw this post about something similar, The person suggested to use ExpandableListView or a Custom Item, I would like to do the Custom Item but my concern is that the OP of the answer quoted this.
If the number of items is low consider using a linearlayout to look
like a listview and another linearlayout for the last item.
I'm not sure what they mean by "low" How many items can I have inside it? Would it cause lag?
so my question is what is the best way to do this? I need to put items
in a ListView that is of multiple types, and one of the types can
expand and have inner views that can again contain the same types and
so on.
Edit: Since 2 of you confused of what I want. I want to do the following inside my ArrayAdapter
Item
Item
Item
ExpandableItem {
Item
Item
Item
Item
}
Item
Item
I am trying to make some of the Items expand to have more items inside them and control the onClick of the inner items and such.
About the meaning of "low": I think it's about performance but devices today are better than devices were in 2013 when the linked post was written - maybe you'll never experience a sluggish UI with your approach. Of course this depends not only on your UI structure but also on the type of data you'll be showing (videos or just text?) and on other factors like does the device have to perform heavy work in the background or not.
RecyclerView was developed to be a "better ListView", so if performance can be an issue, then maybe it is a good choice. (The adapters for both ViewGroups can handle different View types but RecyclerView offers a better means of showing changes to single items and customizing change animations, another point in its favor)
How can I do this?
Have different View types for expandable and flat items
You can "manually" expand/ shrink a View (AFAIK your only choice when working with ListView) by using some type of animation (or the Transition framework) but you always have to...
Keep track of the expanded state of each expandable item (e.g. in the Adapter) and use them in getView() respectively onBindViewHolder().
Add another ListView like RecyclerView on clicking of any item.

Android Recyclerview for a few items

I have a list CardViews, each of which has a title and an icon. Upon clicking on each item, an Activity opens with that certain card's details.
I don't think I need more than a few items (I don't think more than 10), and I'm not sure how dynamic and changing they will be.
My question is: Should I use a RecyclerView to show these items, or should I just duplicate the cards and show them inside a LinearLayout without a RecyclerView?
In terms of performance, which option is better?
Both options should be OK (I haven't tested the performance, but both run OK), although I would prefer the Linearlayout. Because the list is small, you lose most advantages of the RecyclerView (performance-wise).
If you decide to go with the LinearLayout, you could create a Compound control and reuse it for the different items so you don't have to edit every item manually if something changes.
And if you would decide to use the RecyclerView, don't forget to call setItemViewCacheSize() and set it to a reasonable amount (the count of your items), because for so few items it doesn't make sense to need to re-populate the items every time you scroll. It makes sense for bigger lists, where you can't effectively work with that many items, but for cca 10 items it seems faster if you cache them all. The next thing you should also do is call setHasFixedSize(true) on the RecyclerView. And if you don't need nested scrolling, call setNestedScrollingEnabled(false) too, otherwise the scrolling might behave a bit strange (no "fast/continued scrolling" if you swipe quickly).
Hope this helps.

Is it possible to make a dynamic mix of ListView and TextView?

I tried to found a method, but no results. I want a ListView like below, and when I click on an element, like "Word", it'll be like this picture :
Is it possible ?
What you described is what an ExpandableListView does.
Basically it's like a listview so you'll still have to create your own adapter, but it lets you click a row to inflate a bigger item that you can then stuff your text into.
I would recommend creating your own class that extends ArrayAdapter or BaseAdapter. Then you can make use of the getView() method that gets called every time the screen gets redrawn for the user. You can then design multiple views for each selection and choose which one to display in the getView() function. So when the user selects an item, you set a flag in your custom class, and then notifyDataSetChanged() and you're good to go!

Categories

Resources