Best practice to reuse RecyclerView layout in other places - android

I have a Fragment which contains a RecyclerView to display a list of items in a feed. However, an individual item might be opened in another Fragment with the same layout it has in the RecyclerView.
What would be the best practice for this scenario? Could/should I create a separate Fragment for the item and use it somehow in the RecyclerView?

Well, when you create RecyclerView in the first fragment, you also create layout for the item you want to display. In the other, fragment you can just include the layout of the item. I don't think it can get more simple than that.
My practice is, i try to identify items that repeat itself and i try to create general layouts, for example, layout with title and subtitle and the use this layout in RecyclerView or just include it. Good thing here is that RecyclerView allows you to have different layouts and instead of drawing views or including it (if there are more view in fragment that i have created), i just add it to one RecyclerView.

Related

Dynamically create layout (similar to listview)

I want to create what is basically a list view but without it being a ListView.
So I have a ListView right now that pulls up a layout with a CardView, TextView, etc. However I need to create multiple lists but listviews wrap the content up and makes me scroll inside the view - I don't want this. I want to be able to see the entire list and scroll in the main view.
How can I just add the layout with the CardView, instead of displaying it as a ListView? Would be a lot easier if I could attach an adapter to a Linear Layout or something.
Edit: I know I could add it all in the XML, but it seems like it'd get very bad looking in the xml code.
ListView (and RecyclerView) do a ton of non-trivial things that are very difficult to independently implement with your own homemade alternative. Fortunately, there are many third-party solutions that address your particular problem. A good choice is CWAC's MergeAdapter. It's essentially a wrapper of sub-adapters.
To handle the section headers, you can either make the first item of each sub-adapter a header item (styled accordingly). Or use MergeAdapter's addView() method to add a "header" view before each addAdapter() call.

A recycler view vs the linear layout inside a scroll view?

Am trying to create a page with a scrollable list. Features would be a normal list to remove item by clicking on it. Number of items in that list are limited and added dynamically by user. You can consider a to do list as example. Now which would be a better approach to implement it? Recycler view with data bound to its adapter? Or the normal linear layout with items added as children at run time?
My current implementation is recycler view. But,I found it lagging and animations are not performing well. So a linear layout is auto animated by specifying it xml -- by setting animate layout changes to true.
FYI data is local and syncs in background.
Never use a LinearLayout for anything longer than a single screen. The whole point of ListView and RecyclerView is to efficiently reuse views instead of needing to hold things in memory when they're not visible. Maybe you can refine or reask your question so people can help you with whatever difficulty you're having with animations, rather than avoiding the issue.

Empty ListView inside ScrollView as two Fragments

I want to have ScrollView Inside a Fragment that contains a TabHost as you can see from the screen-shot
it's composed by two sections:
Header: in my case it's an image, I want this section to scroll
TabHost: contains three Fragments one of them is a ListView
What I have tried ?
I tried to create a scroll view and a list view inside the TabHost, everything is fine for the two first tabs, but in the third one which is a ListView, the items are not displayed.
After some research, I figured out that we can't have a ListView inside a ScrollView.
So what is the best approach to create a view like described here is a screen-shot of what I want to create:
In fact there's a way to implement ListView in ScrollView. But with this solution, ListView acts like a LinearLayout in memory.
btw: I used it before and working nice, saves the day.

ListView v/s an inflated view added on another view

Suppose i want to make a view like as given below image
Is it efficient to have a custom list-view for this or a simple linear layout with elements which can be inflated and added to another layout. I do not want to have any click operation over the items, i just need to show them over a screen. Which one will be better approach?
I already know how to make Custom ListViews

Need suggestions for dynamically creating views in android

I am going to start one app where my activity page will contain "n" grouped views. Grouped view means "collections of views (i.e. One group can have TextView+Button+ImageView)". So the page will have "n" number of such grouped views.
I need suggestions like what would be the best practice to implement this. I could think of below ones:
1) Should a ScrollView be used (Then I will have to create groups in runtime and place one under another)?
2) Or a ListView be used (Then how can I accommodate the height of each row as grouped views height may differ from each other?)
Or is there any other way I can go along with?
Appreciate the suggestions and any sample examples if have. Advance Thanks.
Both options would work, it really depends on your use case.
Place a vertical LinearLayout inside of a ScrollView and add your grouped-views to the LinearLayout. I would recommend this if you have a relatively small number of such views (not necessarily a fixed number, but small enough that you wouldn't have to scroll many "pages" to see them all). Make sure the ScrollView has android:layout_height="match_parent" and the LinearLayout has android:layout_height="wrap_content".
If the number of grouped-views is not small, you could use a ListView and make an Adapter for it. This lets you take advantage of ListView's automatic view recycling when items get scrolled off screen.
For either case, make an XML file for just the grouped-views. In code, you would get a LayoutInflater object (usually by calling Activity.getLayoutInflater()) and call inflate(R.layout.your_grouped_views, null). If using the LinearLayout, you would add it in code with one of the LinearLayout.addView(..) methods; if using the ListView, your adapter would return the whole thing from getView(...).
create one xml layout containing the constant elements of your group view.
in you main xml layout which will be the contentView of your application, put a ScrollView and a single LinearLayout.
then in the program inflate as many views of your group view as you want.
For your answer i want to give you referance of this website, on this website you can learn create dynamic view in android...

Categories

Resources