Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 首頁fragment切換

android 首頁fragment切換

編輯:關於Android編程

方法一:

布局文件:

android:layout_width=match_parent
android:layout_height=match_parent
android:orientation=vertical >
<framelayout android:layout_width=match_parent
android:layout_weight=1
android:layout_height=0dp >

<framelayout android:layout_width=match_parent
android:layout_weight=1
android:layout_height=0dp >

<framelayout android:layout_width=match_parent
android:layout_weight=1
android:layout_height=0dp >


android:layout_width=match_parent
android:layout_height=@dimen/common_tabbar_height
android:background=@drawable/common_border_top_selector
android:layout_gravity=bottom
android:clickable=true
android:weightSum=3.0 >
</framelayout



 

java文件:

BottomUtil buttom = new BottomUtil(this, 0); //初始化bottom_view.xml文件

View mMainContainerView = findViewById(R.id.content_frame_main);
View mListContainerView = findViewById(R.id.content_frame_list);
View mPersonalContainerView = findViewById(R.id.content_frame_personal);

點擊切換時調用setShowMode方法:

public void setShowMode(int index) {
mListContainerView.setVisibility(View.GONE);
mMainContainerView.setVisibility(View.GONE);
mPersonalContainerView.setVisibility(View.GONE);
switch (index) {
case BottomUtil.MAIN_FRAGMENT:
if (mMainFragment == null) {
mMainFragment = new MainFragment();
//FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
t.replace(R.id.content_frame_main, mMainFragment);
t.commit();
} else {
mMainFragment = (MainFragment) this.getSupportFragmentManager()
.findFragmentById(R.id.content_frame_main);
}
mMainContainerView.setVisibility(View.VISIBLE);
getSupply();
break;
case BottomUtil.JOBLIST_FRAGMENT:
if (mListFragment == null) {
mListFragment = new ListFragment();


FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
t.replace(R.id.content_frame_list, mListFragment);
t.commit();
} else {
mListFragment = (ListFragment)this.getSupportFragmentManager()
.findFragmentById(R.id.content_frame_list);
}
mListContainerView.setVisibility(View.VISIBLE);
break;
case BottomUtil.PERSONAL_FRAGMENT:
if (mPersonalFragment == null) {
mPersonalFragment = new PersonalFragment();


FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
t.replace(R.id.content_frame_personal, mPersonalFragment);
t.commit();
} else {
mPersonalFragment = (PersonalFragment) this
.getSupportFragmentManager().findFragmentById(
R.id.content_frame_personal);
}
mPersonalContainerView.setVisibility(View.VISIBLE);
break;
}
}

 

 

BottomUtil .java文件(加載初始化底部切換按鈕):


public class BottomUtil implements OnClickListener {
public static final int MAIN_FRAGMENT = 1;
public static final int JOBLIST_FRAGMENT = 2;
public static final int PERSONAL_FRAGMENT = 3;

MainActivity mContext;
/**
* 當前頁面焦點,即顯示的頁面索引
*/
int mCurrentFocus = -1;
/**
* 底部菜單欄初始化所有控件類的一個實例
*/
BottomViewItem item;


public BottomUtil(MainActivity context, int index) {
mContext = context;
item = BottomViewItem.getInstance();
initTab(index);
}


/**
* 控件初始化
*/
private void initTab(int index) {
for (int i = 0; i < item.viewNum; i++) {
item.linears[i] = (LinearLayout) mContext
.findViewById(item.linears_id[i]);
item.linears[i].setOnClickListener(this);
item.images[i] = (ImageView) mContext
.findViewById(item.images_id[i]);
item.texts[i] = (TextView) mContext.findViewById(item.texts_id[i]);
}


setViewTab(index);
}


/**
* @param index
* 根據索引值切換背景
*/
private void setViewTab(int index) {
if (index == mCurrentFocus)
return;
mCurrentFocus = index;
for (int i = 0; i < item.viewNum; i++) {
item.images[i]
.setBackgroundResource(i == index ? item.images_selected[i]
: item.images_unselected[i]);
item.texts[i]
.setTextColor(i == index ? mContext.getResources().getColor(R.color.common_btn_main_pressed_color)
: mContext.getResources().getColor(R.color.common_textcolor_third));
}
}


@Override
public void onClick(View v) {
for (int i = 0; i < item.viewNum; i++) {
if (v.getId() == item.linears_id[i] && (i != mCurrentFocus)) {
/*if(i==2&&(!Preferences.isLogin())){
ToastUtil.make(mContext).show(您還沒有登錄);
return;
}*/
setViewTab(i);
// mContext.startActivity(new Intent(mContext,item.intents[i]));
mContext.setShowMode(i + 1);
}
}
}


}

方法二:

布局文件

 



    <framelayout android:id="@+id/fl_page" android:layout_height="0dp" android:layout_weight="1" android:layout_width="match_parent">

    </framelayout>

    


        

            

            
        

        

            

            
        

        

            

            
        


        

            

            
        


    



 

java文件:

 

public class MainActivity extends BaseActivity {
    private static final String FRAGMENT_TAG_SERVICE = service;
    private static final String FRAGMENT_TAG_BBS = bbs;
    private static final String FRAGMENT_TAG_NEWS = news;
    private static final String FRAGMENT_TAG_PROFILE = profile;

    private ServiceFragment mServiceFragment;
    private BbsFragment mBbsFragment;
    private NewsFragment mNewsFragment;
    private ProfileFragment mProfileFragment;
    private FragmentManager mFragmentManager;
    private View mCurTabItemView;
    private Fragment mFragment;//當前的Fragment


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentManager = getFragmentManager();
        if (savedInstanceState != null) {
            //復活Fragment
            mServiceFragment = (ServiceFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG_SERVICE);
            mBbsFragment = (BbsFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG_BBS);
            mNewsFragment = (NewsFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG_NEWS);
            mProfileFragment = (ProfileFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG_PROFILE);
            FragmentTransaction fg = mFragmentManager.beginTransaction();
            if (mProfileFragment != null) {
                if (mProfileFragment.isAdded()) {
                    fg.hide(mProfileFragment);
                }
            }
            if (mNewsFragment != null) {
                if (mNewsFragment.isAdded()) {
                    fg.hide(mNewsFragment);
                }
            }

            if (mBbsFragment != null) {
                if (mBbsFragment.isAdded()) {
                    fg.hide(mBbsFragment);
                }
            }

            if (mServiceFragment != null) {
                if (mServiceFragment.isAdded()) {
                    fg.hide(mServiceFragment);
                }
            }
            fg.commit();
            Log.d(main, fragment 被回收了,現在又復活了);
        }
        onTabItemClick(findViewById(R.id.ll_main_tabitem_service));
    }

    public void onTabItemClick(View v) {
        //TODO tab change
        if (mCurTabItemView == v) {
            return;
        }
        if (mCurTabItemView != null) {
            mCurTabItemView.setSelected(false);
        }
        mCurTabItemView = v;
        mCurTabItemView.setSelected(true);
        Fragment toFragment;//即將被顯示的Fragment
        String tag;
        switch (v.getId()) {
            case R.id.ll_main_tabitem_service:
                if (mServiceFragment == null) {
                    mServiceFragment = new ServiceFragment();
                }
                tag = FRAGMENT_TAG_SERVICE;
                toFragment = mServiceFragment;
                break;
            case R.id.ll_main_tabitem_bbs:
                if (mBbsFragment == null) {
                    mBbsFragment = new BbsFragment();
                }
                tag = FRAGMENT_TAG_BBS;
                toFragment = mBbsFragment;
                break;
            case R.id.ll_main_tabitem_news:
                if (mNewsFragment == null) {
                    mNewsFragment = new NewsFragment();
                }
                tag = FRAGMENT_TAG_NEWS;
                toFragment = mNewsFragment;
                break;
            case R.id.ll_main_tabitem_profile:
                if (mProfileFragment == null) {
                    mProfileFragment = new ProfileFragment();
                }
                tag = FRAGMENT_TAG_PROFILE;
                toFragment = mProfileFragment;
                break;
            default:
                return;
        }
        if (mFragment == toFragment) {
            return;
        }

        FragmentTransaction fg = mFragmentManager.beginTransaction();
        //交換Fragment
        if (toFragment.isAdded()) {
            //已經被添加了
            if (mFragment != null) {
                fg.hide(mFragment);
            }
            fg.show(toFragment);
        } else {
            //沒有被添加
            if (mFragment != null) {
                fg.hide(mFragment);
            }
            fg.add(R.id.fl_page, toFragment, tag);
        }
        fg.commit();
        mFragment = toFragment;
    }

}

 

 

 

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