FragmentTabHost+ViewPager实现底部按钮

 package com.example.fragmenttabdemo;

 import java.util.ArrayList;
import java.util.List; import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView; @SuppressLint("InflateParams")
public class MainActivity extends FragmentActivity {
// 定义FragmentTabHost对象
private FragmentTabHost mTabHost;
// 定义一个布局
private LayoutInflater layoutInflater;
// 定义数组来存放Fragment界面
@SuppressWarnings("rawtypes")
private Class fragmentArray[] = { FragmentPage1.class, FragmentPage1.class,
FragmentPage1.class, FragmentPage1.class, FragmentPage1.class };
// 定义数组来存放按钮图片
private int mImageViewArray[] = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
// Tab选项卡的文字
private String mTextviewArray[] = { "首页", "消息", "好友", "广场", "更多" };
private ViewPager vp;
private List<Fragment> list = new ArrayList<Fragment>(); public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initPager();
} /**
* 初始化组件
*/
private void initView() {
vp = (ViewPager) findViewById(R.id.pager);
vp.setOnPageChangeListener(new ViewPagerListener());
// MyAdapter adapter = new MyAdapter();
// vp.setAdapter(adapter);
// 实例化布局对象
layoutInflater = LayoutInflater.from(this);
// 实例化TabHost对象,得到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// 得到对象后,初始化
// mTabHost.getTabWidget().setDividerDrawable(android.R.color.transparent);
mTabHost.setup(this, getSupportFragmentManager(), R.id.pager);
mTabHost.setOnTabChangedListener(new TabHostListener());
// 得到fragment的个数
int count = fragmentArray.length; for (int i = 0; i < count; i++) {
// 为每一个Tab按钮设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
.setIndicator(getTabItemView(i));
// 将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
mTabHost.setTag(i);
mTabHost.getTabWidget().setDividerDrawable(
android.R.color.transparent);
// 设置Tab按钮的背景
// mTabHost.getTabWidget().getChildAt(i)
// .setBackgroundResource(R.drawable.selector_tab_background);
}
} //初始化viewpager
private void initPager() {
FragmentPage1 p1 = new FragmentPage1();
FragmentPage1 p2 = new FragmentPage1();
FragmentPage1 p3 = new FragmentPage1();
FragmentPage1 p4 = new FragmentPage1();
FragmentPage1 p5 = new FragmentPage1();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
} /**
* 给Tab按钮设置图标和文字
*/
private View getTabItemView(int index) {
View view = layoutInflater.inflate(R.layout.btn_bottom, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageViewArray[index]);
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextviewArray[index]);
return view;
} private class TabHostListener implements OnTabChangeListener {
@Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
vp.setCurrentItem(position);
}
} class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
} @Override
public Fragment getItem(int arg0) {
return list.get(arg0);
} @Override
public int getCount() {
return list.size();
} } class ViewPagerListener implements OnPageChangeListener { public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageSelected(int index) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(index);
widget.setDescendantFocusability(oldFocusability);
}
}
}
上一篇:[翻译] - - 直接执行数据库命令


下一篇:图像处理-07-图像的轮廓提取-Robert算子