标签:fragmenttabhost viewpager 微信
最近看到很多界面主页都差不多,决定研究研究写出来,以后直接拿来用,不做代码的轮子,多总结,多学习
还是废话少说,先上图
介绍一下我的代码:
首先是布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/black" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
下面就是MainActivity了:
package com.sdufe.thea.framework; import java.util.ArrayList; import java.util.List; import java.util.zip.Inflater; import android.os.Bundle; import android.R.integer; import android.annotation.SuppressLint; import android.app.Activity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; 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.Menu; 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; public class MainActivity extends FragmentActivity implements OnPageChangeListener, OnTabChangeListener { private FragmentTabHost mTabHost; private LayoutInflater layoutInflater; private Class fragmentArray[] = { Fragment1.class, Fragment.class, Fragment3.class, Fragment4.class }; private int imageViewArray[] = { R.drawable.mywork, R.drawable.mypatient, R.drawable.infusion, R.drawable.personal }; private String textViewArray[] = { "工作", "病人", "互动", "个人中心" }; private List<Fragment> list = new ArrayList<Fragment>(); private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_tab_layout); initView(); initPage(); } /** * 控件初始化 */ private void initView() { vp = (ViewPager) findViewById(R.id.pager); vp.setOnPageChangeListener(this); layoutInflater = LayoutInflater.from(this); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.pager); mTabHost.setOnTabChangedListener(this); int count = textViewArray.length; for (int i = 0; i < count; i++) { TabSpec tabSpec = mTabHost.newTabSpec(textViewArray[i]) .setIndicator(getTabItemView(i)); mTabHost.addTab(tabSpec, fragmentArray[i], null); mTabHost.setTag(i); } } /** * 初始化Fragment */ private void initPage() { Fragment1 fragment1 = new Fragment1(); Fragment2 fragment2 = new Fragment2(); Fragment3 fragment3 = new Fragment3(); Fragment4 fragment4 = new Fragment4(); list.add(fragment1); list.add(fragment2); list.add(fragment3); list.add(fragment4); vp.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), list)); } private View getTabItemView(int i) { View view = layoutInflater.inflate(R.layout.tab_content, null); ImageView mImageView = (ImageView) view .findViewById(R.id.tab_imageview); TextView mTextView = (TextView) view.findViewById(R.id.tab_textview); mImageView.setBackgroundResource(imageViewArray[i]); mTextView.setText(textViewArray[i]); return view; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { TabWidget widget = mTabHost.getTabWidget(); int oldFocusability = widget.getDescendantFocusability(); widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); mTabHost.setCurrentTab(arg0); widget.setDescendantFocusability(oldFocusability); mTabHost.getTabWidget().getChildAt(arg0) .setBackgroundResource(R.drawable.selector_tab_background); } @Override public void onTabChanged(String tabId) { int position = mTabHost.getCurrentTab(); vp.setCurrentItem(position); } }
源码:https://github.com/zimoguo/FragmentTabHost-ViewPager
使用FragmentTabHost和ViewPager实现仿微信主界面策划
标签:fragmenttabhost viewpager 微信
原文地址:http://blog.csdn.net/elinavampire/article/details/40374011