Android零基础入门第88节:Fragment显示和隐藏、绑定和解绑

    在上一期我们学习了FragmentManager和FragmentTransaction的作用,并用案例学习了Fragment的添加、移除和替换,本期一起来学习Fragment显示和隐藏、绑定和解绑。

一、Fragment显示和隐藏

    由于上一期有简单介绍过对应的api,这里直接通过案例来进行学习。

    创建一个新的module名为fragmentshowhide,然后创建一个Fragment对应的布局文件fragment_demo.xml,代码如下:

<?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:background="#f1d60d"
              android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是我的第一个Fragment"
        android:textColor="#0c1ff1"
        android:textSize="18sp"/>

</LinearLayout>

    紧接着创建一个Fragment文件,命名为DemoFragment,代码非常简单,如下:

package com.cqkxzsxy.jinyu.android.fragmentshowhide;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DemoFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_demo, container, false);
        return view;
    }
}

    然后就是我们要操作的界面设计了,这里一共包括2个按钮,分别表示隐藏Fragment和显示Fragment,主布局acticity_main文件的代码如下:

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/show_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="show" />

        <Button
            android:id="@+id/hide_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hide" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

    然后就是修改主界面MainActivity的代码,获取按钮并设置监听事件,对应不同的事件做出不同的操作,代码如下:

package com.cqkxzsxy.jinyu.android.fragmentshowhide;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mHideBtn = null;
    private Button mShowBtn = null;
    private Fragment mDemoFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHideBtn = (Button) findViewById(R.id.hide_btn);
        mShowBtn = (Button) findViewById(R.id.show_btn);

        // 创建和获取Fragment实例
        mDemoFragment = new DemoFragment();
        // 获取到FragmentManager对象
        FragmentManager fragmentManager = getFragmentManager();
        // 开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 添加Fragment
        fragmentTransaction.add(R.id.fragment_container, mDemoFragment);
        // 提交事务
        fragmentTransaction.commit();

        // 设置监听事件
        mHideBtn.setOnClickListener(this);
        mShowBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 获取到FragmentManager对象
        FragmentManager fragmentManager = getFragmentManager();
        // 开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // Fragment操作
        switch (v.getId()) {
            case R.id.hide_btn:
                if (!mDemoFragment.isHidden()) {
                    fragmentTransaction.hide(mDemoFragment);
                }
                break;
            case R.id.show_btn:
                if (mDemoFragment.isHidden()) {
                    fragmentTransaction.show(mDemoFragment);
                }
                break;
            default:
                break;
        }

        // 提交事务
        fragmentTransaction.commit();
    }
}

    运行程序,可以看到下图左侧所示界面。

    点击“HIDE”按钮,可将显示出来的Fragment进行隐藏,如上图右侧所示。然后再点击“SHOW”按钮,即可将刚才隐藏的Fragment重新显示出来。

    到这里有的同学就会有疑问了:将Fragment隐藏的时候是否将其销毁了,然后再显示的时候重新新建的?那么接下来通过Logcat来进行验证。

    将DemoFragment的生命周期方法补全,并每一个生命周期方法中加一句Logcat代码,然后重新运行程序。可以发现,无论我们是隐藏还是显示Fragment,没有任何生命周期方法被调用。说明hide操作只是将Fragment变得不可见而已,其本身仍然是存在的,在具体使用的时候需要注意。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值