Android homepage fragment Switching
Method 1:
Layout file:
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 type = "codeph" text = "/codeph">
</Framelayout
Java file:
BottomUtil buttom = new BottomUtil (this, 0); // initialize the bottom_view.xml File
View mMainContainerView = findViewById (R. id. content_frame_main );
View mListContainerView = findViewById (R. id. content_frame_list );
View mPersonalContainerView = findViewById (R. id. content_frame_personal );
Call the setShowMode method when you click to switch:
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
. Getsuppfrfragmentmanager (). findFragmentById (
R. id. content_frame_personal );
}
MPersonalContainerView. setVisibility (View. VISIBLE );
Break;
}
}
BottomUtil. java file (loading initialization bottom switch button ):
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;
/**
* The current page focus, that is, the displayed page index
*/
Int mCurrentFocus =-1;
/**
* Initialize an instance of all control classes in the bottom menu bar.
*/
BottomViewItem item;
Public BottomUtil (MainActivity context, int index ){
MContext = context;
Item = BottomViewItem. getInstance ();
InitTab (index );
}
/**
* Control Initialization
*/
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
* Switch the background based on the index value
*/
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 (you have not logged on yet );
Return;
}*/
SetViewTab (I );
// MContext. startActivity (new Intent (mContext, item. intents [I]);
MContext. setShowMode (I + 1 );
}
}
}
}
Method 2:
Layout File
<framelayout android:id="@+id/fl_page" android:layout_height="0dp" android:layout_weight="1" android:layout_width="match_parent"> </framelayout>
Java file:
Public class MainActivity extends BaseActivity {private static final StringFRAGMENT_TAG_SERVICE= Service; private static final StringFRAGMENT_TAG_BBS= Bbs; private static final StringFRAGMENT_TAG_NEWS= News; private static final StringFRAGMENT_TAG_PROFILE= Profile; private ServiceFragment mServiceFragment; private BbsFragment ment region; private NewsFragment region; private ProfileFragment region; private FragmentManager mFragmentManager; private View mCurTabItemView; private Fragment mFragment; // The current Fragment @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout.Activity_main); MFragmentManager = getFragmentManager (); if (savedInstanceState! = Null) {// restore 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 has been recycled and is now resurrected);} 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 to be displayed; 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 (); // exchange Fragment if (toFragment. isAdded () {// if (mFragment! = Null) {fg. hide (mFragment);} fg. show (toFragment);} else {// not added if (mFragment! = Null) {fg. hide (mFragment);} fg. add (R. id.Fl_page, ToFragment, tag);} fg. commit (); mFragment = toFragment ;}}