Android 碎片Fragment讲解

好了废话不多说.看核心代码.下面有源码下载哦.

/**
 * Project Name:com.zhuxiaohao.tab_05
 * File Name:MainFragmentActivity.java
 * Package Name:com.zhuxiaohao.tab_05
 * Date:2015-4-12上午10:15:00
 * Copyright (c) 2015, zhuxiaohao All Rights Reserved.
 *
 */

package com.zhuxiaohao.tab_05;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * ClassName:MainFragmentActivity <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2015-4-12 上午10:15:00 <br/>
 * 
 * @author chenhao
 * @version
 * @since JDK 1.6
 * @see
 */
public class MainFragmentActivity extends FragmentActivity implements
        OnClickListener {
    private Context context;

    Tab_1_fragment tab_1_fragment;
    Tab_2_fragment tab_2_fragment;
    Tab_3_fragment tab_3_fragment;
    Tab_4_fragment tab_4_fragment;

    TextView txt_tab_1;
    TextView txt_tab_2;
    TextView txt_tab_3;
    TextView txt_tab_4;

    FragmentManager fragmentManager;

    private int number = 1;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        setContentView(R.layout.activity_main);
        context = getApplicationContext();
        fragmentManager = getSupportFragmentManager();
        initView();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        outState.putBoolean("MainFragment", true);
        super.onSaveInstanceState(outState);
    }

    /**
     * 
     * initView:(初始化). <br/>
     * 
     * @author chenhao
     * @since JDK 1.6
     */
    private void initView() {
        // TODO Auto-generated method stub
        txt_tab_1 = (TextView) findViewById(R.id.txt_tab_1);
        txt_tab_2 = (TextView) findViewById(R.id.txt_tab_2);
        txt_tab_3 = (TextView) findViewById(R.id.txt_tab_3);
        txt_tab_4 = (TextView) findViewById(R.id.txt_tab_4);
        txt_tab_1.setOnClickListener(this);
        txt_tab_2.setOnClickListener(this);
        txt_tab_3.setOnClickListener(this);
        txt_tab_4.setOnClickListener(this);

        setTabSelection(0);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.txt_tab_1:
            setTabSelection(0);
            break;
        case R.id.txt_tab_2:
            setTabSelection(1);
            break;
        case R.id.txt_tab_3:
            setTabSelection(2);
            break;
        case R.id.txt_tab_4:
            setTabSelection(3);
            break;
        }
    }

    /**
     * 
     * setTabSelection:(根据传入的指数参数设置选中的标签页。). <br/>
     * 
     * @author chenhao
     * @param index
     * @since JDK 1.6
     */
    @SuppressLint({ "NewApi", "ResourceAsColor" })
    public void setTabSelection(int index) {
        resetBtn();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
        case 0:
            ((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_1_fragment == null) {
                tab_1_fragment = new Tab_1_fragment();
                transaction.add(R.id.content_view, tab_1_fragment, index + "");
            } else {
                transaction.show(tab_1_fragment);
            }
            break;
        case 1:
            ((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_2_fragment == null) {
                tab_2_fragment = new Tab_2_fragment();
                transaction.add(R.id.content_view, tab_2_fragment, index + "");
            } else {
                transaction.show(tab_2_fragment);
            }
            break;
        case 2:
            ((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_3_fragment == null) {
                tab_3_fragment = new Tab_3_fragment();
                transaction.add(R.id.content_view, tab_3_fragment, index + "");
            } else {
                transaction.show(tab_3_fragment);
            }
            break;
        case 3:
            ((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_4_fragment == null) {
                tab_4_fragment = new Tab_4_fragment();
                transaction.add(R.id.content_view, tab_4_fragment, index + "");
            } else {
                transaction.show(tab_4_fragment);
            }
            break;

        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 
     * resetBtn:(重置). <br/>
     * 
     * @author chenhao
     * @since JDK 1.6
     */
    @SuppressLint("ResourceAsColor")
    private void resetBtn() {
        ((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
                .setTextColor(getResources().getColorStateList(R.color.black));
        ((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
                .setTextColor(getResources().getColorStateList(R.color.black));
        ((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
                .setTextColor(getResources().getColorStateList(R.color.black));
        ((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
                .setTextColor(getResources().getColorStateList(R.color.black));

    }

    /**
     * 
     * hideFragments:(判断是否隐藏). <br/>
     * 
     * @author chenhao
     * @param transaction
     * @since JDK 1.6
     */
    @SuppressLint("NewApi")
    private void hideFragments(FragmentTransaction transaction) {
        if (tab_1_fragment != null) {
            transaction.hide(tab_1_fragment);
        }
        if (tab_2_fragment != null) {
            transaction.hide(tab_2_fragment);
        }
        if (tab_3_fragment != null) {
            transaction.hide(tab_3_fragment);
        }
        if (tab_4_fragment != null) {
            transaction.hide(tab_4_fragment);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (number < 1) {
                finish();
                return true;
            } else {
                number--;
                Toast.makeText(context, "再按一次,退出应用", Toast.LENGTH_LONG).show();
                return false;
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}

下面说下关键部分代码.


    /**
     * 
     * setTabSelection:(根据传入的指数参数设置选中的标签页。). <br/>
     * 
     * @author chenhao
     * @param index
     * @since JDK 1.6
     */
    @SuppressLint({ "NewApi", "ResourceAsColor" })
    public void setTabSelection(int index) {
        resetBtn();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
        case 0:
            ((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_1_fragment == null) {
                tab_1_fragment = new Tab_1_fragment();
                transaction.add(R.id.content_view, tab_1_fragment, index + "");
            } else {
                transaction.show(tab_1_fragment);
            }
            break;
        case 1:
            ((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_2_fragment == null) {
                tab_2_fragment = new Tab_2_fragment();
                transaction.add(R.id.content_view, tab_2_fragment, index + "");
            } else {
                transaction.show(tab_2_fragment);
            }
            break;
        case 2:
            ((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_3_fragment == null) {
                tab_3_fragment = new Tab_3_fragment();
                transaction.add(R.id.content_view, tab_3_fragment, index + "");
            } else {
                transaction.show(tab_3_fragment);
            }
            break;
        case 3:
            ((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
                    .setTextColor(getResources()
                            .getColorStateList(R.color.blue));
            if (tab_4_fragment == null) {
                tab_4_fragment = new Tab_4_fragment();
                transaction.add(R.id.content_view, tab_4_fragment, index + "");
            } else {
                transaction.show(tab_4_fragment);
            }
            break;

        }
        transaction.commitAllowingStateLoss();
    }

通过 传入的参数到这里来判断当前 fragment 是否已经显示过了,如果显示过了,那么直接 show 出来,如果没有那么久 add进去.最后调用

transaction.commitAllowingStateLoss();

commit.这里直接一提的事.fragment 管理器是

fragmentManager = getSupportFragmentManager();

这个方法是重置所有 tab的选择状态.

/**
     * 
     * resetBtn:(重置). <br/>
     * 
     * @author chenhao
     * @since JDK 1.6
     */
    @SuppressLint("ResourceAsColor")
    private void resetBtn() {
        ((TextView) txt_tab_1.findViewById(R.id.txt_tab_1))
                .setTextColor(getResources().getColorStateList(R.color.black));
        ((TextView) txt_tab_2.findViewById(R.id.txt_tab_2))
                .setTextColor(getResources().getColorStateList(R.color.black));
        ((TextView) txt_tab_3.findViewById(R.id.txt_tab_3))
                .setTextColor(getResources().getColorStateList(R.color.black));
        ((TextView) txt_tab_4.findViewById(R.id.txt_tab_4))
                .setTextColor(getResources().getColorStateList(R.color.black));

    }

这个方法是判断是够隐藏


    /**
     * 
     * hideFragments:(判断是否隐藏). <br/>
     * 
     * @author chenhao
     * @param transaction
     * @since JDK 1.6
     */
    @SuppressLint("NewApi")
    private void hideFragments(FragmentTransaction transaction) {
        if (tab_1_fragment != null) {
            transaction.hide(tab_1_fragment);
        }
        if (tab_2_fragment != null) {
            transaction.hide(tab_2_fragment);
        }
        if (tab_3_fragment != null) {
            transaction.hide(tab_3_fragment);
        }
        if (tab_4_fragment != null) {
            transaction.hide(tab_4_fragment);
        }
    }

其他的东西太简单了,就不 贴出来了,下方有源码.

点我下载源代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值