1. 程式人生 > >Fragment建立新增切換和對應底部導航

Fragment建立新增切換和對應底部導航

示例一:

效果如圖所示

每一個Fragment顯示一個字串,比如

在所有的例子裡面,都有四個簡單Fragment,分別為FragmentA,FragmentB,FragmentC,FragmentD。。

類的內容

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false); }

佈局檔案就是FrameLayout中有一個TextView。

MainActivity的佈局檔案內容可以看出下面四個導航按鈕是RadioButton

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/content" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> </
FrameLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/devider_line" > </View> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <RadioButton android:id="@+id/btn_stimulation" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/stimulation" android:gravity="center_horizontal" android:text="@string/tab_stimulation" android:textColor="@color/yellow" /> <RadioButton android:id="@+id/btn_prescription" ............. /> <RadioButton android:id="@+id/btn_network" .............. /> <RadioButton android:id="@+id/btn_setting" ........ /> </RadioGroup> </LinearLayout>

使用ButterKnife獲取控制元件物件

    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.btn_stimulation)
    RadioButton btnStimulation;
    ............
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

在初始化時新增radioGroup的點選響應事件

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.btn_stimulation:
                        selectNavigation(0);
                        break;
                    case R.id.btn_prescription:
                        selectNavigation(1);
                     ........
                }
            }
        });

 按鈕選擇函式selectNavigation就是先將四個變灰色,再把選擇的變成彩色,設定選中,同時呼叫Fragment選擇函式。

    private void selectNavigation(int page) {
        //四個導航按鈕繪製成灰色
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            Drawable grey_image = getResources().getDrawable(greyed[i]);
            grey_image.setBounds(0, 0, grey_image.getMinimumWidth(),
                    grey_image.getMinimumHeight());
            RadioButton child = (RadioButton) radioGroup.getChildAt(i);
            child.setCompoundDrawables(null, grey_image, null, null);
            child.setTextColor(getResources().getColor(
                    R.color.dark_gray));

        }
        //將指定按鈕繪製成彩色,並設定選中
        Drawable yellow = getResources().getDrawable(colored[page]);
        yellow.setBounds(0, 0, yellow.getMinimumWidth(),
                yellow.getMinimumHeight());
        RadioButton select = (RadioButton) radioGroup.getChildAt(page);
        select.setCompoundDrawables(null, yellow, null, null);
        select.setTextColor(getResources().getColor(
                R.color.yellow));
        select.setChecked(true);

        // 選擇導航的時候,同時選擇Fragment
        showFragment(page);
    }

其中greyed和colored就是底部四個灰色和彩色資原始檔id陣列。

 

Fragment的宣告,建立和顯示

 

    FragmentA fragmentA;
    FragmentB fragmentB;
    FragmentC fragmentC;
    FragmentD fragmentD;
    List<Fragment> fragments = new ArrayList<Fragment>();

在onCreate的時候建立Fragment,呼叫InitFragments

    private void InitFragments() {
        fragments.clear();

        if (fragmentA == null) {
            fragmentA = new FragmentA();
        }
        if (fragmentB == null) {
            fragmentB = new FragmentB();
        }
        if (fragmentC == null) {
            fragmentC = new FragmentC();
        }
        if (fragmentD == null) {
            fragmentD = new FragmentD();
        }
        fragments.add(fragmentA);
        fragments.add(fragmentB);
        fragments.add(fragmentC);
        fragments.add(fragmentD);
        for (Fragment frag : fragments) {
            if (!frag.isAdded()) {
                getSupportFragmentManager().beginTransaction().add(R.id.content, frag).commit();
            }
        }
    }

在切換導航的時候同時呼叫切換Fragment函式

    private void showFragment(int frag) {
        //首先隱藏所有Fragments
        for (Fragment f : fragments) {
            getSupportFragmentManager().beginTransaction().hide(f).commit();
        }
        //獲取當前 序號的fragment
        Fragment current_frag = fragments.get(frag);
        if (current_frag != null) {
            getSupportFragmentManager().beginTransaction().show(current_frag).commit();

        }

    }