您的位置:首页 > 其它

fragment与底部菜单联动

2016-06-08 15:08 113 查看
<?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);

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: