今天写了个示例代码,就是使用Actionbar类实现顶部标签切换功能。如果所示。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站设计、太平网络推广、重庆小程序开发、太平网络营销、太平企业策划、太平品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供太平建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
使用最新的adt工具,创建项目的时候都会带一个android-support-v7-appcompat的类库项目,
这个libproject中有我们要用的ActionBar,可以适配2.1的Android系统。
废话不多说,直接上代码。
1、修改activity_main.xml,增加ViewPager。
2、修改MainActivity中的代码,让其继承ActionBarActivity
public class MainActivity extends ActionBarActivity implements TabListener {
3、创建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、创建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、创建布局文件fragment_app.xml
6、创建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、创建布局文件fragment_game.xml
8、创建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、创建布局文件fragment_movie.xml
10、回到 MainActivity类,添加Tabs到ActionBar中,并处理点击滑动事件。完整代码
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 ={"应用","游戏","视频"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取viewpager viewPager = (ViewPager) findViewById(R.id.pager); //实例化pageradapter mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mTabsPagerAdapter); //获取适配的actionbar actionBar = getSupportActionBar(); //设置home按钮不可点击 actionBar.setHomeButtonEnabled(false); //设置顶部导航的模式 -tabs actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //添加标签 for(String tab:tabs) { actionBar.addTab(actionBar.newTab().setText(tab).setTabListener(this)); } //设置ViewPager切换时候的监听事件 viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { //页面滑动,顶部标签跟着改变 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 } }
代码就是这样的,搞定收工了。示例代码下载