码迷,mamicode.com
首页 > 其他好文 > 详细

FragmentTabHost+ViewPager实现底部按钮

时间:2015-03-18 20:03:56      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.example.fragmenttabdemo;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.annotation.SuppressLint;
  7 import android.os.Bundle;
  8 import android.support.v4.app.Fragment;
  9 import android.support.v4.app.FragmentActivity;
 10 import android.support.v4.app.FragmentManager;
 11 import android.support.v4.app.FragmentPagerAdapter;
 12 import android.support.v4.app.FragmentTabHost;
 13 import android.support.v4.view.ViewPager;
 14 import android.support.v4.view.ViewPager.OnPageChangeListener;
 15 import android.view.LayoutInflater;
 16 import android.view.View;
 17 import android.view.ViewGroup;
 18 import android.widget.ImageView;
 19 import android.widget.TabHost.OnTabChangeListener;
 20 import android.widget.TabHost.TabSpec;
 21 import android.widget.TabWidget;
 22 import android.widget.TextView;
 23 
 24 @SuppressLint("InflateParams")
 25 public class MainActivity extends FragmentActivity {
 26     // 定义FragmentTabHost对象
 27     private FragmentTabHost mTabHost;
 28     // 定义一个布局
 29     private LayoutInflater layoutInflater;
 30     // 定义数组来存放Fragment界面
 31     @SuppressWarnings("rawtypes")
 32     private Class fragmentArray[] = { FragmentPage1.class, FragmentPage1.class,
 33             FragmentPage1.class, FragmentPage1.class, FragmentPage1.class };
 34     // 定义数组来存放按钮图片
 35     private int mImageViewArray[] = { R.drawable.ic_launcher,
 36             R.drawable.ic_launcher, R.drawable.ic_launcher,
 37             R.drawable.ic_launcher, R.drawable.ic_launcher };
 38     // Tab选项卡的文字
 39     private String mTextviewArray[] = { "首页", "消息", "好友", "广场", "更多" };
 40     private ViewPager vp;
 41     private List<Fragment> list = new ArrayList<Fragment>();
 42 
 43     public void onCreate(Bundle savedInstanceState) {
 44         super.onCreate(savedInstanceState);
 45         setContentView(R.layout.activity_main);
 46         initView();
 47         initPager();
 48     }
 49 
 50     /**
 51      * 初始化组件
 52      */
 53     private void initView() {
 54         vp = (ViewPager) findViewById(R.id.pager);
 55         vp.setOnPageChangeListener(new ViewPagerListener());
 56         // MyAdapter adapter = new MyAdapter();
 57         // vp.setAdapter(adapter);
 58         // 实例化布局对象
 59         layoutInflater = LayoutInflater.from(this);
 60         // 实例化TabHost对象,得到TabHost
 61         mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
 62         // 得到对象后,初始化
 63         // mTabHost.getTabWidget().setDividerDrawable(android.R.color.transparent);
 64         mTabHost.setup(this, getSupportFragmentManager(), R.id.pager);
 65         mTabHost.setOnTabChangedListener(new TabHostListener());
 66         // 得到fragment的个数
 67         int count = fragmentArray.length;
 68 
 69         for (int i = 0; i < count; i++) {
 70             // 为每一个Tab按钮设置图标、文字和内容
 71             TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
 72                     .setIndicator(getTabItemView(i));
 73             // 将Tab按钮添加进Tab选项卡中
 74             mTabHost.addTab(tabSpec, fragmentArray[i], null);
 75             mTabHost.setTag(i);
 76             mTabHost.getTabWidget().setDividerDrawable(
 77                     android.R.color.transparent);
 78             // 设置Tab按钮的背景
 79             // mTabHost.getTabWidget().getChildAt(i)
 80             // .setBackgroundResource(R.drawable.selector_tab_background);
 81         }
 82     }
 83 
 84     //初始化viewpager
 85     private void initPager() {
 86         FragmentPage1 p1 = new FragmentPage1();
 87         FragmentPage1 p2 = new FragmentPage1();
 88         FragmentPage1 p3 = new FragmentPage1();
 89         FragmentPage1 p4 = new FragmentPage1();
 90         FragmentPage1 p5 = new FragmentPage1();
 91         list.add(p1);
 92         list.add(p2);
 93         list.add(p3);
 94         list.add(p4);
 95         list.add(p5);
 96         vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
 97     }
 98 
 99     /**
100      * 给Tab按钮设置图标和文字
101      */
102     private View getTabItemView(int index) {
103         View view = layoutInflater.inflate(R.layout.btn_bottom, null);
104         ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
105         imageView.setImageResource(mImageViewArray[index]);
106         TextView textView = (TextView) view.findViewById(R.id.textview);
107         textView.setText(mTextviewArray[index]);
108         return view;
109     }
110 
111     private class TabHostListener implements OnTabChangeListener {
112         @Override
113         public void onTabChanged(String tabId) {
114             int position = mTabHost.getCurrentTab();
115             vp.setCurrentItem(position);
116         }
117     }
118 
119     class MyAdapter extends FragmentPagerAdapter {
120 
121         public MyAdapter(FragmentManager fm) {
122             super(fm);
123             // TODO Auto-generated constructor stub
124         }
125 
126         @Override
127         public Fragment getItem(int arg0) {
128             return list.get(arg0);
129         }
130 
131         @Override
132         public int getCount() {
133             return list.size();
134         }
135 
136     }
137 
138     class ViewPagerListener implements OnPageChangeListener {
139 
140         public void onPageScrollStateChanged(int arg0) {
141 
142         }
143 
144         public void onPageScrolled(int arg0, float arg1, int arg2) {
145 
146         }
147 
148         public void onPageSelected(int index) {
149             TabWidget widget = mTabHost.getTabWidget();
150             int oldFocusability = widget.getDescendantFocusability();
151             widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
152             mTabHost.setCurrentTab(index);
153             widget.setDescendantFocusability(oldFocusability);
154         }
155     }
156 }

 

FragmentTabHost+ViewPager实现底部按钮

标签:

原文地址:http://www.cnblogs.com/jinglecode/p/4348328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!