Embedding action bar tabs with fragments

Recently, I needed to implement action bar tabs that would replace the current fragment with a new fragment. Despite hours of intense searching, I couldn’t find a clear solution, so I decided to imagine how I solved the problem here. Two of the fragments contained list views, which turned out to be the main complicating factor.

+6
source share
1 answer

First create an action to which you want to attach fragments. In the XML file for this action, add a linear layout, for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".WoodenSideProject" > <LinearLayout android:id="@+id/fragment_placeholder" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> 

Do not add anything to the XML file. Even a tab node preloaded with Eclipse.

Then create your fragments. First create the user interface for the fragment as you want using the fragment XML file. I will show how to create a fragment with a list:

 public class Fragment1Name extends Fragment { public static String TAG="DirectionsFragment"; private String[] list_items = {"Put the list of Strings you want here"}; ListView lView1; /*@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list_items); setListAdapter(adapter); }*/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_XML_title, container, false); createListView(view); // Inflate the layout for this fragment return view; } private void createListView(View view) { lView1 = (ListView) view.findViewById(R.id.ListViewID); //Set option as Multiple Choice. So that user can able to select more the one option from list lView1.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list_items)); } } 

Some people suggest extending ListFragment to a fragment that uses a list view. My experience is that this is more of a problem than worth it.

Configure the java Activity file as follows:

 public class ActivityName extends FragmentActivity { public static Context appContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_project); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); setTitle("WoodenSideProject"); ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1"); ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2"); ActionBar.Tab tab3 = actionBar.newTab().setText("Tab3"); Fragment Fragment1Name = new Fragment1Name(); Fragment Fragment2Name = new Fragment2Name(); Fragment Fragment3Name = new Fragment3Name(); tab1.setTabListener(new MyTabsListener(Fragment1Name)); tab2.setTabListener(new MyTabsListener(Fragment2Name)); tab3.setTabListener(new MyTabsListener(Fragment3Name)); actionBar.addTab(tab1); actionBar.addTab(tab2); actionBar.addTab(tab3); } 

Within the same action, create the following class:

 class MyTabsListener implements ActionBar.TabListener { public Fragment fragment; public MyTabsListener(Fragment fragment) { this.fragment = fragment; } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { //do what you want when tab is reselected, I do nothing } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.replace(R.id.fragment_placeholder, fragment); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(fragment); } } 

I don’t know if others had such problems as I implement tabs of the action bar with fragments, but if they are, I hope this helps. Any suggestions for better implementations would be highly appreciated.

+15
source

All Articles