Thursday, February 13, 2014

actionBar add tabs Why not show up? Novice, solving .....


Ask for advice, ask what tabs do not show up, "A Tab", "B Tab", "C Tab" not showing up. Solving ....... Here is the code:

public class myActivity extends Activity {

private static final String TAG = "ActionBarTestActivity";
public void OnCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.myactivity);

ActionBar bar = getActionBar ();
bar.setNavigationMode (ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled (false);
Tab tabA = bar.newTab () setText ("A Tab") setTabListener (new MyTabListener (new AFragmentTab ()));..
bar.addTab (tabA);

Tab tabB = bar.newTab () setText ("B Tab") setTabListener (new MyTabListener (new BFragmentTab ()));..
bar.addTab (tabB);

Tab tabC = bar.newTab () setText ("C Tab") setTabListener (new MyTabListener (new CFragmentTab ()));..
bar.addTab (tabC);

}

/ **
* Interface
achieve ActionBar.TabListener* /
public class MyTabListener implements ActionBar.TabListener
{
/ / Receive for each Tab corresponds Fragment, operating
private Fragment fragment;
public MyTabListener (Fragment fragment)
{
this.fragment = fragment;
}
public void onTabReselected (Tab tab, FragmentTransaction ft)
{
}
/ / When the Tab is selected when adding the corresponding Fragment
public void onTabSelected (Tab tab, FragmentTransaction ft)
{
ft.add (android.R.id.content, fragment, null);
}
/ / When the Tab is not selected when the Tab delete the corresponding corresponding Fragment
public void onTabUnselected (Tab tab, FragmentTransaction ft)
{
ft.remove (fragment);
}
}
}



AFragmentTab, BFragmentTab, CFragmentTab under the same

public class AFragmentTab extends Fragment
{
@ Override
public void onCreate (Bundle savedInstanceState)
{
System.out.println ("EidtFragment ---> onCreate");
super.onCreate (savedInstanceState);
}
@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate (R.layout.fragment_a, container, false);
}


@ Override
public void onStop ()
{
System.out.println ("EidtFragment ---> onStop");
super.onStop ();
}
}

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Consult the. " . . .
Reply:
Gurus, you went back to a bar!
Reply:
I also encountered the same problem

No comments:

Post a Comment