大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > Android -- TabHost、Fragment、状态保存、通信

Android -- TabHost、Fragment、状态保存、通信(2)

关键词:保存状态通信AndroidTabHost  阅读(3376) 赞(15)

[摘要]本文是对Android -- TabHost、Fragment、状态保存、通信的讲解,对学习Android编程技术有所帮助,与大家分享。

main布局

有一个id为tab_content的FrameLayout,用来存放要显示的Fragment。底部有一个RadioGroup,用于tab的切换

<?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:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/tab_content"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1.0"
            android:background="#77557799" />

        <RadioGroup
            android:id="@+id/tabs_rg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:paddingBottom="7dp"
            android:paddingTop="7dp" >

            <RadioButton
                android:id="@+id/tab_rb_a"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/selector_tab"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/tablatestalert"
                android:gravity="center"
                android:singleLine="true"
                android:text="Tab1"
                android:textColor="#000000"
                android:textSize="13sp" />

            <RadioButton
                android:id="@+id/tab_rb_b"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/selector_tab"
                android:button="@null"
                android:drawableTop="@drawable/tabsearch"
                android:gravity="center"
                android:singleLine="true"
                android:text="Tab2"
                android:textColor="#000000"
                android:textSize="13sp" />

            <RadioButton
                android:id="@+id/tab_rb_c"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/selector_tab"
                android:button="@null"
                android:drawableTop="@drawable/tabrecommd"
                android:gravity="center"
                android:singleLine="true"
                android:text="Tab3"
                android:textColor="#000000"
                android:textSize="13sp" />

            <RadioButton
                android:id="@+id/tab_rb_d"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/selector_tab"
                android:button="@null"
                android:drawableTop="@drawable/tabconfigicon"
                android:gravity="center"
                android:singleLine="true"
                android:text="Tab4"
                android:textColor="#000000"
                android:textSize="13sp" />

            <RadioButton
                android:id="@+id/tab_rb_e"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/selector_tab"
                android:button="@null"
                android:drawableTop="@drawable/tababoutus"
                android:gravity="center"
                android:singleLine="true"
                android:text="Tab5"
                android:textColor="#000000"
                android:textSize="13sp" />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>

现在回到MainActivity中,下面这个FragmentTabAdapter类是关键,是我自己编写的用于绑定和处理fragments和RadioGroup之间的逻辑关系

FragmentTabAdapter2 tabAdapter = new FragmentTabAdapter2(this, fragments, R.id.tab_content, rgs);

FragmentTabAdapter2

package com.yydcdut.tabhoststudy;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class FragmentTabAdapter2 {
    private List<Fragment> fragments; // 一个tab页面对应一个Fragment
    private RadioGroup rgs; // 用于切换tab
    private FragmentActivity fragmentActivity; // Fragment所属的Activity
    private int fragmentContentId; // Activity中所要被替换的区域的id

    private int currentTab; // 当前Tab页面索引

    private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // 用于让调用者在切换tab时候增加新的功能

    /**
     * 构造函数
     * 
     * @param fragmentActivity
     * @param fragments
     * @param fragmentContentId
     * @param rgs
     */
    public FragmentTabAdapter2(FragmentActivity fragmentActivity,
            List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;

        // 默认显示第一页
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                .beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        ft.commit();
        // 设置监听事件
        MyCheckedChangeListener myCheckedChangeListener = new MyCheckedChangeListener();
        rgs.setOnCheckedChangeListener(myCheckedChangeListener);

    }

    /**
     * 监听器内部类
     * 
     * @author sansung
     * 
     */
    class MyCheckedChangeListener implements OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int i = 0; i < rgs.getChildCount(); i++) {
                if (rgs.getChildAt(i).getId() == checkedId) {
                    Fragment fragment = fragments.get(i);
                    FragmentTransaction ft = obtainFragmentTransaction(i);

                    getCurrentFragment().onPause(); // 暂停当前tab
                    // getCurrentFragment().onStop(); // 暂停当前tab

                    if (fragment.isAdded()) {
                        // fragment.onStart(); // 启动目标tab的onStart()
                        fragment.onResume(); // 启动目标tab的onResume()
                    } else {
                        ft.add(fragmentContentId, fragment);
                    }
                    showTab(i); // 显示目标tab
                    ft.commit();

                    // 如果设置了切换tab额外功能功能接口
                    if (null != onRgsExtraCheckedChangedListener) {
                        onRgsExtraCheckedChangedListener
                                .OnRgsExtraCheckedChanged(group, checkedId, i);
                    }
                }
            }
        }
    }

    /**
     * 切换tab
     * 
     * @param idx
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);

            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    /**
     * 获取一个带动画的FragmentTransaction
     * 
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                .beginTransaction();
        // 设置切换动画
        if (index > currentTab) {
            ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
        } else {
            ft.setCustomAnimations(R.anim.slide_right_in,
                    R.anim.slide_right_out);
        }
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
        return onRgsExtraCheckedChangedListener;
    }

    public void setOnRgsExtraCheckedChangedListener(
            OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
        this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
    }

    /**
     * 切换tab额外功能功能接口
     */
    static class OnRgsExtraCheckedChangedListener {
        public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
                int checkedId, int index) {
        }
    }
}


相关评论