I have a Fragment activity which hosts a fragment list. The fragment activity also has a couple of text views which summarise the data in the fragment list.
FragmentActivity = Layout that contains a fragment container, text views and a button.
ListFragment = goes inside of the FragmentActivity's fragment container and is a list of data
This works fine as an activity, but now I'm trying to put the whole lot into another fragment container to display it in an action bar tab on another activity. I can only get it to work with the ListFragment (obviously this is because the FragmentActivity can't go inside of a fragment container) but this means that the summary text views and the buttons are missing and I really need them there too.
I wondered if anyone had suggestions of the best way to implement this? Some thoughts of solutions I've had:
Change the Layout of the activity holding the tabs if this certain tab is clicked
Programmatically add the text views and button to the fragment container if this tab is clicked
Add another fragment to the fragment activity, below the list fragment
Either way I'm stumped so any help is really appreciated.
Create a Fragment that resembles the layout of you FragmentActivity and add it instead. you can't add the FragmentActivity as a Fragment simply because it's not a Fragment but an Activity that was added a Fragments support for older versions.
So instead of adding you button and TextViews directly to the FragmentActivity Layout. Create a Fragment with the same components and add it to the Activity layout.
That way could could reuse this set of components in another location in your application if you needed to, using the same Fragment.
Related
I usually open a new fragment from an existing fragment or activity. However, with an adapter, which can be used in multiple fragments / activities, how do I open a particular fragment dynamically?
I usually use the following piece of code:
Navigation.findNavController(view).navigate(R.id.action_startFragment_to_destinationFragment);
after creating an action in the navgraph.
As navigation occurs among fragments, so normally you'd keep the fragment's the responsibility to do that.
As you told that adapter which can be used in multiple fragments, so it should be attached to a particular fragment at a time, which normally it's the fragment that instantiated it.
So, you can pass a listener interface to the adapter which is implemented by the fragment; where you can trigger its callback in the adapter whenever you want to navigate to another fragment in the nav graph.
This callback method will be executed at the fragment which already implements the listener, and you can normally use the traditional navigation code:
Navigation.findNavController(view).navigate(R.id.action_startFragment_to_destinationFragment);
This way your adapter can be reused, and every time a fragment wants to reuse it, it should implements the listener.
Note: Probably you can pass an int argument to the listener callback that pass in the row number in the adapter back to the fragment so that you might decide to navigate to some other fragment.
This way you can keep the navigation only through fragments.
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.
I am new to android. I have created a new android project selecting tabs as a template Eclipse implements tabs and pager itself. Each tab view has a fragment. Then I implemented ListView in a fragment.
Now when a list item is tapped, new view (fragment) should be open. To open a new fragment view on clicking list item, I need to give R.id.fragment_container to it. this id tells us on which container fragment view will be shown.
My Question is where should I get R.id.container as there is no container is defined with this id in the Main Activity. I have tried many things came in google like to add a layout (Relative/Linear) as well but it crashes and also I have tried to give pager id as well but nothing happens.
Think i have some understanding problem with fragments. Any explanation/help specific to my case would be highly appreciated. Thanks
R.id.fragment_container or R.id.container are the IDs of a View in your MainActivity's layout file. Open that activity_main.xml (or whatever it is called) and add a container to hold your fragment.
You'll need to add the android:id="#+id/fragment_container" line to the View/Layout you choose.
add andriod:id="#+id/id_name_". in your fragment layout and call IT wherever u want
In my main activity I have a ViewPager with two tabs and each of them has its contents in a fragment. One of the tabs holds a list of items and when I press on an item from that list I want the details of that item to appear on the screen. I thought of putting the whole ViewPager in a fragment and when the user taps the item the fragment representing the whole ViewPager is replaced with another fragment representing the details of the item. Is it possible and is it a good practice? Maybe I should just start another activity with the details of the item?
How you do it is your own choice. It's perfectly fine to switch your Fragments.
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new FragDetails())
.addToBackStack(FragPager.class.getName()).commit();
Switching to another Activity is fine too, you just have more of a "flicking" screen when opening a new Activity (and some other life cycle stuff too of course). If you don't want that, switch Fragments.
In my application I want a panel that contains some text information then below it a list of menu items. What is the best way to do this?
Is it possible to have a ListView within the fragment along with other TextView's etc.
If not is it possible to have a ListFragment within a Fragment? That is what I am attempting at the moment but when I try and run it I get an error:
java.lang.IllegalStateException: Fragment LeftMenuFragment{4089c090} not attached to Activity
Yes you can have listview in a fragment. Like ListActivty there is Listfragment which you can use for using ListView in your application.
See this market tutorial for help
The answer is 'Yes' we can have a listview in fragment. The error is self explanatory. You hav not added it to the activity or any layout.