方法一:

布局文件:

<?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. AndroidManifest.xml文件详解(uses-configuration)
  2. Android4.0中修改挂断键(ENDCALL)的默认行为
  3. 禁止状态栏下拉
  4. Android(安卓)intent 传递数组对象序列化
  5. Android(安卓)文件打开方式
  6. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
  7. NPM 和webpack 的基础使用
  8. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  9. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像

随机推荐

  1. 苹果电脑如何开启显示器减弱动态效果设置
  2. Linux修改系统时间为东八区北京时间(上海
  3. 苹果Mac如何关闭右上角的Siri显示?
  4. Cisco ISE如何关机-----ISE的关机方法和
  5. 从事运维工作是一种怎样的体检?
  6. 苏宁大数据离线任务开发调度平台实践:任务
  7. 逻辑复制-更改复制标识(REPLICA IDENTITY)
  8. Prometheus 之 Kubernetes节点的监控
  9. 帆软和思迈特软件Smartbi产品的详细对比
  10. Linux运维入门教程06-04 (硬盘分区、格式