Android Fragment隐藏显示调用的方法

//在Activity中加载显示隐藏Fragment
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;

import com.commons.Config;
import com.geluyawangluo.withthenight.activitymy.SignInActivity;
import com.geluyawangluo.withthenight.fragment.CatFragment;
import com.geluyawangluo.withthenight.fragment.ClassificationFragment;
import com.geluyawangluo.withthenight.fragment.InformationFragment;
import com.geluyawangluo.withthenight.fragment.MyFragment;
import com.geluyawangluo.withthenight.fragment.ShoppingFragment;
import com.geluyawangluo.withthenight.widget.ScreenManager;
import com.geluyawangluo.withthenight.widget.TabBar;

public class MainActivity extends Activity {
    private TabBar tabBar;
    private CatFragment catFragment;
    private ClassificationFragment classificationFragment;
    private InformationFragment informationFragment;
    private MyFragment myFragment;
    private ShoppingFragment shoppingFragment;
    private FragmentManager fm;
    private FragmentTransaction transaction;
    private Config config;
    private int ind=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabBar=(TabBar)findViewById(R.id.tabBar);
        ScreenManager.getInstance().addActivity(this);
        config=Config.getInstance();
        tabBar.addTab(R.mipmap.qt1_12,R.mipmap.qt1_12, "商城");
        tabBar.addTab(R.mipmap.huodongimage,R.mipmap.huodongimage, "活动");
        tabBar.addTab(R.mipmap.shequimage,R.mipmap.shequimage, "社区");
        tabBar.addTab(R.mipmap.qt1_15,R.mipmap.qt1_15, "购物车");
        tabBar.addTab(R.mipmap.qt1_16,R.mipmap.qt1_16, "我的");
         fm=getFragmentManager();
         transaction=fm.beginTransaction();
        catFragment = new CatFragment();
        classificationFragment=new ClassificationFragment();
        myFragment=new MyFragment();
        shoppingFragment=new ShoppingFragment();
        informationFragment = new InformationFragment();
        transaction.add(R.id.id_content,shoppingFragment);
        transaction.add(R.id.id_content,classificationFragment);
        transaction.add(R.id.id_content,informationFragment);
        transaction.add(R.id.id_content,catFragment);

        transaction.add(R.id.id_content,myFragment).commit();



        tabBar.setTabShouldChange(new TabBar.TabShouldChange() {

            @Override
            public boolean shouldChange(int index) {
                // TODO Auto-generated method stub
                if (index == 0) {

                    getFragmentManager().beginTransaction().hide(catFragment).hide(myFragment).hide(classificationFragment).hide(informationFragment).show(shoppingFragment).commit();
                    ind=0;
                } else if (index == 1) {
                    getFragmentManager().beginTransaction().hide(catFragment).hide(myFragment).hide(shoppingFragment).hide(informationFragment).show(classificationFragment).commit();
                    ind=1;
                } else if (index == 2) {
                    getFragmentManager().beginTransaction().hide(catFragment).hide(myFragment).hide(shoppingFragment).hide(classificationFragment).show(informationFragment).commit();
                    ind=2;
                }else if (index == 3) {
//                    setCatFragment();
                    getFragmentManager().beginTransaction().hide(informationFragment).hide(myFragment).hide(shoppingFragment).hide(classificationFragment).show(catFragment).commit();
                    ind=3;
                }else if (index == 4) {
                    String land=config.getString("uid");
                    if(land.length()<1){
                        Intent intent=new Intent();
                        intent.putExtra("main","main");
                        intent.setClass(MainActivity.this, SignInActivity.class);
                        startActivity(intent);
                        tabBar.setSelectedndex(ind);
                        return false;

                    }else{
                        getFragmentManager().beginTransaction().hide(informationFragment).hide(catFragment).hide(shoppingFragment).hide(classificationFragment).show(myFragment).commit();
                   ind=4;
                    }

                }
                return true;
            }
        });
//        setShoppingFragment();
        Intent intent=this.getIntent();
        if(intent!=null){
            String mark=intent.getStringExtra("mark");
            if(mark!=null&&mark.equals("feilei")){

                getFragmentManager().beginTransaction().hide(catFragment).hide(myFragment).hide(shoppingFragment).hide(informationFragment).show(classificationFragment).commit();
           tabBar.setSelectedndex(1);
                ind=1;
            }else{
                getFragmentManager().beginTransaction().hide(catFragment).hide(myFragment).hide(classificationFragment).hide(informationFragment).show(shoppingFragment).commit();
            }


        }



    }



}
 
  
 
  
//Fragment显示隐藏的时候会调用
//Fragment中的onHiddenChanged方法在这里可以更新界面数据
 
  
@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);

 }
}

你可能感兴趣的:(Android Fragment隐藏显示调用的方法)