21 RadioGroup ListFragment
- 结构
MainActivity.java
package com.qf.day21_radiogroupfragment_demo3;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private RadioGroup rgMain;
//Fragment数据源
private List<Fragment> list = new ArrayList<Fragment>();
private RadioButton[] rbs;
private String[] titles={"news","happy","dz","cj"};
private int currentIndex =0;//当前展示的Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgMain = (RadioGroup) findViewById(R.id.rg_main);
initData();
initTab();
}
//初始化标签
private void initTab(){
//改变标签内容
rbs = new RadioButton[rgMain.getChildCount()];
for(int i=0;i<rgMain.getChildCount();i++){
rbs[i] = (RadioButton) rgMain.getChildAt(i);
rbs[i].setText(titles[i]);
}
//点击按钮进行替换
rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0;i<rgMain.getChildCount();i++){
if(rbs[i].getId() == checkedId){//当前按钮被点击
//开始替换
//replaceFragment(i);
switchFragment(i);
}
}
}
});
}
//replace 缺点 影响性能
public void replaceFragment(int index){
MyFragment myFragment = MyFragment.getInstance(index+1);
getSupportFragmentManager().
beginTransaction().
replace(R.id.layout_content_id, list.get(index)).commit();
}
//替换 使用 show() 和hide() 方法 减少性能开销
public void switchFragment(int targetIndex){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//点击的Fragment(目标)
Fragment targetFragment = list.get(targetIndex);
//当前的Fragment
Fragment currentFragment = list.get(currentIndex);
//点击的 按钮对象的Fragment 存在 show()展示出来 隐藏当前的Fragment
if(!targetFragment.isAdded()){
transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();
}else{
transaction.show(targetFragment).hide(currentFragment).commit();
}
//当前展示的Fragment就是点击替换的Fragment
currentIndex = targetIndex;
}
//初始化数据
private void initData(){
for(int i=0;i<rgMain.getChildCount();i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
//程序运行 默认展示第一个Fragment
getSupportFragmentManager().
beginTransaction().
add(R.id.layout_content_id, list.get(0)).commit();
}
}
MyFragment.java
package com.qf.day21_radiogroupfragment_demo3;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private RadioGroup rgMain;
//Fragment数据源
private List<Fragment> list = new ArrayList<Fragment>();
private RadioButton[] rbs;
private String[] titles={"news","happy","dz","cj"};
private int currentIndex =0;//当前展示的Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rgMain = (RadioGroup) findViewById(R.id.rg_main);
initData();
initTab();
}
//初始化标签
private void initTab(){
//改变标签内容
rbs = new RadioButton[rgMain.getChildCount()];
for(int i=0;i<rgMain.getChildCount();i++){
rbs[i] = (RadioButton) rgMain.getChildAt(i);
rbs[i].setText(titles[i]);
}
//点击按钮进行替换
rgMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0;i<rgMain.getChildCount();i++){
if(rbs[i].getId() == checkedId){//当前按钮被点击
//开始替换
//replaceFragment(i);
switchFragment(i);
}
}
}
});
}
//replace 缺点 影响性能
public void replaceFragment(int index){
MyFragment myFragment = MyFragment.getInstance(index+1);
getSupportFragmentManager().
beginTransaction().
replace(R.id.layout_content_id, list.get(index)).commit();
}
//替换 使用 show() 和hide() 方法 减少性能开销
public void switchFragment(int targetIndex){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//点击的Fragment(目标)
Fragment targetFragment = list.get(targetIndex);
//当前的Fragment
Fragment currentFragment = list.get(currentIndex);
//点击的 按钮对象的Fragment 存在 show()展示出来 隐藏当前的Fragment
if(!targetFragment.isAdded()){
transaction.add(R.id.layout_content_id, targetFragment).hide(currentFragment).commit();
}else{
transaction.show(targetFragment).hide(currentFragment).commit();
}
//当前展示的Fragment就是点击替换的Fragment
currentIndex = targetIndex;
}
//初始化数据
private void initData(){
for(int i=0;i<rgMain.getChildCount();i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
}
//程序运行 默认展示第一个Fragment
getSupportFragmentManager().
beginTransaction().
add(R.id.layout_content_id, list.get(0)).commit();
}
}
selecte_main.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@android:drawable/ic_menu_add"></item>
<item android:state_checked="false" android:drawable="@android:drawable/ic_menu_call"></item>
</selector>
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/rg_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:checked="true"
android:text="新闻" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:text="娱乐" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:text="体育" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selecte_main"
android:gravity="center"
android:text="财经" />
</RadioGroup>
<FrameLayout
android:id="@+id/layout_content_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
></FrameLayout>
</LinearLayout>
fragment_layout.xml
<?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" >
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="AAA"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/title_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="name"
/>
<TextView
android:id="@+id/content_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="aaa"
android:layout_alignBottom="@id/iv_item"
/>
</RelativeLayout>
21 RadioGroup ListFragment的更多相关文章
- 21 ViewPager RadioGroup
结构 MainActivity.java package com.qf.day21_viewpagerfragmentrg_demo4; import java.util.ArrayList; imp ...
- Android RadioGroup和RadioButton详解
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...
- RadioGroup实现导航栏
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- 无废话ExtJs 入门教程十[单选组:RadioGroup、复选组:CheckBoxGroup]
无废话ExtJs 入门教程十[单选组:RadioGroup.复选组:CheckBoxGroup] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个一个单选组,一个复 ...
- android自定义RadioGroup实现可以添加多种布局
android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到r ...
- Android控件系列之RadioButton&RadioGroup(转)
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...
- fragment做成选项卡,tab效果。 fragment+RadioGroup
fragment做成选项卡,tab效果. fragment+RadioGroup from://http://blog.csdn.net/zimo2013/article/details/122393 ...
- Android学习之RadioGroup和RadioButton
转载自:http://my.oschina.net/amigos/blog/59261 实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioG ...
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...
随机推荐
- CentOS6.9安装
我安装在VM的虚拟机中.具体安装方式网上很多,由于本机只能安装32位的linux系统,所以悬在了Centsos6.9版本.点此下载. 其中有一种是叫做LIVEDVD的版本,这种的值虚拟机中配置后,打开 ...
- 使用Nwjs开发桌面应用体验
之前一直用.net开发桌面应用,最近由于公司需要转为nodejs,但也是一直用nodejs开发后台应用,网站,接口等.近期,需要开发一个客户端,想着既然nodejs号称全栈,就试一下开发桌面应用到底行 ...
- [HNOI 2004]宠物收养场
Description 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的 ...
- 【luoguP4006 清华集训2017】小Y和二叉树
题目描述 小 Y 是一个心灵手巧的 OIer,她有许多二叉树模型. 小 Y 的二叉树模型中,每个结点都具有一个编号,小 Y 把她最喜欢的一个二叉树模型挂在了墙上,树根在最上面,左右子树分别在树根的左下 ...
- hdu 4267 线段树间隔更新
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1402 FFT(模板)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1348 Wall 凸包
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1348 题意:给出一个凸包,求出与凸包距离 L的外圈周长 凸包模板题,练练Andrew算法求出凸包周长再 ...
- Spring学习笔记1——入门
Spring是一个基于IOC和AOP的结构J2EE系统的框架 IOC 反转控制 是Spring的基础,Inversion Of Control 简单说就是创建对象由以前的程序员自己new 构造方法来调 ...
- html高度塌陷以及定位的理解
高度塌陷的含义: 父元素的高度,默认被子元素撑开,目前来讲box2多高,box1就多高.此时如果子元素设置浮动,则会导致其完全脱离文档流,子元素脱离文档流将无法撑开父元素, 导致父元素的高度丢失,就是 ...
- 华科机考:N阶楼梯上楼
时间限制:1秒空间限制:32768K 题目描述 N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入描述: 输入包括一个整数N,(1<=N<90). 输出描 ...