Can I use the same fragment multiple times in a single activity? - android

I have an activity needs to put some custom card fragments in it. These fragments basically look the same, I just need to change some parameter values of each. Now I'm planing to use the same fragment as a 'template' in the page multiple times. But in the fragment's layout xml file there are some fixed item IDs, so if I want to change one of the parameters of the fragment's UI display, I need to use findViewByID() in the fragment's class to set different values of same item ID. Can I do like that?

Related

Navigate between several ListView

I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?
Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.

Clarification on the structure of my app

Some background info first. I require my app to show a feed of posts, implemented as cards. The data within the posts/cards is retrieved from my site's REST API. My app has 3 different tabs, each containing a different feed of posts/cards (but still the same layout).
Currently, I have an activity_main and the 3 fragments made for each tab (fragment_1, fragment_2, fragment_3). To my understanding, a Fragment is a layout that can be re-used on different Activities. Is this correct?
Additionally, where should I implement the layout/design for the cards? Should it be directly within the separate Fragments (even though the layout of the cards will be the same across all 3 tabs)?
After implementing the card layout, how would I populate the cards given the requested REST API json data? In other words, how would I loop through each result and insert the data into the card and display it on the screen?
Sorry for the noob questions.
Currently, I have an activity_main and the 3 fragments made for each
tab (fragment_1, fragment_2, fragment_3). To my understanding, a
Fragment is a layout that can be re-used on different Activities. Is
this correct?
Yes.
Additionally, where should I implement the layout/design for the
cards? Should it be directly within the separate Fragments (even
though the layout of the cards will be the same across all 3 tabs)?
Have a single fragment. Create 3 instances of this fragment and inflate the same layout/design if the layout of the cards will be the same across all 3 tabs.
After implementing the card layout, how would I populate the cards
given the requested REST API json data? In other words, how would I
loop through each result and insert the data into the card and display
it on the screen?
Pass a unique identifier that identifies the feeds (for this fragment) to the fragment as a bundle when you create a new instance and set them as the fragments argument. You can then retrieve this information to retrieve data relevant for this fragment.
To display the data, use a ListView/RecyclerView along with a suitable adapter to connect your data to your view.
I hope this answers your question.
OK. Lets try this. Yes, you are right, Fragments are basically layouts that can be used in different Activities. As it says on Android Developers site:
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities.
Having that said, since it is the same layout for all three tabs, you can use only one Fragment and populate it with different data. So if you want to change anything, you would do it only within that one fragment.
For the cards I am guessing that you will be using a custom ListView adapter. And that you should (must) implement outside the Fragments.
And regarding the last question, there are many libraries that make consuming ReST webservices much easier. For example Retrofit.

Why should I not add Fragments to the activity layout XML file?

Edit: source : http://developer.android.com/training/basics/fragments/creating.html last parargraph says :
Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson.
Thanks in advance.
You can use fragments in two ways ,
Static Fragments
Here you can define the fragment in whatever the layout file you need. Only thing is, that defined fragment can not be change at the run time. So, re-usability will be issue here, you cant take the advantage of the re-usability of fragments in this case.
Dynamic Fragments
Here you can define a place holder(frame layout etc) on your layout and you can add/replace whatever the fragment you expect at any time while your activity is running. This ensure the re-usability.
Also you can use backStack if back navigation is required.
So, it depend on your requirement.
If the fragments are defined in Xml you won't be able to change them at runtime; in that case I'd suggest using an <include layout="#layout/my_inner_layout"/> tag, that is not only better performance-wise as it is easier to use (you can access the views defined in my_inner_layout.xml using findViewById() method from the host activity).

Using same views twice in two Fragments

I have a quiz app with two fragments, for the two quiz modes I have, within a ViewPager. For each fragment, in addition to other things, I dynamically create a list by inflating linearlayouts from XML within a while loop iterating through all the categories. This layout which contains this list is identical in both the modes, it is the rest of the fragment that is different, hence requiring two separate pages.
The problem therefore is that I do this costly process of inflating layouts twice, once within each fragment. I would like to do this only once and use it twice.
Two solutions I've considered:
Using an adapter in the parent activity, accessing this through the
fragments and subsequently using a ExpandableListView.
Problems:
I use checkboxes to select categories. The two fragments deal with clicks differently. With my limited knowledge I have no clue how achieve this. I understand that I can inflate a custom layout but then how do I set a listener in the fragment?
Some of my categories are grouped, others are standalone items. How do I remove the arrow in these items?
.
Using an arraylist in the parent activity. Accessing this in each fragment and adding the layouts to the parent this way.
Problems:
Cannot do this as I'm adding the same view twice so I get an error as the specified child already has a parent. (IllegalStateException)
However this does mean I can easily access the CheckBoxes and iterate through settings oncheckedchangedlisteners
Thank you so much in advance :) If you require any more information or anything else just ask!
I dynamically create a list by inflating linearlayouts from XML
within a while loop iterating through all the categories.
This suggests a ListView.
Using an arraylist in the parent activity. Accessing this in each
fragment and adding the layouts to the parent this way.
This solution will not work from the start, because you have the two fragments in a ViewPager which imposes a different behavior. If the fragments weren't in the ViewPager you could build a mechanism for storing those views in a list followed by dynamically detaching//attaching them from the fragments views. But storing the views in a list(and having them in memory all the time) plus the headache of managing their status isn't worth it.
Using an adapter in the parent activity, accessing this through the
fragments and subsequently using a ExpandableListView.
Why use only one adapter and not two, one for each fragment(so they are independent)? I would also implement the adapter in the fragment itself.
I use checkboxes to select categories. The two fragments deal with
clicks differently. With my limited knowledge I have no clue how
achieve this. I understand that I can inflate a custom layout but then
how do I set a listener in the fragment?
Not quite sure, but if you're talking about accessing the CheckBoxe from a "row" layout and having different behaviors on user actions based on the two fragments, you could very easy implement a base adapter class. That base adapter class will handle the common logic, but you'll also have an abstract method called on a CheckBox action(in the OnCheckedChangeListener). The two subclases(for the two fragments) of that base adapter will each implement that abstract method with their own logic independent from each other.
Some of my categories are grouped, others are standalone items. How do
I remove the arrow in these items?
Like above, this can be done by implementing a smart adapter.

Dynamic adding of multiple fragments

I am thinking of an app in which I will be programatically able to display some fragments according to metadata I have stored somewhere. Up to know, I have been able to find out, that each fragments lies in corresponding FrameLayout, or especially, when I create activity with one FrameLayout, I am able to store there only one Fragment at a time, no matter what kind is it. Problem is, however, with situation, when my metadata declares I have to put 3 fragments into my activity, while there is only one FrameLayout.
I see two possible solutions:
1) making several FrameLayout and in final stage, some of them will be used or not
2) somehow join multiple fragments to fit into one available FrameLayout
I don't like solutuion 1) and I don't know how to achieve 2). How to you see it? Is it possible to dynamically add multiple Fragments into activity with one Frame Layout?
In your situation where you require more number of Fragments to be added to your screen avoid using the FrameLayout, you will not be able to achive it. There are several factors that define how you layout multiple Fragments
Are all the fragments of the same size?
Should the fragments be place in a particular order?
First Create an empty parent layout (Layout class is left to your choice except FrameLayout). Next define your child view for your parent layout using a seperate xml file this file must contain your fragment place holder. Using the inflator inflate your child view and assign a Fragment to the inflated layout and eventually add it to your parent layout, through this way you can active what you are looking for. Just remember as you inflate your layout do mention its layout size so that you achive the kindly of layout you want.

Categories

Resources