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. CSS选择器解析

    通配符选择器 Universal Selector * { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizin ...

  2. jquery keyup 在IOS设备上输入中文时不触发

    今天做一个异步查询功能的时候发现在IOS设备上查询中文时keyup没有触发,在其他设备上时可以的,后来在stackoverflow上找到下面这种解决方法,贴出来算是抛砖引玉了. $h_input.on ...

  3. Flume笔记--source端监听目录,sink端上传到HDFS

    官方文档参数解释:http://flume.apache.org/FlumeUserGuide.html#hdfs-sink 需要注意:文件格式,fileType=DataStream 默认为Sequ ...

  4. Unix和Linux下C语言学习指南

    转自:http://www.linuxdiyf.com/viewarticle.php?id=174074 Unix和Linux下C语言学习指南 引言 尽管 C 语言问世已近 30 年,但它的魅力仍未 ...

  5. Android实现三级联动下拉框 下拉列表spinner

    Android实现(省.市.县)三级联动下拉框 下拉列表spinner 转载请注明出处: http://www.goteny.com/articles/2013/11/46.html http://w ...

  6. sqoop组件运行出错问题解决--com.mysql.jdbc.Driver

    sqoop list-tables --connect jdbc:mysql://192.168.11.94:3306/huochetoudalian --username xxx -password ...

  7. Ruby on Rails Tutorial读书笔记-1

    只是怕忘了命令,全部撸一次,记个大概.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 安装Ruby之前,先要安装RVM: curl -L https://get.rvm.io | bas ...

  8. HDU_2033——时间加法

    Problem Description HDOJ上面已经有10来道A+B的题目了,相信这些题目曾经是大家的最爱,希望今天的这个A+B能给大家带来好运,也希望这个题目能唤起大家对ACM曾经的热爱.这个题 ...

  9. New Year Permutation(Floyd+并查集)

    Description User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to mak ...

  10. HDU-4857(拓扑排序)

    Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. ...