tab contents using actionbarsherlock tab style

橙三吉。 提交于 2019-12-31 10:56:16

问题


referring to this code below (taken from https://gist.github.com/1126843 ) how do i set the contents of the tabs?

public class NativeTabActivity extends Activity {
    private TabHost mTabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();

        addTab(new TextView(this), "Tab 1");
        addTab(new TextView(this), "Tab 2");
        addTab(new TextView(this), "Tab 3");
    }

    private void addTab(final View content, final String title) {
        View tabView = LayoutInflater.from(this).inflate(R.layout.abs__action_bar_tab_layout, null);
        TextView tv = (TextView) tabView.findViewById(R.id.abs__tab);
        tv.setText(title);

        TabSpec setContent = mTabHost.newTabSpec(title).setIndicator(tabView).setContent(new TabContentFactory() {
            public View createTabContent(String tag) {
                return content;
            }
        });
        mTabHost.addTab(setContent);
    }
}

from the code, it seems I need to put the contents under the View createTabContent(String tag) but how do I do it?


回答1:


I use Fragments for my Sherlock implementation.

public class ActionBarTabs extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    bar.addTab(bar.newTab()
            .setText("Home")
            .setTabListener(new TabListener<DashBoardFragment>(
                    this, "home", DashBoardFragment.class, null)));

    bar.addTab(bar.newTab()
            .setText("Inventory")
            .setTabListener(new TabListener<InventoryFragment>(
                    this, "inventory", InventoryFragment.class, null)));

if (savedInstanceState != null) {

        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }


}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go Location selection
            Intent intent = new Intent(ActionBarTabs.this, LocationSelectorActivity.class);
            ActionBarTabs.this.startActivityForResult(intent,0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private final FragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;




    public TabListener(FragmentActivity activity, String tag, Class<T> clz, Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();


        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            ft.detach(mFragment);
        }
    }

    @Override
    public void onTabSelected(Tab tab) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
            ft.commit();
        } else {
            ft.attach(mFragment);
            ft.commit();
        }
    }

    @Override
    public void onTabUnselected(Tab tab) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

        if (mFragment != null) {
            ft.detach(mFragment);
            ft.commitAllowingStateLoss();
        }           
    }

    @Override
    public void onTabReselected(Tab tab) {

    }

}

}

The key change to load different fragments into the tabs is to simply change the "YOUR_FRAGMENT_NAME" to the name of your fragment class in these lines:

bar.addTab(bar.newTab()
            .setText("Home")
            .setTabListener(new TabListener<YOUR_FRAGMENT_NAME>(
                    this, "home", YOUR_FRAGMENT_NAME.class, null)));

Hope this helps!




回答2:


I found Rymnel's answer very helpful, but I had to make some changes to get it working properly with ActionBarSherlock 4.0. I was having problems with overriding the onTab methods passing in FragmentTransactions, so I just used the default methods and reassigned the "ft" var inside the method. I'm sure there's a cleaner way to do this, but here is my working code:

public class TabTestActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setDisplayShowTitleEnabled(true);
    bar.setTitle("Activity Title");
    bar.addTab(bar
            .newTab()
            .setText("Tab 1")
            .setTabListener(
                    new TabListener<TabTest1>(this, "tab1",
                            TabTest1.class, null)));

    bar.addTab(bar
            .newTab()
            .setText("Tab 2")
            .setTabListener(
                    new TabListener<TabTest2>(this, "tab2",
                            TabTest2.class, null)));
    bar.addTab(bar
            .newTab()
            .setText("Tab 3")
            .setTabListener(new TabListener<TabTest3>(this, "tab3", TabTest3.class, null)));

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // app icon in action bar clicked; go home
                    Intent intent = new Intent(this, DashboardActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getSupportActionBar()
            .getSelectedNavigationIndex());
}

public class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private final FragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;

    public TabListener(FragmentActivity activity, String tag, Class<T> clz,
            Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;
        FragmentTransaction ft = mActivity.getSupportFragmentManager()
                .beginTransaction();

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state. If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = mActivity.getSupportFragmentManager()
                .findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            ft.detach(mFragment);
        }
    }


    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft = mActivity.getSupportFragmentManager()
                .beginTransaction();

        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(),
                    mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
            ft.commit();
        } else {
            ft.attach(mFragment);
            ft.commit();
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft = mActivity.getSupportFragmentManager()
                .beginTransaction();

        if (mFragment != null) {
            ft.detach(mFragment);
            ft.commitAllowingStateLoss();
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}


来源:https://stackoverflow.com/questions/9455046/tab-contents-using-actionbarsherlock-tab-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!