Fragment实现底部Tab,切换可保存状态

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,切换可保存状态的更多相关文章
- Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换
一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...
- 解决Fragment在Viepager中切换不保存状态的问题
在FragmentPagerAdapter中重写以下方法: @Override public Object instantiateItem(ViewGroup container, int posit ...
- 09 Flutter底部Tab切换保持页面状态的几种方法
IndexedStack:保此所有页面的状态: AutomaticKeepAliveClientMixin:保此部分页面的状态: 修改的页面代码: 页面效果: Tabs.dart import 'pa ...
- Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...
- Android组件:Fragment切换后保存状态
之前写的第一篇Fragment实例,和大多数人一开始学的一样,都是通过FragmentTransaction的replace方法来实现,replace方法相当于先移除remove()原来所有已存在的f ...
- Fragment实现底部选项卡切换效果
现在很多APP的样式都是底部选项卡做为首页的,实现这样的效果,我们一般有这样几种方式,第一,最屌丝的做法,我直接自定义选项卡视图,通过监听选项卡视图,逻辑控制内容页的切换,这样做的想法一般是反正这几个 ...
- mui 配置底部tab切换方式以模板的方式访问
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Activity内切换fragment实现底部菜单切换遇到的坑
1.一般说来,app底部导航都会设计为5个菜单,可以使用textView,也可使用radioButton,这里我选择用radioButton,给radioButton直接设置selector就可以实现 ...
- Android典型界面设计(3)——访网易新闻实现双导航tab切换
一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...
随机推荐
- 【原创】用shell+ant构建apk
#!/bin/bash ######################################################################### ## $PRODVERSIO ...
- HTML5 Canvas 中的颜色、样式和阴影的属性和方法
颜色.样式和阴影的属性与方法 fillStyle 设置或返回用于填充绘画的颜色.渐变或模式 strokeStyle 设置或返回用于笔触的颜色.渐变或模式 ...
- 《Javascript模式》之对象创建模式读书笔记
引言: 在javascript中创建对象是很容易的,可以使用对象字面量或者构造函数或者object.creat.在接下来的介绍中,我们将越过这些方法去寻求一些其他的对象创建模式. 我们知道js是一种简 ...
- Swift—Cocoa Touch设计模式-备
目标(Target)与动作(Action)是iOS和OS X应用开发的中事件处理机制. 问题提出 如图所示是一个ButtonLabelSample案例设计原型图,其中包含一个标签和一个按钮,当点击 ...
- win7禁止自动使用浏览器打开FTP而是用资源管理器
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\ftp] @="URL:File Transfer Protocol" ...
- 创建.NET应用程序所经历的步骤
1.使用某种.NET兼容语言(如C#)编写应用程序.2.把代码编译为(CIL),存储在程序集中.3.在执行代码时(如果这是一个可执行文件,就自动运行,或者在其他代码使用它时运行),首先必须使用JIT( ...
- 51单片机C语言学习笔记3: 存储器结构
MCS-51单片机在物理结构上有四个存储空间: 1.片内程序存储器2.片外程序存储器3.片内数据存储器4.片外数据存储器 但在逻辑上,即从用户的角度上,8051单片机有三个存储空间: 1.片内外统一编 ...
- 我为什么放弃了win7系统
作为一个软件开发者,由于要常年维护一些老系统,所以非常看中win7对一些稍老一点软件的兼容性,可它还是让我失望了: 如果没有特殊手段,sqlserver2000是不能向导式安装成功的: 之前公司的主要 ...
- HDU_2049——部分错位排列,概率论
Problem Description 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样 ...
- HDU_2017——统计数字字符出现的个数
Problem Description 对于给定的一个字符串,统计其中数字字符出现的次数. Input 输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数 ...