3. Add Tag Tabs
The implementation tab in ActionBar can implement android. app. ActionBar. TabListener, and rewrite onTabSelected, onTabUnselected, and onTabReselected methods to associate Fragment. The Code is as follows:
Java code:
- Private class MyTabListener implements ActionBar. TabListener {
- Private TabContentFragment mFragment;
- Public TabListener (TabContentFragment fragment ){
- MFragment = fragment;
- }
- @ Override
- Public void onTabSelected (Tab, FragmentTransaction ft ){
- Ft. add (R. id. fragment_content, mFragment, null );
- }
- @ Override
- Public void onTabUnselected (Tab, FragmentTransaction ft ){
- Ft. remove (mFragment );
- }
- @ Override
- Public void onTabReselected (Tab, FragmentTransaction ft ){
- }
- }
Copy code
Next we will create an ActionBar in the Activity. The Code is as follows:
Java code:
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- Final ActionBar actionBar = getActionBar (); // The system prompts that the getActionBar method must be behind setContentView.
- ActionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS );
- ActionBar. setDisplayOptions (0, ActionBar. DISPLAY_SHOW_TITLE );
- Fragment artistsfragment = new artistsfragment ();
- Actionbar. addtab (actionbar. newtab (). settext (R. String. tab_artists). settablistener (New tablistener (artistsfragment )));
- Fragment albumsfragment = new albumsfragment ();
- Actionbar. addtab (actionbar. newtab (). settext (R. String. tab_albums). settablistener (New tablistener (albumsfragment )));
- }
Copy code
4. Add drop-down navigation
Create a SpinnerAdapter and provide the Drop-down option. Unlike the Tab mode, you only need to modify the setNavigationMode mode and change ActionBar. NAVIGATION_MODE_TABS to ActionBar. NAVIGATION_MODE_LIST. The improved code is
Java code:
- Actionbar = getactionbar ();
- Actionbar. setnavigationmode (actionbar. navigation_mode_list );
- Actionbar. setlistnavigationcallbacks (mspinneradapter, mnavigationcallback );
Copy code
The above code binds a SpinnerAdapter control through the setListNavigationCallbacks method. The specific OnNavigationListener code example is as follows:
Java code:
- Monnavigationlistener = new onnavigationlistener (){
- String [] strings = getresources (). getstringarray (R. array. action_list );
- @ Override
- Public boolean onNavigationItemSelected (int position, long itemId ){
- ListContentFragment newFragment = new ListContentFragment ();
- FragmentTransaction ft = openFragmentTransaction ();
- Ft. replace (R. id. fragment_container, newFragment, strings [position]);
- Ft. commit ();
- Return true;
- }
- };
Copy code
The ListContentFragment code is
Java code:
- Public class ListContentFragment extends Fragment {
- Private String mText;
- @ Override
- Public void onAttach (Activity activity ){
- Super. onAttach (activity );
- MText = getTag ();
- }
- @ Override
- Public View onCreateView (LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState ){
- TextView text = new TextView (getActivity ());
- Text. setText (mText );
- Return text;
- }
- }
Copy code