Android ActionBar Tabs - Swipe

前端 未结 2 1663
花落未央
花落未央 2020-12-15 01:44

it would be nice, if i could swipe between the tabs i had in my actionbar.

I\'ve followed these tutorial: http://developer.android.com/training/implementing-navigati

相关标签:
2条回答
  • 2020-12-15 02:32

    This works perfectly, you will swipe between tabs:

    MainActivity:

    public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {
        CollectionPagerAdapter mCollectionPagerAdapter;
        ViewPager mViewPager;
    
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
    
               mCollectionPagerAdapter = new CollectionPagerAdapter(
               getSupportFragmentManager());
    
            final ActionBar actionBar = getActionBar();
            actionBar.setHomeButtonEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mCollectionPagerAdapter);
            mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    
                @Override
    
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
    
            });
        for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
        .setText(mCollectionPagerAdapter.getPageTitle(i))
        .setTabListener(this));
        }
    
        }
    
        public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    
        }
    
        public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    
        }
    
        public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    
        }
    
        public class CollectionPagerAdapter extends FragmentPagerAdapter {
    
        final int NUM_ITEMS = 3; // number of tabs
    
        public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    
        }
    
        @Override
    
        public Fragment getItem(int i) {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    
    }
    
        @Override
        public int getCount() {
    
        return NUM_ITEMS;
    
    }
    
        @Override
        public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
            case 0:
                tabLabel = getString(R.string.label1);
                break;
            case 1:
                tabLabel = getString(R.string.label2);
                break;
            case 2:
                tabLabel = getString(R.string.label3);
                break;
    
        }
    
        return tabLabel;
    
        }
        }
    
         public static class TabFragment extends Fragment {
         public static final String ARG_OBJECT = "object";
    
         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);
        int tabLayout = 0;
        switch (position) {
            case 0:
                tabLayout = R.layout.tab1;
                break;
            case 1:
                tabLayout = R.layout.tab2;
                break;
            case 2:
                tabLayout = R.layout.tab3;
                break;
        }
    
        View rootView = inflater.inflate(tabLayout, container, false);
        return rootView;
    
        }
    
       }
    
       }
    

    activity_main:

    <android.support.v4.view.ViewPager             xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>
    

    tab1.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    
    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/body1" />
    
    </LinearLayout>
    

    tab2.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/body2" />
    
     </LinearLayout>
    

    tab3.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/body3" />
    
     </LinearLayout>
    
    0 讨论(0)
  • 2020-12-15 02:35

    Have you ever seen this this one. It will help you for creating Tabs with ActionBar

    0 讨论(0)
提交回复
热议问题