Android RadioGroup Fragment Viewpager FragmentPagerAdapter 去哪网Fragment嵌套
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嵌套的更多相关文章
- Viewpager模仿微信主布局的三种方式 ViewPager,Fragment,ViewPager+FragmentPagerAdapter
效果大概就是这样 很简单 : 1 创建 top 和bottom 2主界面布局 添加top 和bottom 中间添加一个ViewPage 3 给ViewPager 和 底部View设置点击事件 源码下载 ...
- 笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法
TabLayout的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Android Tab -- 使用ViewPager、Fragment、FragmentPagerAdapter来实现
原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 效果:滑动切换:点击标签切换. 代码:https://github.com/l ...
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- Android应用经典主界面框架之二:仿网易新闻client、CSDN client (Fragment ViewPager)
另外一种主界面风格则是以网易新闻.凤凰新闻以及新推出的新浪博客(阅读版)为代表.使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View.而是Fragment.所 ...
- Android控件-Fragment+ViewPager(高仿微信界面)
什么是Fragment? Fragment是Android3.0后新增的概念,Fragment名为碎片,不过却和Activity十分相似,具有自己的生命周期,它是用来描述一些行为或一部分用户界面在一个 ...
- Android中Fragment+ViewPager的配合使用
官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例.FragmentPa ...
- Android tab导航的几种方法:ActionBar tab +fragment,Viewpager+pagerTitleStrip,开源框架ViewPageIndicator 和 ViewPager
action来实现tab标签 并跟fragment结合 因为要写新闻客户端这个tab导航是必须的 这里我写几个小练习,希望大家融会贯通. 1actionbar设置tab +fragment 布局是个l ...
- Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24740977 Android如今实现Tab类型的界面方式越来越多,今天就把常见的 ...
随机推荐
- OS X Yosemite下安装Hadoop2.5.1伪分布式环境
最近开始学习Hadoop,一直使用的是公司配好的环境.用了一段时间后发现对Hadoop还是一知半解,故决定动手在本机上安装一个供学习研究使用.正好自己用的是mac,所以没啥说的,直接安装. 总体流程 ...
- 给id赋值
var div = document.getElementByTagName('div') div.id="mydiv";div.setAttribute("id&quo ...
- ICE学习第四步-----客户端请求服务器返回数据
这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...
- sphinx ---rotate 运行机制
如果sphinx在运行中,要indexer时,需要加上--rotate参数,这样索引完就直接生效了. 原因是sphinx的searchd在启动时会创建一个 .spl 锁文件,并在关闭时会删除它.在in ...
- CreateJS第0章- Canvas基础
最近网页游戏比较火,以前做过一些小游戏,但是过段时间就都忘了,今天在这里记录一下学习过程,以备后用.做网页游戏有很多种框架,我是flash程序用Adobe出品的CreateJS最容易.基本上继承了fl ...
- Notepad++ Shortcuts(Chinese and English Version)
Ctrl+C 复制Ctrl+X 剪切Ctrl+V 粘贴Ctrl+Z 撤消Ctrl+Y 恢复Ctrl+A 全选Ctrl+F 键查找对话框启动Ctrl+H 查找/替换对话框Ctrl+D 复制并粘贴当行 C ...
- HDU 1995
Problem Description 用1,2,...,n表示n个盘子,称为1号盘,2号盘,....号数大盘子就大.经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故. ...
- 在uboot上创建菜单
一.原理 菜单其实就是一个uboot中的命令,和其他的命令没有什么差别. uboot启动时,如果进入uboot命令模式,先运行这个命令,就会打印出一个菜单界面. 在uboot的命令模式,通过键入“m ...
- 在 iOS 应用中直接跳转到 AppStore 的方法--备用
找到应用程序的描述链接,比如:http://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8 然后将 http:// 替换为 itms ...
- ZT: WEB学习资料
根据个人经验整理一些Web开发的一些技术书籍,希望对需要进入Web开发领域的TX有所指导,由于是在家里整理的,所有每本书对应的豆瓣链接打不开,如果整理有缺陷请多多指出. 入门篇:通过相关技术的全面基础 ...