不一样的TabHost

发布时间:2014-10-22 20:00:05编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"不一样的TabHost",主要涉及到不一样的TabHost方面的内容,对于不一样的TabHost感兴趣的同学可以参考一下。

package com.example.actionbartabs; import java.util.ArrayList; import java.util.List; import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.Activity; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { ActionBar mActionBar;  //bar ViewPager mViewPager; //page MyPagerAdapter mTabPagerAdapter;  //一个类 List<View> mTabPagerList = new ArrayList<View>();  //view   arrayList @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InitActionBar(); //init InitViewPager();  //init } private void InitActionBar() { MyTabListener mtabListener = new MyTabListener(); mActionBar = getActionBar(); mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); mActionBar.addTab(mActionBar.newTab().setText("Tab 1") .setTabListener(mtabListener)); mActionBar.addTab(mActionBar.newTab().setText("Tab 2") .setTabListener(mtabListener)); mActionBar.addTab(mActionBar.newTab().setText("Tab 3") .setTabListener(mtabListener)); } private void InitViewPager() { mViewPager = (ViewPager) findViewById(R.id.viewpager); LayoutInflater mInflater = getLayoutInflater(); mTabPagerList.add(mInflater.inflate(R.layout.lay1, null)); mTabPagerList.add(mInflater.inflate(R.layout.lay2, null)); mTabPagerList.add(mInflater.inflate(R.layout.lay3, null)); mViewPager.setAdapter(new MyPagerAdapter(mTabPagerList)); mViewPager.setCurrentItem(0); mViewPager.setOnPageChangeListener(new MyOnPageChangeListener()); } private class MyPagerAdapter extends PagerAdapter { public List<View> mListViews; public MyPagerAdapter(List<View> tlist) { this.mListViews = tlist; } @Override public int getCount() { return mListViews.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == (object); } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(mListViews.get(position)); } @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView(mListViews.get(position), 0); return mListViews.get(position); } } private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } @Override public void onPageScrollStateChanged(int state) { } } private class MyTabListener implements ActionBar.TabListener { @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { for (int i = 0; i < mTabPagerList.size(); i++) { if (i == tab.getPosition()) { mViewPager.setCurrentItem(i); } } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { } } }


上一篇:json2.js处理javascript json数据
下一篇:北风网--3G-安卓零基础就业班

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款