Display the same Android fragment twice and use call getById - java

So I have 2 fragments visible if the user uses a device with a big enough screen (like a Samsung Galaxy Tab).
Right now I am displaying a fragment list twice (same fragment twice). Inside the code of the fragment I use this to hide a progressbar.
ProgressBar pb = (ProgressBar)getActivity().findViewById(R.id.progress);
pb.setVisibility(View.GONE);
Problem is that it only works on one of the fragments. Both have the same id since its the same fragment?
Should I create 2 identical fragments or is it possible to find the "correct" progress-bar in the correct fragment?

In stead of finding the view (the ProgressBar) in the activity's view hierarchy, find it in the fragment's view hierarchy. So inside the fragment, do the following:
ProgressBar pb = (ProgressBar) getView().findViewById(R.id.progress);
Generally, you don't want to do lookups in the parent's view hierarchy, so above basically applies to all views in the fragment's layout.

Related

How to reuse a layout in other fragments

I'm using a Navigation Drawer Activity. When I made two fragments, I noticed that their layout was the same. The difference is that pressing the button executes a different method for each fragment. But the other buttons do the same thing on the two fragments. How do I reuse the layout and java code for the fragments and be able to specify a method to be executed on each fragment, knowing that it will need View access?
One way to do this is to create two different fragment classes that load the same XML layout and assign the click handler for the button programmatically.
you can use visibility of the button.use same xml for both fragment instead of one button use two, then in fragment check which fragment is in view then change the visibility.Gone of the not needed button for the activity. if you are using databindng then do different task for two button in viewmodel that will do the rest of your button task
You can set a Boolean (fals is the first "fragment", true the second). Then you can use a check in witch "fragment" you are and execute the code.
if(true){
executeFirstButtonUse()
}else{
executeSecondButtonUse()
}
Then U wil have 1 fragmen, 1 view, (1 viewModel).
I u coud provide more info or code I could provide more detail or an other method.

How to re-use same fragment across viewPager

I have a fragment with a textView. I want the textView to change when I scroll across the viewPager.I want to reuse the same fragment rather than creating a new fragment for displaying each text.How do I do this? I hope pro developers can understand what I mean.
You cannot attach a same fragment into activity twice simultaneously. If you want the behavior you mentioned don't use ViewPager. Just use swipe gesture detection in a layout and maybe some animations.

Dynamically Adding Compounds to Fragments

Found the solution, see answer below
tl;dr - My View from the Fragment's onCreateView() is fine, but the result shown in the app is not. WTF?
I am new to Android. I am developing a single-activity Android app with some tabs managed by a TabLayout. The contents are Fragments displayed in a ViewPager. My activity_main.xml looks as follows:
<!-- SIMPLIFIED VERSION -->
<LinearLayout>
<android.support.design.widget.TabLayout/>
<android.support.v4.view.ViewPager/>
</LinearLayout>
I'm using a FragmentPagerAdapter which keeps the Fragments alive all the time (contrary to FragmentStatePagerAdapter), but destroys the View when the Fragment cannot be reached by the user from the current position. This means, that if the user is on the outer left tab, all tabs past the second tab from the left exist as a Fragment, but have no View. This seems counter-intuitive, as the user could just click on the tab he wants and reach ANY Fragment at all time, but I understand that the FragmentPagerAdapter only sees the ViewPager (which is navigated through swiping), not the TabLayout.
As the fragments Views get created, onCreateView() is called. When the View is detroyed, onDestroyView() is called. My Fragments are rather simple:
<!-- SIMPLIFIED VERSION -->
<LinearLayout>
<ScrollView>
<LinearLayout>
<!-- I want to dynamically add Views here -->
</LinearLayout>
<ScrollView>
<LinearLayout>
Now, I have a bunch ob objects in the background representing some data. When the Fragment gets a View (onCreateView()), I want to add custom Views for each data item into the Fragment. This should end up looking like a vertical list. I already instantiated the Views to be added in onCreate(). This seems logical to me, as onCreate() is only called once in my whole app.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// The fragment UI
_me = (LinearLayout) inflater.inflate(R.layout.fragment_log, container, false);
// LinearLayout inside the ScrollView
LinearLayout l = (LinearLayout) _me.findViewById(R.id.scrollableLinearLayout);
// Already instantiated CompoundViews
for(LogEntryCompound compound: _logEntryCompounds) {
l.addView(compound);
// My method to populate all TextViews, etc. from compound._data
compound.refreshUI();
}
return _me;
}
Since the view of the fragments gets destroyed, but not the fragment itself, this is done every time the user swipes too far from this fragment. I don't get this part completely (see my list of questions below), but it seems that when onCreateView() is called again later, the Compounds I already added are still there. Therefore I remove all Views in the Fragments' onDetroyView():
#Override
public void onDestroyView() {
super.onDestroyView();
LinearLayout l = (LinearLayout) _me.findViewById(R.id.scrollableLinearLayout);
l.removeAllViews();
}
The Compounds I display are sort of like Tiles, have some TextViews, ProgressBars, and are clickable. They directly represent a data object. All things they display come from this data directly (each Compound has a reference to his _data). I set all relevant UI elements within these Compounds by calling refreshUI():
public void refreshUI() {
_txtName.setText(_data.getName());
_txtDescr.setText(_data.getDescription());
_txtTopRight.setText(_data.getTimeString());
...
}
Now I have a really weird problem: All _data objects behave as they should, all the data is correct, even the Compounds' UI Elements have the correct .text set. The view I return in onCreateView is perfectly fine. But the View then shown in the app is not. Three out of four TextViews are fine, but one shows the same value for all 'Tiles' im displaying. The value displayed would be the correct value only for the LAST Tile.
Additionally, I have some more general questions (and I assume the more experienced readers will already be on their toes to tell me) about how things SHOULD be done:
1. Can I get the FragmentPagerAdapter to behave in a way that the views of the fragments are never destroyed? It's only three tabs and not a horrendeous amount of data. Recreating the View every time is surely inefficient?
2. How exactly does the FragmentPagerAdapter/ViewPager work? Why is it, that even though the View is apparently destroyed, it still contains the children I added before it got destroyed?
3. Should I add my Compounds anywhere else in code? Does the onCreateView() automagiacally restores its previous state? Or is onCreate() for instantiation and onCreateView() for adding to the UI correct?
After some late night debugging and code review I had a look at my XML file for the 'Tile'-View in question. For the TextView that behaved weirdly I found this:
android:textIsSelectable="true"
Removed it - everything works like a charm now.
Can't really explain this. Can only imagine that maybe the fact that the view is selectable, android messes up something and sees the TextViews from all Tiles as one and thus showing the same text across all.

Hide Activity with linear layout

I am inflating multiple same linear layouts from one activity, and I would like to hide all of these from another activity. How can I accomplish this?
what I tried:
LinearLayout test_layout=(LinearLayout)this.findViewById(R.layout.test);
test_layout.setVisibility(View.INVISIBLE);
But test_layout is always "null"
My Activity is "singleTask", so I think if I get hold of one view and close, it will close all similar views opened by this activity.

Getting Currently Set Content View

We are creating an app with two main views: sView and sViewSettings. If the Android Back button is pressed we want an if statment to check if the current view is set to sView settings, if it is then call the sView.
Already have a listener setup for the back button just need it to call the if statement to check the current view.
Have already tried
if (this.findViewById(android.R.id.content) == sViewSettings)
Any ideas on this?
Thank you for Reading,
Travis
The view with id android.R.id.content is a FrameLayout holding your content view. Try this:
ViewGroup contentFrame = (ViewGroup) findViewById(android.R.id.content);
if (contentFrame.getChild(0) == sViewSettings) { ... }
However, I suggest a slightly different approach: use a ViewSwitcher (or any kind of ViewAnimator) to flip between the two main views and keep track in your code of which one is on display.
EDIT: If you want to keep your layouts loaded separately, you can assign an id (the same one) to the root view of each layout and then retrieve the content view directly using findViewById.

Categories

Resources