ViewPager和Tabhost结合,可滑动的tabhost

  1. public class ViewPagerActivity extends Activity {  
  2.   
  3.     List<View> listViews;  
  4.   
  5.     Context context = null;  
  6.   
  7.     LocalActivityManager manager = null;  
  8.   
  9.     TabHost tabHost = null;  
  10.   
  11.     private ViewPager pager = null;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.           
  17.         setContentView(R.layout.viewpager);  
  18.           
  19.         context = ViewPagerActivity.this;  
  20.           
  21.         pager  = (ViewPager) findViewById(R.id.viewpager);  
  22.           
  23.         //定放一个放view的list,用于存放viewPager用到的view  
  24.         listViews = new ArrayList<View>();  
  25.           
  26.         manager = new LocalActivityManager(this, true);  
  27.         manager.dispatchCreate(savedInstanceState);  
  28.           
  29.         Intent i1 = new Intent(context, T1Activity.class);  
  30.         listViews.add(getView("A", i1));  
  31.         Intent i2 = new Intent(context, T2Activity.class);  
  32.         listViews.add(getView("B", i2));  
  33.         Intent i3 = new Intent(context, T3Activity.class);  
  34.         listViews.add(getView("C", i3));  
  35.   
  36.         tabHost = (TabHost) findViewById(R.id.tabhost);  
  37.         tabHost.setup();  
  38.         tabHost.setup(manager);  
  39.           
  40.           
  41.         //这儿主要是自定义一下tabhost中的tab的样式  
  42.         RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget, null);    
  43.         TextView tvTab1 = (TextView)tabIndicator1.findViewById(R.id.tv_title);  
  44.         tvTab1.setText("第一页");  
  45.           
  46.         RelativeLayout tabIndicator2 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget,null);    
  47.         TextView tvTab2 = (TextView)tabIndicator2.findViewById(R.id.tv_title);  
  48.         tvTab2.setText("第二页");  
  49.           
  50.         RelativeLayout tabIndicator3 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget,null);    
  51.         TextView tvTab3 = (TextView)tabIndicator3.findViewById(R.id.tv_title);  
  52.         tvTab3.setText("第三页");  
  53.           
  54.         Intent intent = new Intent(context,EmptyActivity.class);  
  55.         //注意这儿Intent中的activity不能是自身,所以我弄了个空的acitivity  
  56.         tabHost.addTab(tabHost.newTabSpec("A").setIndicator(tabIndicator1).setContent(intent));  
  57.         tabHost.addTab(tabHost.newTabSpec("B").setIndicator(tabIndicator2).setContent(intent));  
  58.         tabHost.addTab(tabHost.newTabSpec("C").setIndicator(tabIndicator3).setContent(intent));  
  59.           
  60.           
  61.         pager .setAdapter(new MyPageAdapter(listViews));  
  62.         pager .setOnPageChangeListener(new OnPageChangeListener() {  
  63.             @Override  
  64.             public void onPageSelected(int position) {  
  65.                 //当viewPager发生改变时,同时改变tabhost上面的currentTab  
  66.                 tabHost.setCurrentTab(position);  
  67.             }  
  68.             @Override  
  69.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  70.             }  
  71.             @Override  
  72.             public void onPageScrollStateChanged(int arg0) {  
  73.             }  
  74.         });  
  75.           
  76.           
  77.      //点击tabhost中的tab时,要切换下面的viewPager  
  78.      tabHost.setOnTabChangedListener(new OnTabChangeListener() {  
  79.             @Override  
  80.             public void onTabChanged(String tabId) {  
  81.                   
  82.                 if ("A".equals(tabId)) {  
  83.                     pager.setCurrentItem(0);  
  84.                 }   
  85.                 if ("B".equals(tabId)) {  
  86.                       
  87.                     pager.setCurrentItem(1);  
  88.                 }   
  89.                 if ("C".equals(tabId)) {  
  90.                     pager.setCurrentItem(2);  
  91.                 }   
  92.             }  
  93.         });  
  94.       
  95.           
  96.           
  97.     }  
  98.   
  99.     private View getView(String id, Intent intent) {  
  100.         return manager.startActivity(id, intent).getDecorView();  
  101.     }  
  102.   
  103.     private class MyPageAdapter extends PagerAdapter {  
  104.           
  105.         private List<View> list;  
  106.   
  107.         private MyPageAdapter(List<View> list) {  
  108.             this.list = list;  
  109.         }  
  110.   
  111.         @Override  
  112.         public void destroyItem(View view, int position, Object arg2) {  
  113.             ViewPager pViewPager = ((ViewPager) view);  
  114.             pViewPager.removeView(list.get(position));  
  115.         }  
  116.   
  117.         @Override  
  118.         public void finishUpdate(View arg0) {  
  119.         }  
  120.   
  121.         @Override  
  122.         public int getCount() {  
  123.             return list.size();  
  124.         }  
  125.   
  126.         @Override  
  127.         public Object instantiateItem(View view, int position) {  
  128.             ViewPager pViewPager = ((ViewPager) view);  
  129.             pViewPager.addView(list.get(position));  
  130.             return list.get(position);  
  131.         }  
  132.   
  133.         @Override  
  134.         public boolean isViewFromObject(View arg0, Object arg1) {  
  135.             return arg0 == arg1;  
  136.         }  
  137.   
  138.         @Override  
  139.         public void restoreState(Parcelable arg0, ClassLoader arg1) {  
  140.         }  
  141.   
  142.         @Override  
  143.         public Parcelable saveState() {  
  144.             return null;  
  145.         }  
  146.   
  147.         @Override  
  148.         public void startUpdate(View arg0) {  
  149.         }  
  150.   
  151.     }  
  152.   
  153. }  
posted @ 2015-12-08 17:58  请叫我码农怪蜀黍  阅读(259)  评论(0编辑  收藏  举报