今天寫了個(gè)示例代碼,就是使用Actionbar類實(shí)現(xiàn)頂部標(biāo)簽切換功能。如果所示。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站設(shè)計(jì)、太平網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、太平網(wǎng)絡(luò)營銷、太平企業(yè)策劃、太平品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供太平建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
使用最新的adt工具,創(chuàng)建項(xiàng)目的時(shí)候都會帶一個(gè)android-support-v7-appcompat的類庫項(xiàng)目,
這個(gè)libproject中有我們要用的ActionBar,可以適配2.1的Android系統(tǒng)。
廢話不多說,直接上代碼。
1、修改activity_main.xml,增加ViewPager。
2、修改MainActivity中的代碼,讓其繼承ActionBarActivity
public class MainActivity extends ActionBarActivity implements TabListener {
3、創(chuàng)建TabsPagerAdapter繼承FragmentPagerAdapter
package com.example.tabswithswie.adatper; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.example.tabswithswie.fragments.AppFragment; import com.example.tabswithswie.fragments.GamesFragment; import com.example.tabswithswie.fragments.MoviesFragment; public class TabsPagerAdapter extends FragmentPagerAdapter { public TabsPagerAdapter(FragmentManager fm) { super(fm); // TODO Auto-generated constructor stub } @Override public Fragment getItem(int index) { switch (index) { case 0: return new AppFragment(); case 1: return new GamesFragment(); case 2: return new MoviesFragment(); } return null; } @Override public int getCount() { // TODO Auto-generated method stub return 3; } }
4、創(chuàng)建AppFragment繼承android.support.v4.app.Fragment
package com.example.tabswithswie.fragments; import com.example.tabswithswie.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class AppFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment_app, container, false); } }
5、創(chuàng)建布局文件fragment_app.xml
6、創(chuàng)建GamesFragment繼承android.support.v4.app.Fragment
package com.example.tabswithswie.fragments; import com.example.tabswithswie.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class GamesFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment_game, container, false); } }
7、創(chuàng)建布局文件fragment_game.xml
8、創(chuàng)建MoviesFragment繼承android.support.v4.app.Fragment
package com.example.tabswithswie.fragments; import com.example.tabswithswie.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MoviesFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment_movie, container, false); } }
9、創(chuàng)建布局文件fragment_movie.xml
10、回到 MainActivity類,添加Tabs到ActionBar中,并處理點(diǎn)擊滑動事件。完整代碼
package com.example.tabswithswie; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar.Tab; import android.support.v7.app.ActionBar.TabListener; import android.support.v7.app.ActionBarActivity; import com.example.tabswithswie.adatper.TabsPagerAdapter; public class MainActivity extends ActionBarActivity implements TabListener { private ViewPager viewPager; private ActionBar actionBar; private TabsPagerAdapter mTabsPagerAdapter; private String[] tabs ={"應(yīng)用","游戲","視頻"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取viewpager viewPager = (ViewPager) findViewById(R.id.pager); //實(shí)例化pageradapter mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mTabsPagerAdapter); //獲取適配的actionbar actionBar = getSupportActionBar(); //設(shè)置home按鈕不可點(diǎn)擊 actionBar.setHomeButtonEnabled(false); //設(shè)置頂部導(dǎo)航的模式 -tabs actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //添加標(biāo)簽 for(String tab:tabs) { actionBar.addTab(actionBar.newTab().setText(tab).setTabListener(this)); } //設(shè)置ViewPager切換時(shí)候的監(jiān)聽事件 viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { //頁面滑動,頂部標(biāo)簽跟著改變 actionBar.setSelectedNavigationItem(position); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } @Override public void onTabReselected(Tab arg0, FragmentTransaction arg1) { // TODO Auto-generated method stub } @Override public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) { //tab選中,切換viewpager viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { // TODO Auto-generated method stub } }
代碼就是這樣的,搞定收工了。示例代碼下載