FragmentTabHost+Fragment的用法

FragmentTabHost+Fragment的用法

之前一直有用Radiobutton+Fragment处理app底部的导航栏,也蛮顺手的。而最近维护的项目,用到的FragmentTabHost+fragment于是学习下。

用到的布局文件main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/main_content"
        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="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    android.support.v4.app.FragmentTabHost>

LinearLayout>

item_tab_home.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/ivTabView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
    <TextView
        android:id="@+id/tvContent"
        style="@style/TabBarTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
LinearLayout>

这两处与网上相关FragmentTab的布局差不多

tab_project_selector

切换用到的选择器这里就写两个吧


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_project_selected"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/tab_project_selected" android:state_activated="true" />
    <item android:drawable="@drawable/tab_project_unselected" />
selector>

tab_more_selector.xml


<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/tab_more_selected" />
    <item android:state_activated="true" android:drawable="@drawable/tab_more_selected"/>
    <item android:drawable="@drawable/tab_more_unselected" />
selector>

Fragmet的布局
fragment_attion.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.szcares.jpbao.ui.fragment.MoreFragment">

    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="关注" />

FrameLayout>

fragment_more.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.szcares.jpbao.ui.fragment.MoreFragment">

    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="更多" />

FrameLayout>

主代码
之前查网上运行实例都是能切换底部字体颜色和背景,但好像不是!

“`
public class MainActivity extends BaseFragmentActivity {

private FragmentTabHost mTabHost;

private int[] mTabImage = {R.drawable.tab_project_selector,  R.drawable.tab_more_selector};
//用来转载底部视图
private List mView = new ArrayList();
private String mTextviewArray[]={"关注","更多"};
private Class fragmentArray[] = {AttentionFragment.class, MoreFragment.class};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initTabHost();//初始化底部菜单
}

/**
 * 初始化底部菜单
 */
private void initTabHost() {
    //
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.main_content);
    int count = fragmentArray.length;
    for (int i = 0; i < count; i++) {
        TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
                .setIndicator(getTabItemView(i));
        mTabHost.addTab(tabSpec, fragmentArray[i], null);
        mTabHost.getTabWidget().getChildAt(i)
                .setBackgroundResource(R.color.white);
    }
    //默认展示关注页面
    mTabHost.setCurrentTabByTag(mTagArray[0]);
    changeTabbarUI(mView.get(0));
    //消除切换按钮之间有横线
    mTabHost.getTabWidget().setDividerDrawable(null);

    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            mTabHost.setCurrentTabByTag(tabId);
            if("关注".equals(tabId)){
                changeTabbarUI(mView.get(0));
            }else if("更多".equals(tabId)){
                changeTabbarUI(mView.get(1));
            }
        }
    });
}

/**
 * 初始化底部
 * @param index
 * @return
 */
private View getTabItemView(int index) {
    View  view  = View.inflate(this,R.layout.item_tab_home,null);
    ImageView imageView = (ImageView) view.findViewById(R.id.ivTabView);
    imageView.setBackgroundResource(mTabImage[index]);
    //imageView.setImageResource(mTabImage[index]);
    TextView textView = (TextView) view.findViewById(R.id.tvContent);
    textView.setText(mTextviewArray[index]);
    mView.add(view);
    return view;
}

/**
 * 更改显示的tab
 *
 * @param v
 */
private void changeTabbarUI(View v) {
    v.setActivated(true);
    ViewGroup parentView = (ViewGroup) v.getParent();
    for (int i = 0; i < parentView.getChildCount(); i++) {
        View childView = parentView.getChildAt(i);
        if (childView != v) {
            childView.setActivated(false);
        }
    }
}

}

你可能感兴趣的:(android)