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. Java语言实现简单FTP软件------>FTP软件本地窗口的实现(五)

    1.首先看一下本地窗口的布局效果 2.看一下本地窗口实现的代码框架 2.本地窗口的具体实现代码LocalPanel.java package com.oyp.ftp.panel.local; impo ...

  2. DSP TMS320C6000基础学习(6)—— gel文件

    什么是gel文件?gel文件能干什么? gel全称General Extended Language,即通用扩展语言文件,gel文件中由类似C语言的代码构成,gel语言是一种解释性语言,gel文件扩展 ...

  3. Css溢出隐藏

    display: -webkit-box;-webkit-line-clamp: 2;     /*多少行数之后显示为省略...*/word-wrap: break-word;word-break: ...

  4. 【结构型】Flyweight模式

    享元模式的主要目的.意图是为对象的大量使用提供一种共享机制.该模式的思想重在复用.共享复用.像文字.列表中的格子等这类多数都是需要考虑复用技术,否则必将大量耗费内存空间而使资源以及性能等大量耗费.该模 ...

  5. SSD、高级格式化硬盘,4K,分区,对齐,Ghost能不能用的解释用SSD的都可以看看

    或者以上,建议选择2048,这是Win7分区使用的值,实际对齐大小就为1024K.  4.如何知道是否对齐?  使用工具AS SSD Benchmark(HDTune Pro 4.6目前测的禁绝)   ...

  6. Sicily 1133. SPAM

    题目地址:1133. SPAM 思路: 题目意思是说在‘@’的前后出现题目给定的合法字符或者不连续出现‘.’字符的话,这个就是合理的输出. 那么以@为中心,向前,向后扫描,当扫描到不符合字符时,记录此 ...

  7. 转:SRIO错误侦测和管理机制

    http://www.deyisupport.com/question_answer/dsp_arm/c6000_multicore/f/53/t/22879.aspx http://www.deyi ...

  8. NGUI类之间的关系和架构

    NGUI Drawcall 1.使用同一个altals的元素尽量放在同一个UIPanel下面,在NGUI中,它消耗的drawcall是以每个Panel为独立计算单位进行计算的. 2.如果一个UIPan ...

  9. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  10. word 生成目录

    生成目录: (1)Ctrl+End,到达文档的最后一页: (2)"插入"菜单--引用--索引和目录(此时出现索引和目录对话框): (3)单击"目录"选项卡 a. ...