Fragment的显示和隐藏

Fragment的显示和隐藏
效果图
在这里插入图片描述
布局

<?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:background="#F5F5F5"
    android:orientation="vertical" >
    
    <!--流程-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FFFFFF"
        android:orientation="horizontal">

        <!--基本信息-->
        <LinearLayout
            android:id="@+id/llBaseInfo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:orientation="horizontal">

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#00000000" />

                <TextView
                    android:id="@+id/tvBaseInfoNum"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:background="@drawable/tv_bg_blue"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="#FFF"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#D8E0E9" />
            </LinearLayout>

            <TextView
                android:id="@+id/tvBaseInfoTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:gravity="center"
                android:lines="1"
                android:text="基本信息"
                android:textColor="#2368C8"
                android:textSize="16sp" />

        </LinearLayout>

        <!--人员信息-->
        <LinearLayout
            android:id="@+id/llPeopleInfo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:orientation="horizontal">

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#D8E0E9" />

                <TextView
                    android:id="@+id/tvPeopleInfoNum"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:background="@drawable/tv_bg_gray"
                    android:gravity="center"
                    android:text="2"
                    android:textColor="#FFF"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#D8E0E9" />
            </LinearLayout>

            <TextView
                android:id="@+id/tvPeopleInfoTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:gravity="center"
                android:lines="1"
                android:text="人员信息"
                android:textColor="#A4A4A4"
                android:textSize="16sp" />

        </LinearLayout>

        <!--上传附件-->
        <LinearLayout
            android:id="@+id/llAttachmentInfo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:orientation="horizontal">

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#D8E0E9" />

                <TextView
                    android:id="@+id/tvAttachmentInfoNum"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:background="@drawable/tv_bg_gray"
                    android:gravity="center"
                    android:text="3"
                    android:textColor="#FFF"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#D8E0E9" />
            </LinearLayout>

            <TextView
                android:id="@+id/tvAttachmentInfoTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:gravity="center"
                android:lines="1"
                android:text="上传附件"
                android:textColor="#A4A4A4"
                android:textSize="16sp" />

        </LinearLayout>

        <!--确认文书-->
        <LinearLayout
            android:id="@+id/llConfirmBook"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:orientation="horizontal">

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#D8E0E9" />

                <TextView
                    android:id="@+id/tvConfirmBookNum"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:background="@drawable/tv_bg_gray"
                    android:gravity="center"
                    android:text="4"
                    android:textColor="#FFF"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="#00000000" />
            </LinearLayout>

            <TextView
                android:id="@+id/tvConfirmBookTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:gravity="center"
                android:lines="1"
                android:text="确认文书"
                android:textColor="#A4A4A4"
                android:textSize="16sp" />

        </LinearLayout>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/mFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="2dp"
        android:layout_weight="1" />

</LinearLayout>

java代码

    private LinearLayout llBaseInfo;
    private TextView tvBaseInfoNum;
    private TextView tvBaseInfoTitle;
    private LinearLayout llPeopleInfo;
    private TextView tvPeopleInfoNum;
    private TextView tvPeopleInfoTitle;
    private LinearLayout llAttachmentInfo;
    private TextView tvAttachmentInfoNum;
    private TextView tvAttachmentInfoTitle;
    private LinearLayout llConfirmBook;
    private TextView tvConfirmBookNum;
    private TextView tvConfirmBookTitle;


    // fragment
    private FragmentTransaction mFragmentTransaction;   // fragment 事务
    private FragmentManager mFragmentManager;           // fragment 管理器

    private DisputeCreateBaseInfoFragment mDisputeCreateBaseInfoFragment;               // 1
    private DisputeCreatePeopleInfoFragment mDisputeCreatePeopleInfoFragment;           // 2
    private DisputeCreateAttachmentInfoFragment mDisputeCreateAttachmentInfoFragment;   // 3
    private DisputeCreateConfirmBookFragment mDisputeCreateConfirmBookFragment;         // 4


    @Override
    protected void initView() {
        mFragmentManager = getSupportFragmentManager();// 获取到fragment的管理对象

        llBaseInfo = (LinearLayout) findViewById(R.id.llBaseInfo);
        tvBaseInfoNum = (TextView) findViewById(R.id.tvBaseInfoNum);
        tvBaseInfoTitle = (TextView) findViewById(R.id.tvBaseInfoTitle);
        
        llPeopleInfo = (LinearLayout) findViewById(R.id.llPeopleInfo);
        tvPeopleInfoNum = (TextView) findViewById(R.id.tvPeopleInfoNum);
        tvPeopleInfoTitle = (TextView) findViewById(R.id.tvPeopleInfoTitle);
        
        llAttachmentInfo = (LinearLayout) findViewById(R.id.llAttachmentInfo);
        tvAttachmentInfoNum = (TextView) findViewById(R.id.tvAttachmentInfoNum);
        tvAttachmentInfoTitle = (TextView) findViewById(R.id.tvAttachmentInfoTitle);
        
        llConfirmBook = (LinearLayout) findViewById(R.id.llConfirmBook);
        tvConfirmBookNum = (TextView) findViewById(R.id.tvConfirmBookNum);
        tvConfirmBookTitle = (TextView) findViewById(R.id.tvConfirmBookTitle);


        llBaseInfo.setOnClickListener(this);
        llPeopleInfo.setOnClickListener(this);
        llAttachmentInfo.setOnClickListener(this);
        llConfirmBook.setOnClickListener(this);

        showFragment(1);    // 默认显示基础信息
    }
    

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llBaseInfo:
                showFragment(1);
                break;
            case R.id.llPeopleInfo:
                showFragment(2);
                break;
            case R.id.llAttachmentInfo:
                showFragment(3);
                break;
            case R.id.llConfirmBook:
                showFragment(4);
                break;
        }
    }


    /**
     * 显示 fragment
     *
     * @param type 要显示的 Fragment
     */
    private void showFragment(int type) {
        mFragmentTransaction = mFragmentManager.beginTransaction();// 开启事务
        hideFragment(mFragmentTransaction);// 显示之前将所有的fragment都隐藏起来,在去显示我们想要显示的fragment
        updataUI(type); // 更新ui
        switch (type) {
            case 1:
                // 如果该 Fragment 是null的话, 就创建一个
                if (mDisputeCreateBaseInfoFragment == null) {
                    mDisputeCreateBaseInfoFragment = new DisputeCreateBaseInfoFragment();
                    mFragmentTransaction.add(R.id.mFragment, mDisputeCreateBaseInfoFragment);// 加入事务
                } else {
                    // 如果该 Fragment 不为空就显示出来
                    mFragmentTransaction.show(mDisputeCreateBaseInfoFragment);
                }
                break;
            case 2:
                if (mDisputeCreatePeopleInfoFragment == null) {
                    mDisputeCreatePeopleInfoFragment = new DisputeCreatePeopleInfoFragment();
                    mFragmentTransaction.add(R.id.mFragment, mDisputeCreatePeopleInfoFragment);
                } else {
                    mFragmentTransaction.show(mDisputeCreatePeopleInfoFragment);
                }
                break;
            case 3:
                if (mDisputeCreateAttachmentInfoFragment == null) {
                    mDisputeCreateAttachmentInfoFragment = new DisputeCreateAttachmentInfoFragment();
                    mFragmentTransaction.add(R.id.mFragment, mDisputeCreateAttachmentInfoFragment);
                } else {
                    mFragmentTransaction.show(mDisputeCreateAttachmentInfoFragment);
                }
                break;
            case 4:
                if (mDisputeCreateConfirmBookFragment == null) {
                    mDisputeCreateConfirmBookFragment = new DisputeCreateConfirmBookFragment();
                    mFragmentTransaction.add(R.id.mFragment, mDisputeCreateConfirmBookFragment);
                } else {
                    mFragmentTransaction.show(mDisputeCreateConfirmBookFragment);
                }
                break;
        }
        mFragmentTransaction.commit();// 提交事务
    }


    /**
     * 隐藏 fragment
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        // 如果该 Fragment 不为空的话就隐藏起来
        if (mDisputeCreateBaseInfoFragment != null) {
            fragmentTransaction.hide(mDisputeCreateBaseInfoFragment);
        }
        if (mDisputeCreatePeopleInfoFragment != null) {
            fragmentTransaction.hide(mDisputeCreatePeopleInfoFragment);
        }
        if (mDisputeCreateAttachmentInfoFragment != null) {
            fragmentTransaction.hide(mDisputeCreateAttachmentInfoFragment);
        }
        if (mDisputeCreateConfirmBookFragment != null) {
            fragmentTransaction.hide(mDisputeCreateConfirmBookFragment);
        }
    }


    // 修改按钮的状态
    private void updataUI(int type) {
        // 为选中状态: tv_bg_gray   "#A4A4A4"    选中状态: tv_bg_blue   "#2368C8"
        tvBaseInfoNum.setBackground(getDrawable(R.drawable.tv_bg_gray));
        tvBaseInfoTitle.setTextColor(Color.parseColor("#A4A4A4"));

        tvPeopleInfoNum.setBackground(getDrawable(R.drawable.tv_bg_gray));
        tvPeopleInfoTitle.setTextColor(Color.parseColor("#A4A4A4"));

        tvAttachmentInfoNum.setBackground(getDrawable(R.drawable.tv_bg_gray));
        tvAttachmentInfoTitle.setTextColor(Color.parseColor("#A4A4A4"));

        tvConfirmBookNum.setBackground(getDrawable(R.drawable.tv_bg_gray));
        tvConfirmBookTitle.setTextColor(Color.parseColor("#A4A4A4"));

        switch (type) {
            case 1:
                tvBaseInfoNum.setBackground(getDrawable(R.drawable.tv_bg_blue));
                tvBaseInfoTitle.setTextColor(Color.parseColor("#2368C8"));
                break;
            case 2:
                tvPeopleInfoNum.setBackground(getDrawable(R.drawable.tv_bg_blue));
                tvPeopleInfoTitle.setTextColor(Color.parseColor("#2368C8"));
                break;
            case 3:
                tvAttachmentInfoNum.setBackground(getDrawable(R.drawable.tv_bg_blue));
                tvAttachmentInfoTitle.setTextColor(Color.parseColor("#2368C8"));
                break;
            case 4:
                tvConfirmBookNum.setBackground(getDrawable(R.drawable.tv_bg_blue));
                tvConfirmBookTitle.setTextColor(Color.parseColor("#2368C8"));
                break;
        }
    }

灰色圆背景: tv_bg_gray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="100dp"/>
    <solid android:color="#D8E0E9" />
</shape>

蓝色圆背景: tv_bg_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="100dp"/>
    <solid android:color="#2368C8" />
</shape>

============================= END =============================

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值