Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Tab -- 使用Fragment、FragmentManager來實現

Android Tab -- 使用Fragment、FragmentManager來實現

編輯:關於Android編程

效果:

\

 

代碼:https://github.com/ldb-github/Layout_Tab

1、布局:使用LinearLayout布置標簽;再使用FrameLayout來布置Fragment。

\\



    

        

            

        

        

        

            

        

        

        

            

        

    

    <framelayout android:id="@+id/content_layout" android:layout_height="match_parent" android:layout_width="match_parent">

    </framelayout>

fragment_fragmentmanager.xml

 

1、方法replaceFragment()實現切換Fragment的功能:通過FragmentTransaction的replace方法來替換FrameLayout中的Fragment。

2、改變標簽的背景色,來區分選中的Tab與其他Tab。

\\
public class FragmentAndFManagerActivity extends FragmentActivity
        implements View.OnClickListener {
    private static final String LOG_TAG = FragmentAndFManagerActivity.class.getSimpleName();

    // 三個選項卡
    private LinearLayout tab1Layout, tab2Layout, tab3Layout;
    // 默認選中第一個
    private int index = 1;
    // fragment管理類
    private FragmentManager fragmentManager;
    // 三個fragment
    private Fragment tab1Fragment, tab2Fragment, tab3Fragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_fragmentmanager);
        fragmentManager = getSupportFragmentManager();
        init();
    }

    /**
     * 初始化控件
     */
    private void init(){
        tab1Layout = (LinearLayout) findViewById(R.id.tab1_layout);
        tab2Layout = (LinearLayout) findViewById(R.id.tab2_layout);
        tab3Layout = (LinearLayout) findViewById(R.id.tab3_layout);

        tab1Layout.setOnClickListener(this);
        tab2Layout.setOnClickListener(this);
        tab3Layout.setOnClickListener(this);

        setDefaultFragment();

    }

    /**
     * 設置默認顯示的fragment
     */
    private void setDefaultFragment(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        tab1Fragment = new FragmentAndFManager_Fragment1();
        transaction.replace(R.id.content_layout, tab1Fragment);
        transaction.commit();
    }

    /**
     * 切換fragment
     * @param newFragment
     */
    private void replaceFragment(Fragment newFragment){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if(!newFragment.isAdded()) {
            Log.d(LOG_TAG, "In replaceFragment -- replace");
            transaction.replace(R.id.content_layout, newFragment);
            transaction.commit();
        }else{
            Log.d(LOG_TAG, "In replaceFragment -- show");
            transaction.show(newFragment);
        }
    }

    /**
     * 改變選項卡的選中狀態
     */
    private void clearStatus(){
        if(index == 1){
            tab1Layout.setBackgroundColor(getResources().getColor(R.color.tab));
        }else if(index == 2){
            tab2Layout.setBackgroundColor(getResources().getColor(R.color.tab));
        }else if(index == 3){
            tab3Layout.setBackgroundColor(getResources().getColor(R.color.tab));
        }
    }

    @Override
    public void onClick(View v) {
        clearStatus();
        switch(v.getId()){
            case R.id.tab1_layout:
                if(tab1Fragment == null){
                    tab1Fragment = new FragmentAndFManager_Fragment1();
                }
                replaceFragment(tab1Fragment);
                tab1Layout.setBackgroundColor(getResources().getColor(R.color.tab_down));
                index = 1;
                break;
            case R.id.tab2_layout:
                if(tab2Fragment == null){
                    tab2Fragment = new FragmentAndFManager_Fragment2();
                }
                replaceFragment(tab2Fragment);
                tab2Layout.setBackgroundColor(getResources().getColor(R.color.tab_down));
                index = 2;
                break;
            case R.id.tab3_layout:
                if(tab3Fragment == null){
                    tab3Fragment = new FragmentAndFManager_Fragment3();
                }
                replaceFragment(tab3Fragment);
                tab3Layout.setBackgroundColor(getResources().getColor(R.color.tab_down));
                index = 3;
                break;
        }
    }
}
FragmentAndFManagerActivity.java

 

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