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.
Related
i have this app that i want to have a specific way of navigation. And so i made a research but i got confused. And i am begginer in android development.
i want to ask what kind of layout or anything i can use to achieve that. I dont search for super specific answer, just what is the thing that is doing the job i want. So there it is:
the blue block("choose a brand" view) has to be on that position at all times and only changing the text if needed. I want when one image button is clicked the whole green block with the image grid change into another xml layout. I want to call multiple layouts in the green block when i interact with the buttons of those layouts. Atm the green block is an <include layout ="layout.xml"/>
I really apreciate any answer. Sorry if is basic but i really tried to find the thing i need but so far i see solutions that prevent me from using simple inflaters. Thanks in advance
You have to use fragments for this scenario.
You will have a LinearLayout where the first element will be your blue block.
The second element will be a FrameLayout that you will change into the Fragment you need (usually it will have the ID container).
Create a Fragment and set the layout your layout.xml file.
Create a second Fragment with the desired layout you would like to change the green block.
As you click on a imageButton you will have to change the current Fragment with the desired one. Here you will see how to send objects to fragments.
You can find here a way to switch between fragments. In the ft.replace method the first id is the FrameLayout you will use as a container(see above).
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
Be careful when you import fragments. If you use the support package you will have to use getSupportFragmentManager();
Read more on the android developer about fragments
I think what you are looking for is Fragment class, which can be used as reusable code that can be linked to a UI/layout.
From android documentation:
To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the Fragment class, which behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle.
Read more here
I am having a really tough time with one small issue in my AppCompat activity with tabs. I am using the Android Design Support Library, and have implemented a tabbed application with fragments.
Now, I have no problem creating tabs & fragments in the Activity's onCreate() method, but I cannot for the life of me find if it is possible to add tabs programmatically from within a fragment.
For reference, all of my tabs use the same fragment (OneFragment.java). I have tried using FragmentManager / FragmentTransaction, but although this creates a fragment (I think!), it does not make a tab.
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI. Any help would be much appreciated!
I have also tried to add a tab to the FragmentPagerAdapter and set the Tab Layout's adapter again, but this also seems to do nothing in the UI
If you managed to add a tab to the FragmentPagerAdapter but the UI didn't change then you probably forgot to call the notifyDataSetChanged() method on it.
Currently, my app is using a navigation drawer for my fragments. However, I have 2 fragments that are related, I would like to put these under one heading on the navigation drawer and then have 2 tabs to switch between the related fragments once I select that choice on the drawer.
Is this possible with only one activity or would I have to implement another activity specifically for the tab? I've considered only adding the tabs once the right fragment is committed but that leaves the question of how to remove it once I switch to a fragment that doesn't require the tabs.
I'd also like to avoid having to implement another activity as I am trying to keep the navigation available through my whole app. I'd rather not have to create an identical drawer and then have to keep things consistent between the 2 drawers.
Edit:
So far, I've managed to make a parent fragment that has a viewpager, fragmentpageradapter, and tablayout. Swiping between the 2 tabs does indeed shift between my 2 desired child fragments. However, I am unable to change a textview in one of my child fragments from my MainActivity. I'm guessing the reason why this is the case is because technically, my parent fragment is in view, not my child fragment. Any suggestions to get around this?
Hierarchy:
MainActivity
-Navigation Drawer
--Fragment1
--Fragment2
--Fragment3
--Fragment4
---FragmentPagerAdapter (under Fragment4, tabs)
----SubFrag1
----SubFrag2
Here's how you might use FragmentStatePagerAdapter to swipe across a Fragment:
http://developer.android.com/intl/es/training/implementing-navigation/lateral.html
I hope it help you.
maybe you can help me. I want to use viewPager to overlap an other layout. so for example: I the first Fragment. On this Fragment is a picture, than I swipe to the right and then the second Fragment should just go over the first Fragment. It's like in the Android Youtube App in which you swipe from left to right and the settings and etc go over the main layout(like in the picture). Can you tell me what i have to do?
Thanks a lot, Vinzenz.
picture:
http://i.stack.imgur.com/CQ8gL.png
What you're asking for is actually not related to ViewPager. There is another layout you can use to implement the same navigation pane overlay: DrawerLayout link: http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
I have one fragment that deals with previewing and so taking pictures, and on that fragment I want to have an option in the action bar have an item.
On some of the other fragments I would like to have items that will be in common, but only if not on a large device (tablet), as the tablet will have a fragment that handles controlling activities.
So, since I only have one Activity in my application, what is the best way to have context sensitive menus in the actionbar, in a fragment.
And how do I best show items only if on a small device?
It appears the best approach is to just have the fragment add the items to the actionbar as described here:
http://developer.android.com/guide/components/fragments.html#ActionBar
But, it will also be necessary to look for an id that is unique to the non-large layout to decide if certain menu items should be included.