سؤال

I'm implementing ActionBar tabs according to official guide.

I have a fragment that is added to activity. In that fragment I'm creating tabs:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab()
                       .setText("One")
                       .setTabListener(new TabListener<DemoFragment>(
                               this, "1", DemoFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
                   .setText("Two")
                   .setTabListener(new TabListener<DemoFragment>(
                           this, "2", DemoFragment.class));
    actionBar.addTab(tab);
}

But these tabs are never displayed. Nor content of DemoFragment. Even though at runtime getActionBar().getTabCount() returns correct count of tabs. And I see that DemoFragment is initialized. All I see is ActionBar.

What am I doing wrong?

I'm not using any support libraries as I'm developing for minSdk=14.

هل كانت مفيدة؟

المحلول

Update

IF YOUR NOT USING ANY SUPPORT LIBRARY THIS SHOULD WORK otherwise you will have to use the getSupportActionBar() and above mentioned dependencies.

Try something more like this:

// Get the Instance of the Action Bar, set Navigation Mode, remove title
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);

// One tab
actionBar.addTab(actionBar.newTab() .setText("One")
                   .setTabListener(new TabListener<DemoFragment>(
                           this, "1", DemoFragment.class)));
// Two tab
actionBar.addTab(actionBar.newTab()
               .setText("Two")
               .setTabListener(new TabListener<DemoFragment>(
                       this, "2", DemoFragment.class)));

Edit to Post - Working Code that I have used for a demo

Here is code from a demo Application That i wrote using Android sdk 14 Just like you are.

public class MainActivity extends Activity {

 // String Titles
static String [] titles = {"Fragment 1", "Fragment 2"};
static String [] fragmentClasses = {Fragment1.class.getName(), Fragment2.class.getName()};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SimpleTabListener.SetUpTabNavigation(this, fragmentClasses, titles);
}

And here is the SimpleTabListener Class

public class SimpleTabListener implements TabListener {


Context m_context;
String m_fragmentClassName ;
Fragment m_fragment = null;


public  SimpleTabListener(Context context, String tabFragmentClassName)
{
    m_context = context;
    m_fragmentClassName = tabFragmentClassName;
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) 
{
    // TODO Auto-generated method stub


}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
    if(m_fragment == null)
    {
        m_fragment = Fragment.instantiate(m_context, m_fragmentClassName);
        ft.add(android.R.id.content, m_fragment);
    }else
    {
        ft.attach(m_fragment);
    }

}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) 
{
    // TODO Auto-generated method stub
    if(m_fragment != null){
        // TODO Auto-generated method stub
        ft.detach(m_fragment);
    }
}


public static void SetUpTabNavigation(Activity activity, String [] classNames, String [] tabTitles){

    ActionBar actionBar = activity.getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.TabListener listener;

    for(int i = 0; i <  tabTitles.length; i++)
    {
        listener = new SimpleTabListener(activity, classNames[i]);
        actionBar.addTab(actionBar.newTab().setText(tabTitles[i]).setTabListener(listener));

    }

}

}

Frament1.class

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_1, container, false);
}

}

Fragment 2.class

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment2, container, false);

}

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top