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:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_main_fragment_sport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:drawableTop="@drawable/tab_sport_selector"
            android:paddingTop="7dp"
            android:text="运动"
            android:textColor="@color/main_botton_text_color" />

        <Button
            android:id="@+id/btn_main_fragment_discover"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:drawableTop="@drawable/tab_discover_selector"
            android:paddingTop="7dp"
            android:text="发现"
            android:textColor="@color/main_botton_text_color" />

        <Button
            android:id="@+id/btn_main_fragment_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:drawableTop="@drawable/tab_msg_selector"
            android:paddingTop="7dp"
            android:text="消息"
            android:textColor="@color/main_botton_text_color" />

        <Button
            android:id="@+id/btn_main_fragment_me"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:drawableTop="@drawable/tab_me_selector"
            android:paddingTop="7dp"
            android:text="我"
            android:textColor="@color/main_botton_text_color" />
    </LinearLayout>

</LinearLayout>


package com.tarena.allrun.view;

import com.tarena.allrun.R;
import com.tarena.allrun.TApplication;
import com.tarena.allrun.util.ExceptionUtil;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainFragmentActivity extends FragmentActivity {
    SportFragment sportFragment;
    DiscoverFragment discoverFragment;
    MessageFragment messageFragment;
    MeFragment meFragment;
    Button[] btnArray = new Button[4];

    Fragment[] fragmentArray = null;
    /**
     * 当前显示的fragment
     */
    int currentIndex = 0;
    /**
     * 选中的button,显示下一个fragment
     */
    int selectedIndex;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        try {
            TApplication.listActivity.add(this);
            setContentView(R.layout.main_fragment);
            setupView();
            addListener();
        } catch (Exception e) {
            ExceptionUtil.handleException(e);
        }
    }

    private void addListener() {
        MyButtonListener myButtonListener = new MyButtonListener();
        for (int i = 0; i < btnArray.length; i++) {
            btnArray[i].setOnClickListener(myButtonListener);
        }
    }

    private void setupView() {
        btnArray[0] = (Button) findViewById(R.id.btn_main_fragment_sport);
        btnArray[1] = (Button) findViewById(R.id.btn_main_fragment_discover);
        btnArray[2] = (Button) findViewById(R.id.btn_main_fragment_message);
        btnArray[3] = (Button) findViewById(R.id.btn_main_fragment_me);
        btnArray[0].setSelected(true);

        sportFragment = new SportFragment();
        discoverFragment = new DiscoverFragment();
        messageFragment = new MessageFragment();
        meFragment = new MeFragment();
        fragmentArray = new Fragment[] { sportFragment, discoverFragment,
                messageFragment, meFragment };

        // 一开始,显示第一个fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment_container, sportFragment);
        transaction.show(sportFragment);
        transaction.commit();

    }

    class MyButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            try {
                switch (v.getId()) {
                case R.id.btn_main_fragment_sport:
                    selectedIndex = 0;
                    break;
                case R.id.btn_main_fragment_discover:
                    selectedIndex = 1;
                    break;
                case R.id.btn_main_fragment_message:
                    selectedIndex = 2;
                    break;
                case R.id.btn_main_fragment_me:
                    selectedIndex = 3;
                    break;
                }

                // 判断单击是不是当前的
                if (selectedIndex != currentIndex) {
                    // 不是当前的
                    FragmentTransaction transaction = getSupportFragmentManager()
                            .beginTransaction();
                    // 当前hide
                    transaction.hide(fragmentArray[currentIndex]);
                    // show你选中

                    if (!fragmentArray[selectedIndex].isAdded()) {
                        // 以前没添加过
                        transaction.add(R.id.fragment_container,
                                fragmentArray[selectedIndex]);
                    }
                    // 事务
                    transaction.show(fragmentArray[selectedIndex]);
                    transaction.commit();

                    btnArray[currentIndex].setSelected(false);
                    btnArray[selectedIndex].setSelected(true);
                    currentIndex = selectedIndex;

                }
            } catch (Exception e) {
                ExceptionUtil.handleException(e);
            }

        }

    }
}













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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值