FragmentTabHost+Fragment的用法

FragmentTabHost+Fragment的用法

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

用到的布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<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

<?xml version="1.0" encoding="utf-8"?>
<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

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

<?xml version="1.0" encoding="utf-8"?>
<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

<?xml version="1.0" encoding="utf-8"?>
<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">

    <!-- TODO: Update blank fragment layout -->
    <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">

    <!-- TODO: Update blank fragment layout -->
    <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<View> mView = new ArrayList<View>();
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);
        }
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值