android 首页fragment切换

方法一:

布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:id="@+id/content_frame_main"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" >
    </FrameLayout>
    <FrameLayout
        android:id="@+id/content_frame_list"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"  >
    </FrameLayout>
    <FrameLayout
        android:id="@+id/content_frame_personal"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"  >
    </FrameLayout>
    
    <LinearLayout
        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" >
        <include layout="@layout/bottom_view" />
     </LinearLayout>


</LinearLayout>

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);
}
}
}


}

方法二:

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    </FrameLayout>

    <LinearLayout
        android:id="@+id/ll_main_tabbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/common_tabbar_height"
        android:background="@drawable/common_tabbar_bg"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="3dp"
        android:paddingTop="5dp">


        <RelativeLayout
            android:id="@+id/ll_main_tabitem_service"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:onClick="onTabItemClick"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/iv_main_tabitem_service"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter"
                android:src="@drawable/main_tabbar_service_selector" />

            <TextView
                android:id="@+id/iv_main_tabitem_service"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="@string/common_tabbar_label_service"
                android:textColor="@drawable/common_tabbar_text_selector"
                android:textSize="@dimen/common_tabbar_textsize" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ll_main_tabitem_bbs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:onClick="onTabItemClick"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/iv_main_tabitem_bbs"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter"
                android:src="@drawable/main_tabbar_bbs_selector" />

            <TextView
                android:id="@+id/iv_main_tabitem_bbs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="@string/common_tabbar_label_bbs"
                android:textColor="@drawable/common_tabbar_text_selector"
                android:textSize="@dimen/common_tabbar_textsize" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ll_main_tabitem_news"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:onClick="onTabItemClick"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/iv_main_tabitem_news"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter"
                android:src="@drawable/main_tabbar_news_selector" />

            <TextView
                android:id="@+id/iv_main_tabitem_news"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="@string/common_tabbar_label_news"
                android:textColor="@drawable/common_tabbar_text_selector"
                android:textSize="@dimen/common_tabbar_textsize" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/ll_main_tabitem_profile"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:gravity="center"
            android:onClick="onTabItemClick"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/iv_main_tabitem_profile"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter"
                android:src="@drawable/main_tabbar_profile_selector" />

            <TextView
                android:id="@+id/iv_main_tabitem_profile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="@string/common_tabbar_label_profile"
                android:textColor="@drawable/common_tabbar_text_selector"
                android:textSize="@dimen/common_tabbar_textsize" />
        </RelativeLayout>


    </LinearLayout>

</LinearLayout>

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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值