Android Tab -- 使用Fragment、FragmentManager来实现

原文地址http://blog.csdn.net/crazy1235/article/details/42678877

效果:

代码:https://github.com/ldb-github/Layout_Tab

1、布局:使用LinearLayout布置标签;再使用FrameLayout来布置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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:orientation="horizontal"
        android:weightSum="3">

        <LinearLayout
            android:id="@+id/tab1_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/tab_down"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选项卡一"
                android:textColor="#ffffff"/>

        </LinearLayout>

        <ImageView
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#ffffff"/>

        <LinearLayout
            android:id="@+id/tab2_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/tab"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选项卡二"
                android:textColor="#ffffff"/>

        </LinearLayout>

        <ImageView
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="#ffffff"/>

        <LinearLayout
            android:id="@+id/tab3_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/tab"
            android:gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选项卡"
                android:textColor="#ffffff"/>

        </LinearLayout>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>
fragment_fragmentmanager.xml

1、方法replaceFragment()实现切换Fragment的功能:通过FragmentTransaction的replace方法来替换FrameLayout中的Fragment。

2、改变标签的背景色,来区分选中的Tab与其他Tab。

public class FragmentAndFManagerActivity extends FragmentActivity
        implements View.OnClickListener {
    private static final String LOG_TAG = FragmentAndFManagerActivity.class.getSimpleName();

    // 三个选项卡
    private LinearLayout tab1Layout, tab2Layout, tab3Layout;
    // 默认选中第一个
    private int index = 1;
    // fragment管理类
    private FragmentManager fragmentManager;
    // 三个fragment
    private Fragment tab1Fragment, tab2Fragment, tab3Fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_fragmentmanager);
        fragmentManager = getSupportFragmentManager();
        init();
    }

    /**
     * 初始化控件
     */
    private void init(){
        tab1Layout = (LinearLayout) findViewById(R.id.tab1_layout);
        tab2Layout = (LinearLayout) findViewById(R.id.tab2_layout);
        tab3Layout = (LinearLayout) findViewById(R.id.tab3_layout);

        tab1Layout.setOnClickListener(this);
        tab2Layout.setOnClickListener(this);
        tab3Layout.setOnClickListener(this);

        setDefaultFragment();

    }

    /**
     * 设置默认显示的fragment
     */
    private void setDefaultFragment(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        tab1Fragment = new FragmentAndFManager_Fragment1();
        transaction.replace(R.id.content_layout, tab1Fragment);
        transaction.commit();
    }

    /**
     * 切换fragment
     * @param newFragment
     */
    private void replaceFragment(Fragment newFragment){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(!newFragment.isAdded()) {
            Log.d(LOG_TAG, "In replaceFragment -- replace");
            transaction.replace(R.id.content_layout, newFragment);
            transaction.commit();
        }else{
            Log.d(LOG_TAG, "In replaceFragment -- show");
            transaction.show(newFragment);
        }
    }

    /**
     * 改变选项卡的选中状态
     */
    private void clearStatus(){
        if(index == 1){
            tab1Layout.setBackgroundColor(getResources().getColor(R.color.tab));
        }else if(index == 2){
            tab2Layout.setBackgroundColor(getResources().getColor(R.color.tab));
        }else if(index == 3){
            tab3Layout.setBackgroundColor(getResources().getColor(R.color.tab));
        }
    }

    @Override
    public void onClick(View v) {
        clearStatus();
        switch(v.getId()){
            case R.id.tab1_layout:
                if(tab1Fragment == null){
                    tab1Fragment = new FragmentAndFManager_Fragment1();
                }
                replaceFragment(tab1Fragment);
                tab1Layout.setBackgroundColor(getResources().getColor(R.color.tab_down));
                index = 1;
                break;
            case R.id.tab2_layout:
                if(tab2Fragment == null){
                    tab2Fragment = new FragmentAndFManager_Fragment2();
                }
                replaceFragment(tab2Fragment);
                tab2Layout.setBackgroundColor(getResources().getColor(R.color.tab_down));
                index = 2;
                break;
            case R.id.tab3_layout:
                if(tab3Fragment == null){
                    tab3Fragment = new FragmentAndFManager_Fragment3();
                }
                replaceFragment(tab3Fragment);
                tab3Layout.setBackgroundColor(getResources().getColor(R.color.tab_down));
                index = 3;
                break;
        }
    }
}
FragmentAndFManagerActivity.java
原文地址:https://www.cnblogs.com/yarightok/p/5640573.html