RadioGroup中的各个选择器

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@color/bg_checked"/>
<item android:state_checked="true" android:drawable="@color/bg_normal"/>
</selector>

bg_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/icon_favor_press"/>
<item android:state_checked="false" android:drawable="@mipmap/icon_favor_normal"/>
</selector>

favor_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/icon_home_press"/>
<item android:state_checked="false" android:drawable="@mipmap/icon_home_normal"/>
</selector>

home_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/icon_order_press"/>
<item android:state_checked="false" android:drawable="@mipmap/icon_order_normal"/>
</selector>

order_selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/icon_setting_press"/>
<item android:state_checked="false" android:drawable="@mipmap/icon_setting_normal"/>
</selector>

setting_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/icon_uc_press"/>
<item android:state_checked="false" android:drawable="@mipmap/icon_uc_normal"/>
</selector>

uc_selector.xml

各个Fragment

 public class FavorFragment extends Fragment{

     @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(android.R.layout.simple_list_item_1,null);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.BLUE);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("收藏");
tv.setTextSize(50);
}
}

FavorFragment.java

 public class OrderFragment extends Fragment{

     @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(android.R.layout.simple_list_item_1,null);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.RED);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("订单");
tv.setTextSize(50);
}
}

OrderFragment.java

 public class SettingFragment extends Fragment{

     @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(android.R.layout.simple_list_item_1,null);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.WHITE);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("设置");
tv.setTextSize(50);
}
}

SettingFragment.java

 public class UCFragment extends Fragment{

     @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(android.R.layout.simple_list_item_1,null);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.YELLOW);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("个人中心");
tv.setTextSize(50);
}
}

UCFragment.java

首页中还要显示一个ViewPager,一页机票,一页景点,可以左右滑动,自定义指示器

 public class JiPiaoFragment extends Fragment{

     @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(android.R.layout.simple_list_item_1,null);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.DKGRAY);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("机票");
tv.setTextSize(50);
}
}

JiPiaoFragment.java

 public class JingDianFragment extends Fragment{

     @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(android.R.layout.simple_list_item_1,null);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.MAGENTA);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("景点");
tv.setTextSize(50);
}
}

JingDianFragment.java

 public class HomeFragment extends Fragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

     List<Fragment> mList = new ArrayList<>();

     @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mList.add(new JiPiaoFragment());
mList.add(new JingDianFragment());
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_home,null);
return layout;
} RadioGroup home_radioGroup;
View view_indicator;
ViewPager home_viewPager; @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); home_radioGroup = (RadioGroup) view.findViewById(R.id.home_radioGroup);
view_indicator = view.findViewById(R.id.view_indicator);
home_viewPager = (ViewPager) view.findViewById(R.id.home_viewPager); //在Fragment中设置FragmentPagerAdapter必须使用getChildFragmentManager
FragmentManager fm = getChildFragmentManager(); home_viewPager.setAdapter(new MyFragmentPagerAdapter(fm,mList));
home_radioGroup.setOnCheckedChangeListener(this);
home_viewPager.addOnPageChangeListener(this);
//默认
home_radioGroup.check(R.id.jipiao);
initIndicator();
} int width;
public void initIndicator(){
//指示器长度分一半
width = getResources().getDisplayMetrics().widthPixels / 2;
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, FrameLayout.LayoutParams.MATCH_PARENT);
view_indicator.setLayoutParams(params);
} @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view_indicator.getLayoutParams();
int left = (int) ((position + positionOffset)*width);
params.setMargins(left,0,0,0);
view_indicator.setLayoutParams(params);
} @Override
public void onPageSelected(int position) {
home_radioGroup.check(position == 0?R.id.jipiao:R.id.jingdian);
} @Override
public void onPageScrollStateChanged(int state) { } @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
home_viewPager.setCurrentItem(checkedId == R.id.jipiao? 0 : 1);
}
}

HomeFragment.java

由于需要创建很多Fragment实例,这里用工厂模式或者反射方式创建

 public class FragmentUtils {

     //工厂模式

     public static final Fragment newInstance(int type){

         switch (type){
case 0:
return new FavorFragment();
case 1:
return new OrderFragment();
case 2:
return new HomeFragment();
case 3:
return new UCFragment();
case 4:
return new SettingFragment();
}
return null;
} //反射方式
public static final String[] classNames = {"Favor","Order","Home","UC","Setting"}; public static final Fragment getInstance(int type){
try {
//通过一个类的字符串对象,转换成一个class类类型
Class className = Class.forName("com.example.lesson10_viewpager_fragmentpageradapter_childfragment."+classNames[type]+"Fragment");
//通过类的类型去创建实例,必须有无参构造方法,并公开
return (Fragment) className.newInstance(); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} return null;
}
}

FragmentUtils.java

 public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

     List<Fragment> mList;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> mList) {
super(fm);
this.mList = mList;
} @Override
public Fragment getItem(int position) {
return mList.get(position);
} @Override
public int getCount() {
return mList.size();
}
}

MyFragmentPagerAdapter.java

 public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

     RadioGroup radioGroup;
Fragment[] fragments = new Fragment[5]; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
//默认显示首页
showFragment(2);
radioGroup.check(R.id.rb_home); }
int currIndex = -1; public void showFragment(int index){
if(index==currIndex){
return;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(currIndex != -1){
ft.hide(fragments[currIndex]);
} if(fragments[index]==null){
//创建,用工厂类
fragments[index] = FragmentUtils.newInstance(index);
ft.add(R.id.fl_content,fragments[index]);
}else{
ft.show(fragments[index]);
} currIndex = index;
ft.commit(); } @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View view = group.findViewById(checkedId);
String tag = view.getTag().toString();
showFragment(Integer.parseInt(tag));
} }

MainActivity.java

Android RadioGroup Fragment Viewpager FragmentPagerAdapter 去哪网Fragment嵌套的更多相关文章

  1. Viewpager模仿微信主布局的三种方式 ViewPager,Fragment,ViewPager+FragmentPagerAdapter

    效果大概就是这样 很简单 : 1 创建 top 和bottom 2主界面布局 添加top 和bottom 中间添加一个ViewPage 3 给ViewPager 和 底部View设置点击事件 源码下载 ...

  2. 笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法

    TabLayout的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  3. Android Tab -- 使用ViewPager、Fragment、FragmentPagerAdapter来实现

    原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 效果:滑动切换:点击标签切换. 代码:https://github.com/l ...

  4. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  5. Android应用经典主界面框架之二:仿网易新闻client、CSDN client (Fragment ViewPager)

    另外一种主界面风格则是以网易新闻.凤凰新闻以及新推出的新浪博客(阅读版)为代表.使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View.而是Fragment.所 ...

  6. Android控件-Fragment+ViewPager(高仿微信界面)

    什么是Fragment? Fragment是Android3.0后新增的概念,Fragment名为碎片,不过却和Activity十分相似,具有自己的生命周期,它是用来描述一些行为或一部分用户界面在一个 ...

  7. Android中Fragment+ViewPager的配合使用

    官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例.FragmentPa ...

  8. Android tab导航的几种方法:ActionBar tab +fragment,Viewpager+pagerTitleStrip,开源框架ViewPageIndicator 和 ViewPager

    action来实现tab标签 并跟fragment结合 因为要写新闻客户端这个tab导航是必须的 这里我写几个小练习,希望大家融会贯通. 1actionbar设置tab +fragment 布局是个l ...

  9. Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24740977 Android如今实现Tab类型的界面方式越来越多,今天就把常见的 ...

随机推荐

  1. 让一个Html元素撑满整个屏幕可以这样玩

    style="width:100%; height: 100%; overflow:hidden; position:absolute; top: 0; left: 0; z-index: ...

  2. 从SQL2008R2导入数据到SQL2005

    从SQL2008R2导入数据到SQL2005,数据很大,数据文件大概有120G. 尝试过直接离线附加,失败 尝试过备份还原,失败 最后找到了 1.先执行 exec sp_msforeachtable ...

  3. Android单元测试初探——Instrumentation(转载)

    学习Android有一段时间了,虽然前段时间对软件测试有了一些了解,不过接触android的单元测试却是头一次.这几天在物流大赛上也用了不少时间,所以对于android的单元测试没有太深入的研究,所以 ...

  4. CreateJS第1章 EaselJS基础 (画图)

    这章学学EaselJS的基本常用API首先下载createjs库,在项目文件里新建一个js文件夹放里面http://code.createjs.com/ 各种形状 var sp = new creat ...

  5. 【python常用模块】os.path

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  6. Python自动化运维之24、JQuery

    jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, ...

  7. C# mvc3 mvc4 伪静态及IIS7.5配置

    mvc3 mvc4路由配置 //单独路由 routes.MapRoute(    name: "XXX",    url: "Home/XXX.html/{id}&quo ...

  8. gulp配置browserify多入口

    需要 var es = require('event-stream'); gulp.task('browserify', function(){ var files = [ { fpath: './j ...

  9. oracle 中使用触发器自动生成UUID

    create or replace trigger tri_test before insert on test for each row declare begin if :new.uuid is ...

  10. Android Task 相关

    在日常开发过程中,只要涉及到activity,那么对task相关的东西总会或多或少的接触到,不过对task相关的一些配置的作用一直理解的还不是很透彻,官方文档在细节上说的也不够清楚,要透彻理解还是得自 ...