fragment实现Tab效果

使用下面两句代码获取对fragment的事务处理:

	FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
使用FrameLayout来显示fragment, 通过transaction.add(int framelayout_Id,Fragment fragment)向FrameLayout添加fragment

每次变化fragment时,先用transaction.hide(fragment)方法来隐藏所有的fragment,再使用transaction.show(fragment)来显示当前的fragment,最后调用transaction.commit()提交事务即可使当前页变化

完整的示例如下:

public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private ImageButton news,friends,address,setting;
    private NewsFragment newsFragment;
    private FriendsFragment friendsFragment;
    private AddressFragment addressFragment;
    private SettingFragment settingFragment;
    private LinearLayout layout_news;
    private LinearLayout layout_friends;
    private LinearLayout layout_address;
    private LinearLayout layout_setting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tab_layout_one);
        initViews();
        initEvents();
        setSelected(R.id.layout_news);
    }

    private void initViews()
    {
        news = (ImageButton) findViewById(R.id.imgBtn_news);
        friends = (ImageButton) findViewById(R.id.imgBtn_friends);
        address = (ImageButton) findViewById(R.id.imgBtn_address);
        setting = (ImageButton) findViewById(R.id.imgBtn_setting);
        layout_news = (LinearLayout) findViewById(R.id.layout_news);
        layout_friends = (LinearLayout) findViewById(R.id.layout_friends);
        layout_address = (LinearLayout) findViewById(R.id.layout_address);
        layout_setting = (LinearLayout) findViewById(R.id.layout_setting);
    }

    private void initEvents()
    {
        layout_news.setOnClickListener(this);
        layout_friends.setOnClickListener(this);
        layout_address.setOnClickListener(this);
        layout_setting.setOnClickListener(this);
    }

    private void setSelected(int viewId)
    {
        setImg();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragments(transaction);
        switch (viewId){
            case R.id.layout_news:
                if(newsFragment == null){
                    newsFragment = new NewsFragment();
                    transaction.add(R.id.fragment_containner,newsFragment);
                }else{
                    transaction.show(newsFragment);
                }
                news.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case R.id.layout_friends:
                if(friendsFragment == null){
                    friendsFragment = new FriendsFragment();
                    transaction.add(R.id.fragment_containner,friendsFragment);
                }else{
                    transaction.show(friendsFragment);
                }
                friends.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case R.id.layout_address:
                if(addressFragment == null){
                    addressFragment = new AddressFragment();
                    transaction.add(R.id.fragment_containner,addressFragment);
                }else{
                    transaction.show(addressFragment);
                }
                address.setImageResource(R.drawable.tab_address_pressed);
                break;
            case R.id.layout_setting:
                if(settingFragment == null){
                    settingFragment = new SettingFragment();
                    transaction.add(R.id.fragment_containner,settingFragment);
                }else{
                    transaction.show(settingFragment);
                }
                setting.setImageResource(R.drawable.tab_settings_pressed);
                break;
        }
        transaction.commit();
    }

    private void setImg() {
        news.setImageResource(R.drawable.tab_weixin_normal);
        friends.setImageResource(R.drawable.tab_find_frd_normal);
        address.setImageResource(R.drawable.tab_address_normal);
        setting.setImageResource(R.drawable.tab_settings_normal);
    }
    private void hideFragments(FragmentTransaction transaction){
        if(newsFragment != null)
            transaction.hide(newsFragment);
        if(friendsFragment != null)
            transaction.hide(friendsFragment);
        if(addressFragment != null)
            transaction.hide(addressFragment);
        if(settingFragment != null)
            transaction.hide(settingFragment);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.layout_news:
                setSelected(R.id.layout_news);
                break;
            case R.id.layout_friends:
                setSelected(R.id.layout_friends);
                break;
            case R.id.layout_address:
                setSelected(R.id.layout_address);
                break;
            case R.id.layout_setting:
                setSelected(R.id.layout_setting);
                break;
            default:
                break;
        }
    }
}

这个Activity需要继承FragmentActivity ,所有的Fragment相关的包要导入v4包,如:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
 
  
注:其实对fragment的变化可以使用如下语句实现:
 
  
  getSupportFragmentManager().beginTransaction().replace(R.id.fragment_containner,fragment).commit();



你可能感兴趣的:(android案例)