Android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态

以前在Activity中切换选项卡,fragment一般采取这样的方法,

代码:

        BlankFragment blankFragment = new BlankFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager2.beginTransaction();
        fragmentTransaction.replace(R.id.llContent, blankFragment2);
        fragmentTransaction.commit();
 replace会先查找FragmentManager中是否包含相同mContainerId 的fragment,如果有就先remove它,然后再add它。 

 add fragment对象后,就意味着在容器中加入了一个新对象,这个fragment对象的生命周期方法会挨个执行,

 在选项卡切换时,每切换一次,会重新调用生命周期的方法,如果fragment有网络请求,会导致重新不断请求数据。

 甚至卡顿,使性能大大降低。

 为了解决这个问题,我们采用add方法,添加fragment对象到FragmentTransaction中,在利用show(fragment)和

 hide(fragment);  显示和隐藏即可


 具体demo如下:

效果图

Android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态_第1张图片

项目结构:

Android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态_第2张图片

核心代码:

Main2Activity中
package com.zhh.android;

import android.content.Intent;
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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends FragmentActivity implements View.OnClickListener{
    private Button button1;
    private Button button2;
// fragment对象的标记
    public static final String fragmentTag1 = "fragment1";
    public static final String fragmentTag2 = "fragment2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        myOnclick();
    }
    private void initView() {
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        firstShow();
    }

    private void myOnclick() {
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//      用对应的标记在内存中找对应的Fragment对象
        Fragment  blankFragment1 =  fragmentManager.findFragmentByTag(fragmentTag1);
        Fragment  blankFragment2 =  fragmentManager.findFragmentByTag(fragmentTag2);

        switch (v.getId()) {
            case R.id.button1:
                fragmentTransaction.show(blankFragment1);
                fragmentTransaction.hide(blankFragment2);
                fragmentTransaction.commit();
                break;

            case R.id.button2:
                fragmentTransaction.hide(blankFragment1);
                fragmentTransaction.show(blankFragment2);
                fragmentTransaction.commit();
                break;
        }
    }

    /**
     * 初始化时显示的fragment
     */
    private void firstShow(){
//      实例化碎片对象
        BlankFragment1 blankFragment1 = new BlankFragment1();
        FragmentManager fragmentManager1 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
//      添加Fragment的时候,要传入标记
        fragmentTransaction1.add(R.id.llContent,blankFragment1,fragmentTag1).commit();

//      放对象
        BlankFragment2 blankFragment2 = new BlankFragment2();
        FragmentManager fragmentManager2 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction2 =fragmentManager2.beginTransaction();
//      添加Fragment的时候,要传入标记
        fragmentTransaction2.add(R.id.llContent,blankFragment2,fragmentTag2).commit();
        fragmentTransaction2.hide(blankFragment2);
    }

}
activity_main2.xml中


    
        
参考文章:
我的这篇文章仅仅为了说明思路,要看完整的代码参考
http://blog.csdn.net/lovexieyuan520/article/details/50594271
源码下载:
http://download.csdn.net/download/zhaihaohao1/10117670



你可能感兴趣的:(fragment)