fragment的显示 隐藏

移动开发 waitig 574℃ 百度已收录 0评论

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.fragment.example.MainActivity">

    <LinearLayout
        android:layout_above="@+id/foot"
        android:id="@+id/container"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />
<LinearLayout
    android:id="@+id/foot"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="50dp">
    <RadioButton
        android:id="@+id/one"
        android:layout_width="0dp"
        android:button="@null"
        android:gravity="center"
        android:drawableTop="@mipmap/shouye"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/two"
        android:layout_width="0dp"
        android:button="@null"
        android:gravity="center"
        android:drawableTop="@mipmap/tuijian1"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/three"
        android:layout_width="0dp"
        android:button="@null"
        android:gravity="center"
        android:drawableTop="@mipmap/yishuquan"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/four"
        android:layout_width="0dp"
        android:button="@null"
        android:gravity="center"
        android:drawableTop="@mipmap/wode1"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>

主界面

package com.fragment.example;

import android.app.Activity;
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.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;

public class MainActivity extends FragmentActivity implements View.OnClickListener{

    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;

    private  String saveName;
    //当前显示的fragment
    private static final String STATE_FRAGMENT_SHOW = "STATE_FRAGMENT_SHOW";

    private Fragment currentFragment = new Fragment();
    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        fragmentManager = getSupportFragmentManager();
        if(savedInstanceState!=null){
            //获取“内存重启”时保存的fragment名字
            saveName = savedInstanceState.getString(STATE_FRAGMENT_SHOW);
            //从fragmentManager里面找到fragment

            fragment1 = (Fragment1) fragmentManager.findFragmentByTag(Fragment1.class.getName());

            fragment2 = (Fragment2) fragmentManager.findFragmentByTag(Fragment2.class.getName());

            fragment3= (Fragment3) fragmentManager.findFragmentByTag(Fragment3.class.getName());

            fragment4= (Fragment4) fragmentManager.findFragmentByTag(Fragment4.class.getName());
            if(TextUtils.isEmpty(saveName)){
                //解决重叠问题  1
                fragmentManager.beginTransaction()
                        .show(fragment1)
                        .hide(fragment2)
                        .hide(fragment3)
                        .hide(fragment4)
                        .commit();

                //把当前显示的fragment记录下来
                currentFragment = fragment1;
            }else{
                if(saveName.equals(fragment1.getClass().getName())){
                    fragmentManager.beginTransaction()
                            .show(fragment1)
                            .hide(fragment2)
                            .hide(fragment3)
                            .hide(fragment4)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fragment1;
                }else if(saveName.equals(fragment2.getClass().getName())){
                    fragmentManager.beginTransaction()
                            .show(fragment2)
                            .hide(fragment1)
                            .hide(fragment3)
                            .hide(fragment4)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fragment2;
                }else if(saveName.equals(fragment3.getClass().getName())){
                    fragmentManager.beginTransaction()
                            .show(fragment3)
                            .hide(fragment1)
                            .hide(fragment2)
                            .hide(fragment4)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fragment3;
                }else if(saveName.equals(fragment4.getClass().getName())){
                    fragmentManager.beginTransaction()
                            .show(fragment4)
                            .hide(fragment1)
                            .hide(fragment2)
                            .hide(fragment3)
                            .commit();

                    //把当前显示的fragment记录下来
                    currentFragment = fragment4;
                }

            }

        }else {//正常启动时调用
            fragment1=new Fragment1();
            fragment2=new Fragment2();
            fragment3=new Fragment3();
            fragment4=new Fragment4();
           showFragment(fragment1);
        }
    }

    /**
     * 初始化控件
     */
    private void initView(){
        RadioButton one= findViewById(R.id.one);
        RadioButton two=findViewById(R.id.two);
        RadioButton three=findViewById(R.id.three);
        RadioButton four=findViewById(R.id.four);

        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
}
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.one:
                showFragment(fragment1);
                break;
            case R.id.two:
                showFragment(fragment2);
                break;
            case R.id.three:
                showFragment(fragment3);
                break;
            case R.id.four:
                showFragment(fragment4);
                break;
}
    }
    /**
     * fragment的显示
     * @param fg
     */
    private void showFragment(Fragment fg){

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        //如果之前没有添加过
        if(!fg.isAdded()){
            transaction
                    .hide(currentFragment)
                    .add(R.id.container,fg,fg.getClass().getName());  //第三个参数为添加当前的fragment时绑定一个tag,即绑定fragment的类名
        }else{
            transaction
                    .hide(currentFragment)
                    .show(fg);
        }

        currentFragment = fg;

        transaction.commit();

    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {

        //“内存重启”时保存当前的fragment名字
        outState.putString(STATE_FRAGMENT_SHOW,currentFragment.getClass().getName());
        super.onSaveInstanceState(outState);
    }
}


本文由【waitig】发表在等英博客
本文固定链接:fragment的显示 隐藏
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)