事务管理

private Button home_but_home;
    private Button home_but_sort;
    private Button home_but_cart;
    private Button home_but_user;
    private Fragment[] allfrag;
    private FragmentManager manger;
    private FragmentTransaction trans;

private void Listener() {
        // TODO Auto-generated method stub
        home_but_home.setOnClickListener(this);
        home_but_sort.setOnClickListener(this);
        home_but_cart.setOnClickListener(this);
        home_but_user.setOnClickListener(this);
    }

private void fragmanager() {
        // TODO Auto-generated method stub
        //创建fragment
        allfrag = new Fragment[4];
        //获取事务管理
        manger = getSupportFragmentManager();
        allfrag[0] = manger.findFragmentById(R.id.home_fragment_home);
        allfrag[1] = manger.findFragmentById(R.id.home_fragment_sort);
        allfrag[2] = manger.findFragmentById(R.id.home_fragment_cart);
        allfrag[3] = manger.findFragmentById(R.id.home_fragment_user);
        //启动事物
        trans = manger.beginTransaction()
        .hide(allfrag[0])
        .hide(allfrag[1])
        .hide(allfrag[2])
        .hide(allfrag[3]);
        
        trans.show(allfrag[0]).commit();
        
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        trans = manger.beginTransaction()
        .hide(allfrag[0])
        .hide(allfrag[1])
        .hide(allfrag[2])
        .hide(allfrag[3]);
    
        
        //获取点击按钮的ID
        switch (v.getId()) {
        case R.id.home_but_home:
            
            trans.show(allfrag[0]).commit();
            home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_selected);
            home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_normal);
            home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_normal);
            home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_normal);
            home_but_home_tv.setTextColor(Color.RED);
            home_but_sort_tv.setTextColor(Color.BLACK);
            home_but_cart_tv.setTextColor(Color.BLACK);
            home_but_user_tv.setTextColor(Color.BLACK);
            break;
        case R.id.home_but_sort:
            
            trans.show(allfrag[1]).commit();
            home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_normal);
            home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_selected);
            home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_normal);
            home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_normal);
            home_but_home_tv.setTextColor(Color.BLACK);
            home_but_sort_tv.setTextColor(Color.RED);
            home_but_cart_tv.setTextColor(Color.BLACK);
            home_but_user_tv.setTextColor(Color.BLACK);
            break;

        case R.id.home_but_cart:
    
            trans.show(allfrag[2]).commit();
            home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_normal);
            home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_normal);
            home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_selected);
            home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_normal);
            home_but_home_tv.setTextColor(Color.BLACK);
            home_but_sort_tv.setTextColor(Color.BLACK);
            home_but_cart_tv.setTextColor(Color.RED);
            home_but_user_tv.setTextColor(Color.BLACK);
            break;

        case R.id.home_but_user:
    
            trans.show(allfrag[3]).commit();
            home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_normal);
            home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_normal);
            home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_normal);
            home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_selected);
            home_but_home_tv.setTextColor(Color.BLACK);
            home_but_sort_tv.setTextColor(Color.BLACK);
            home_but_cart_tv.setTextColor(Color.BLACK);
            home_but_user_tv.setTextColor(Color.RED);
            break;


        default:
            break;
        }
        
    }

原文地址:https://www.cnblogs.com/taogev5/p/5659130.html