I have a fragment that has a layout with non-unique IDs (the layout repeats itself multiple times). For example:
LinearLayout (vertical)
FrameLayout (id = R.id.always_the_same)
FrameLayout (id = R.id.always_the_same)
FrameLayout (id = R.id.always_the_same)
I can't make the IDs unique because they are loaded from an XML file individually.
For each of the FrameLayouts, I would like to add a child fragment inside. I tried to call the following multiple times from the outer fragment's onCreateView method, but it added all of the child fragments to the first FrameLayout.
this.getChildFragmentManager().beginTransaction()
.add(R.id.always_the_same, MyChildFragment.newInstance(), "tag")
.commit();
How can I specify which FrameLayout to add the fragment to? Or am I going about this all wrong and should be using something other than nested Fragments?
OK, found a workaround:
In the outer fragment's onCreateView method, instead of adding the fragments directly, I added placeholder layouts with unique IDs to the FrameLayouts. Then in the outer fragment's onStart method, I used those unique IDs to add the fragments.
Related
I am building parts of my layout programmatically.
My process looks like this:
I inflate a layout: View view = inflater.inflate(R.layout.view_layout, viewGroup, false);
I add this view to the ViewGroup: viewGroup.addView(view);
Before adding views in this way, I first add a fragment to the root view of viewGroup:
getChildFragmentManager().beginTransaction().add(R.id.viewgroup_root, fragment, "fragment_tag").commit();
My ViewGroup is a LinearLayout and I am adding the Fragment before I add the other views. However, the Fragment is appearing last - after all the views I add using ViewGroup.addView().
How can I get the added Fragment to be displayed first in the LinearLayout, and why is the LinearLayout displaying it last if it was added first?
commit() is asynchronous - it does not run immediately. Therefore you definitely are running your addView methods before your fragment is actually added.
As Fragments automatically are re-added to their respective layout based on ID and you do not control the ordering of when the fragment is added to the layout in those cases, you can't rely on any initial ordering.
Instead, you should always add a Fragment to its own container - if using Fragment 1.2.0 or higher (the latest right now is 1.2.5), you should add a FragmentContainerView to your LinearLayout. If you're using an earlier version of Fragments, you'd want to add a FrameLayout to your LinearLayout. In either case, you'd need to make sure you use setId() with that layout and use the same ID when you use add.
You will have to leave placeholder under your LinearLayout and use placeholder's ID to do beginTransaction/add stuff. Any viewgroup would do. You are not just adding another view to your viewgroup, you are adding a fragment. It has its own lifecycle, bound to entities hosting it (activity, fragment).
I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?
Two days ago i asked the following question:
Change the fragment in a framelayout from within another fragment of said framelayout
A fellow user Krish, really helped me out in finding out what was wrong with the way i thought, but i am stil not sure how to actually get what i want done.
I want to be able to switch between three fragments in one FrameLayout.
- the first of the three is loaded at the start of the parent fragment and when back is pressed at the second fragment
- the second must be replacing the first at the click of an item in the listview of the first fragment, and when the back button is pressed from the third fragment
-the third must be loaded when a button is pressed in the second layout
I've tried achieving this by calling the following line whenever the fragment must be changed. A1_frame is the FrameLayout of the parent Fragment/Layout and A1_B0_C2 is the fragment i am replacing
getChildFragmentManager().beginTransaction().replace(R.id.A1_Frame, new A1_B0_C2()).addToBackStack(null).commit();
From what i understand the problem with my solution is that it isn't possible to replace a fragment in the FrameLayout of a parent Fragment/Layout, but if it would work, it would solve my solution. thats why i chose to put it in here.
I hope someone is able to tell me what would work!
getChildFragmentManager() returns the Fragment manager of the Fragment it's being called from, in this case whatever Fragment is in A1_Frame
The method you're looking for is getFragmentManager(), which returns the Fragment Manager of the Activity/Fragment that the Fragment is a part of. I.e. MainActivity, or whatever is creating your first fragment.
I have a user interface divided into two blocks, a menu panel which is the same all the time, and a content which changes depending on the menu entry which is selected.
To change the content fragment dynamically i have to replace the current fragment by the new one, as explained in this SO question : Replacing a fragment with another fragment inside activity group
The problem is that the "current" fragment is not only one fragment, but two : the panel AND the content. So if I attempt to replace i assume that both fragments will be replaced by the new content fragment, which is not what i want.
Is there a way to replace one particular fragment with removing other present fragments, if any ? Thank you.
You can easily replace single fragment with FragmentTransaction#replace method.
Suppose you want to replace a fragment in a Framelayout, you can do it as shown below:
getFragmentManager().beginTransaction()
.replace(R.id.container, newFragment, null).commit();
Here R.id.container is id of the FrameLayout and newFragment is the Fragment you want to put in layout.
I guess you have to have two fragments in the content block. For the first fragment you would be setting a weight of 1 so that it occupies full content block unless you want your second fragment in the block to be visible.you would be setting visibility of that second fragment in content block to "gone" if you want only one fragment to be visible or else give it some weight or layout width and layout height for both the fragments to be visible.
I have read the android documentation about getLayoutInflator and I am still not understanding what it does. Can someone give me a use case for this method or may be during what time would you want to call getLayoutInflator?
XML Layouts in Android need to be Inflated (parsed into View objects) before they are used. getLayoutInflator() gets you an instance of the LayoutInflator that will allow you to manually inflate layouts for specific uses.
One example being in a Custom ArrayAdapter to populate a ListView with a Custom Layout.
You need to manually inflate and populate your desired Layout for each individual list item in the ArrayAdapter's overridden getView() method.
Use setContentView() when you're in an Activity. That method inflates the layout and displays the selected layout as the view for that Activity. But when you're NOT in an Activity and you need to work with a layout file, you have to inflate it to get access to the view objects in the XML.