方法一:

布局文件:

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

更多相关文章

  1. android 文件搜索
  2. Android方法的传递值及其改变
  3. 【Android】打开/读取文件的方法
  4. android 利用startActivityForResult()方法得到另一个Activity的
  5. android studio中rendering problems解决方法
  6. Android往SD卡上存储文件
  7. TextView 在xml文件中的解释
  8. android java 文件的读写

随机推荐

  1. 各种类型Android(安卓)Market了解
  2. Android(安卓)Button应用法则
  3. Android(安卓)AIDE入门——Hello,world,
  4. Android(安卓)for opencv(1)android使用ope
  5. Android命令Monkey压力测试,详解
  6. Android(安卓)面试题总结之Android(安卓)
  7. 让Python程序在Android手机上跑起来
  8. 《Android深入透析》之Android事件分发机
  9. Android(安卓)应用程序之道
  10. 【Android(安卓)开发】: Android(安卓)消