activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <FrameLayout
android:id="@+id/mHomeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/mRadioGroup" >
</FrameLayout> <RadioGroup
android:id="@+id/mRadioGroup"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@drawable/main_bottom_bg"
android:orientation="horizontal" > <ImageView
android:id="@+id/Tab_Conversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/skin_tab_icon_conversation_selected" /> <ImageView
android:id="@+id/Tab_Contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/skin_tab_icon_contact_normal" /> <ImageView
android:id="@+id/Tab_Plugin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@null"
android:src="@drawable/skin_tab_icon_plugin_normal" />
</RadioGroup> </RelativeLayout>

关键代码:

 import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.content.Context;
import android.os.Bundle; @ContentView(R.layout.activity_main)
public class MainActivity extends ActionBarActivity { @ViewInject(R.id.Tab_Conversation)
private ImageView Tab_Conversation;
@ViewInject(R.id.Tab_Contact)
private ImageView Tab_Contact;
@ViewInject(R.id.Tab_Plugin)
private ImageView Tab_Plugin; private FragmentConversation mFragmentConversation;
private FragmentContact mFragmentContact;
private FragmentPlugin mFragmentPlugin;
private Context mContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ViewUtils.inject(this);
mContext = this;
InitFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.mHomeContainer, mFragmentConversation).commit();
} private void InitFragment() {
mFragmentConversation = new FragmentConversation();
mFragmentContact = new FragmentContact();
mFragmentPlugin = new FragmentPlugin();
} /**
* 隐藏其他所有Fragment
*/
private void HideOtherFragments(Fragment fragment) {
if (getSupportFragmentManager().getFragments() == null) {
return;
}
for (Fragment fm : getSupportFragmentManager().getFragments()) {
if (fm != fragment) {
getSupportFragmentManager().beginTransaction().hide(fm)
.commit();
}
}
} /**
* Tab切换
*
* @param view
*/
@OnClick({ R.id.Tab_Conversation, R.id.Tab_Contact, R.id.Tab_Plugin })
private void OnTabSelected(View view) {
ClearSelection();
FragmentTransaction mTransaction = getSupportFragmentManager()
.beginTransaction();
switch (view.getId()) {
case R.id.Tab_Conversation:
Tab_Conversation.setImageDrawable(getResources().getDrawable(
R.drawable.skin_tab_icon_conversation_selected));
if (!getSupportFragmentManager().getFragments().contains(
mFragmentConversation)) {
mTransaction.add(R.id.mHomeContainer, mFragmentConversation);
}
mTransaction.show(mFragmentConversation);
HideOtherFragments(mFragmentConversation);
break;
case R.id.Tab_Contact:
Tab_Contact.setImageDrawable(getResources().getDrawable(
R.drawable.skin_tab_icon_contact_selected));
if (!getSupportFragmentManager().getFragments().contains(
mFragmentContact)) {
mTransaction.add(R.id.mHomeContainer, mFragmentContact);
}
mTransaction.show(mFragmentContact);
HideOtherFragments(mFragmentContact);
break;
case R.id.Tab_Plugin:
Tab_Plugin.setImageDrawable(getResources().getDrawable(
R.drawable.skin_tab_icon_plugin_selected));
if (!getSupportFragmentManager().getFragments().contains(
mFragmentPlugin)) {
mTransaction.add(R.id.mHomeContainer, mFragmentPlugin);
}
mTransaction.show(mFragmentPlugin);
HideOtherFragments(mFragmentPlugin);
break;
default:
break;
}
mTransaction.commit();
} /**
* 清除所有选中状态
*/
private void ClearSelection() {
Tab_Conversation.setImageDrawable(getResources().getDrawable(
R.drawable.skin_tab_icon_conversation_normal));
Tab_Contact.setImageDrawable(getResources().getDrawable(
R.drawable.skin_tab_icon_contact_normal));
Tab_Plugin.setImageDrawable(getResources().getDrawable(
R.drawable.skin_tab_icon_plugin_normal));
} }

Fragment实现底部Tab,切换可保存状态的更多相关文章

  1. Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换

    一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...

  2. 解决Fragment在Viepager中切换不保存状态的问题

    在FragmentPagerAdapter中重写以下方法: @Override public Object instantiateItem(ViewGroup container, int posit ...

  3. 09 Flutter底部Tab切换保持页面状态的几种方法

    IndexedStack:保此所有页面的状态: AutomaticKeepAliveClientMixin:保此部分页面的状态: 修改的页面代码: 页面效果: Tabs.dart import 'pa ...

  4. Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...

  5. Android组件:Fragment切换后保存状态

    之前写的第一篇Fragment实例,和大多数人一开始学的一样,都是通过FragmentTransaction的replace方法来实现,replace方法相当于先移除remove()原来所有已存在的f ...

  6. Fragment实现底部选项卡切换效果

    现在很多APP的样式都是底部选项卡做为首页的,实现这样的效果,我们一般有这样几种方式,第一,最屌丝的做法,我直接自定义选项卡视图,通过监听选项卡视图,逻辑控制内容页的切换,这样做的想法一般是反正这几个 ...

  7. mui 配置底部tab切换方式以模板的方式访问

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Activity内切换fragment实现底部菜单切换遇到的坑

    1.一般说来,app底部导航都会设计为5个菜单,可以使用textView,也可使用radioButton,这里我选择用radioButton,给radioButton直接设置selector就可以实现 ...

  9. Android典型界面设计(3)——访网易新闻实现双导航tab切换

    一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...

随机推荐

  1. uva10020 贪心

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. Oracle数据库之数据类型

    Oracle数据库之数据类型 Oracle基本数据类型(亦叫内置数据类型,internal datatypes或built-in datatypes)可以按类型分为:字符串类型.数字类型.日期类型.L ...

  3. How To Learn English Very Fast

    How do you learn English very fast? Every week, I get emails about this topic.   Typically, someone ...

  4. 关于Cococs中的CCActionEase(中)

    相比之前的速度正弦变化动作(这个东西叫什么更好一些?渐变动画?)与速度指数级变化动作,CCEaseIn/CCEaseOut/CCEaseInOut更具灵活性.你可以设置运动的速率,甚至是在运动的过程中 ...

  5. Java基础学习第二天

    ================每日必读==================== 写代码: 1.明确需求.我需要实现什么需求? 2.分析思路.我需要怎么实现需求? 3.确定步骤.我的每一部分思路需要使 ...

  6. SQL basic

    Function 从例子学习 drop function dbo.getWorkingDateCount; --getWorkingDateCount = functionName CREATE FU ...

  7. uva10003 Cutting Sticks

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. 使用SQLiteDatabase进行数据库操作的步骤

    1.获取SQLiteDatabase对象,它代表了与数据库的连接.2.调用SQLiteDatabase的方法来执行SQL语句.3.操作SQL语句的执行结果,比如用SimpleCursorAdapter ...

  9. Xamarin devexpress datagrid 样式

    DevExpress的提供光与暗的内置,可以应用到主题GridControl改变其外观.   主题 iOS版 Android版 光(默认适用于iOS) 黑暗(默认为Android) 应用预定义的主题 ...

  10. Skipped Rebuild All: Project with VS2008

    This Problem is getting me head around it in these days. BUt , i have solve it: Description of Probl ...