Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Fragment隱藏顯示調用的方法

Android Fragment隱藏顯示調用的方法

編輯:關於Android編程

//在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); } }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved