Android開發之ViewPager+ActionBar+Fragment實現響應式可滑動Tab
阿新 • • 發佈:2019-02-03
按照一般的思路,我們或許會這麼做:首先,使用getActionBar()方法獲得操作欄,然後我們將操作欄的導航模式設定為Tab,並新增一些Tab,然後實現TabListener介面;其次,我們將多個佈局通過Inflater()方法變成View,然後放到ViewPager裡面(其實呢,ViewPager就是個容器啦,你換成FrameLayout也是一樣的,所以這裡可以用Fragment替換就是這個道理),並實現OnPageChangeListener介面就可以了。由此我們可以寫出下面的程式碼:
[java] view plain- package com.Android.AnyViewUI;
- import java.util.ArrayList;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.view.ViewPager;
-
import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.app.ActionBar;
- import android.app.ActionBar.Tab;
- import android.app.ActionBar.TabListener;
- import android.app.Activity;
- import android.app.FragmentTransaction;
- import android.view.LayoutInflater;
- import android.view.View;
-
publicclass MainActivity extends FragmentActivity
- private ActionBar mActionBar;
- private ViewPager mViewPager;
- private TabPagerAdapter mAdapter;
- private ArrayList<View> mViews;
- private ArrayList<ActionBar.Tab> mTabs;
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_main);
- //取得ActionBar
- mActionBar=getActionBar();
- //以Tab方式導航
- mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- //禁用ActionBar標題
- mActionBar.setDisplayShowTitleEnabled(false);
- //禁用ActionBar圖示
- mActionBar.setDisplayUseLogoEnabled(false);
- //禁用ActionBar返回鍵
- mActionBar.setDisplayShowHomeEnabled(false);
- //新增Tabs
- mTabs=new ArrayList<ActionBar.Tab>();
- ActionBar.Tab tab0=mActionBar.newTab();
- tab0.setText("介面一");
- tab0.setTabListener(this);
- mTabs.add(tab0);
- mActionBar.addTab(tab0);
- ActionBar.Tab tab1=mActionBar.newTab();
- tab1.setText("介面二");
- tab1.setTabListener(this);
- mTabs.add(tab1);
- mActionBar.addTab(tab1);
- ActionBar.Tab tab2=mActionBar.newTab();
- tab2.setText("介面三");
- tab2.setTabListener(this);
- mTabs.add(tab2);
- mActionBar.addTab(tab2);
- //獲取ViewPager
- mViewPager=(ViewPager)findViewById(R.id.ViewPager);
- //初始化mViews
- mViews=new ArrayList<View>();
- mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_0, null));
- mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_1, null));
- mViews.add(LayoutInflater.from(this).inflate(R.layout.layout_2, null));
- //初始化mAdapter
- mAdapter=new TabPagerAdapter(mViews);
- mViewPager.setAdapter(mAdapter);
- mViewPager.setOnPageChangeListener(this);
- //預設顯示第二項
- mViewPager.setCurrentItem(2);
- }
- @Override
- publicvoid onTabReselected(Tab mTab, FragmentTransaction arg1)
- {
- }
- @Override
- publicvoid onTabSelected(Tab mTab, FragmentTransaction arg1)
- {
- if(mViewPager!=null)
- {
- mViewPager.setCurrentItem(mTab.getPosition());
- }
- }
- @Override
- publicvoid onTabUnselected(Tab mTab, FragmentTransaction arg1)
- {
- }
- @Override
- publicvoid onPageScrollStateChanged(int arg0)
- {
- }
- @Override
- publicvoid onPageScrolled(int arg0, float arg1, int arg2)
- {
- }
- @Override
- publicvoid onPageSelected(int Index)
- {
- //設定當前要顯示的View
- mViewPager.setCurrentItem(Index);
- //選中對應的Tab
- mActionBar.selectTab(mTabs.get(Index));
- }
- }
- package com.Android.AnyViewUI;
- import java.util.ArrayList;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.view.View;
- publicclass TabPagerAdapter extends PagerAdapter
- {