How can I put titles in ViewPager using fragments?

后端 未结 4 1366
旧时难觅i
旧时难觅i 2020-12-13 11:26

I am using ViewPager to allow user to swipe between fragments.

How can I add a the title of each fragment to the screen?

package com.multi.andres;

i         


        
4条回答
  •  猫巷女王i
    2020-12-13 12:01

    Where mine says SherlockFragmentActivity, you can just have FragmentActivity. getSupportActionBar can be getActionBar().

    public class Polling extends SherlockFragmentActivity implements OnMenuItemClickListener {
        private ViewPager mViewPager;
        private TabsAdapter mTabsAdapter;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mViewPager = new ViewPager(this);
            mViewPager.setId(R.id.pager);
            setContentView(mViewPager);
            bar = getSupportActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            bar.setDisplayShowTitleEnabled(false);
            bar.setDisplayShowHomeEnabled(false);
            mTabsAdapter = new TabsAdapter(this, mViewPager);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
                    LoginFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
                    EconFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
                    ElectionsFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.politics),
                    PoliticsFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.science),
                    ScienceFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.finance),
                    FinanceFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.religion),
                    ReligionFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.military),
                    MilitaryFragment.class, null);
            mTabsAdapter.addTab(bar.newTab().setText(R.string.international),
                    InternationalFragment.class, null); 
        }
    
        public static class TabsAdapter extends FragmentPagerAdapter
        implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
            private final Context mContext;
            private final ActionBar mActionBar;
            private final ViewPager mViewPager;
            private final ArrayList mTabs = new ArrayList();
    
            static final class TabInfo {
                private final Class clss;
                private final Bundle args;
    
                TabInfo(Class _class, Bundle _args) {
                    clss = _class;
                    args = _args;
                }
            }
    
            public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
                super(activity.getSupportFragmentManager());
                mContext = activity;
                mActionBar = activity.getSupportActionBar();
                mViewPager = pager;
                mViewPager.setAdapter(this);
                mViewPager.setOnPageChangeListener(this);
            }
    
            public void addTab(ActionBar.Tab tab, Class clss, Bundle args) {
                TabInfo info = new TabInfo(clss, args);
                tab.setTag(info);
                tab.setTabListener(this);
                mTabs.add(info);
                mActionBar.addTab(tab);
                notifyDataSetChanged();
            }
    
            public int getCount() {
                return mTabs.size();
            }
    
            public SherlockFragment getItem(int position) {
                TabInfo info = mTabs.get(position);
                return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
            }
    
            public void onPageSelected(int position) {
                mActionBar.setSelectedNavigationItem(position);
            }
            public void onPageScrollStateChanged(int state) {}
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                mViewPager.setCurrentItem(tab.getPosition());
                //Log.v(TAG, "clicked");
                Object tag = tab.getTag();
                for (int i=0; i

提交回复
热议问题