FragmentTabhost的使用(保存fragment的状态)

这是一个关于小小的FragmentTabhost的使用范例,

android已经不推荐使用tabhost,现在一般都用fragmenttabhost,记住要使用v包来支持,切勿导错包


1、主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/real_tab_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tab_host"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/maintab_toolbar_bg">

        <FrameLayout
            android:id="@+id/tab_content"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0">

        </FrameLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

2、tab_item_view.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"
    android:gravity="center">

    <ImageView
        android:id="@+id/tab_itme_image"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:focusable="false"
        android:padding="3dp"
        android:src="@drawable/hand_green_no"/>
    
    <TextView
        android:id="@+id/tab_item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="10sp"
        android:textColor="#ffffff"/>

</LinearLayout>


3、各个简单的fragmentview.xml (fragmentview1.xml多一个EditView, 测试fragment的状态保存,其他只简单修改背景颜色)


<?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:background="#ff0000"
    android:orientation="vertical">

    <EditText
        android:layout_marginTop="150dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="input something"/>

</LinearLayout>


4、各个tab对应的fragment

package demo.test.com.cn.fragmenttabhost;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * Created by Administrator on 2015/10/26.
 */
public class Fragment_1 extends Fragment{

    /**
     * 缓存view,保存fragment状态
     */
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if(rootView == null){
            rootView=inflater.inflate(R.layout.fragment_view_1, null);
        }
        //缓存的rootView需要判断是否已经被加过parent, 如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }
}

5、main方法

package demo.test.com.cn.fragmenttabhost;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    /**
     * FragmentTabHost
     */
    private FragmentTabHost mTabHost;
    /**
     * 布局填充器
     */
    private LayoutInflater mLayoutInflater;
    /**
     * Fragment数组
     */
    private Class mFragmentArray[] ={Fragment_1.class, Fragment_2.class, Fragment_3.class, Fragment_4.class, Fragment_5.class};
    /**
     * tab文字数组
     */
    private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };
    /**
     * tab图标
     */
    private int mImageArray[] = {R.drawable.main_uncheck, R.drawable.main_product_uncheck
            , R.drawable.hand_green_no, R.drawable.main_car_uncheck, R.drawable.main_mine_uncheck};

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        mLayoutInflater = LayoutInflater.from(this);
        //找到tabhost
        mTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content);
        int count = mFragmentArray.length;
        for (int i = 0; i < count; i++) {
            //给每个tab按钮设置图标、文字和内容
            FragmentTabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
                    .setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, mFragmentArray[i], null);
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.selector_tab_background);
        }
    }

    /**
     * 给每个tabItem设置图标和文字
     * @param index
     * @return
     */
    private View getTabItemView (int index) {
        View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView itemImage = (ImageView) view.findViewById(R.id.tab_itme_image);
        itemImage.setImageResource(mImageArray[index]);
        TextView itemText = (TextView) view.findViewById(R.id.tab_item_text);
        itemText.setText(mTextArray[index]);

        return view;
    }
}

以上为一个简单的fragmenttabhost使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值