ViewPage + RadioGroup + Fragment学习
底部是RadioGroup中RadioButton的切换。上面时ViewPage ,能够滑动,假设你们的需求是不须要滑动的话,那就直接用FrameLayout就能够了。
以下将会用两种方式实现。请大家看代码。
效果图:
方式一:ViewPage + RadioGroup+Fragment 能够左右滑动
1.RadioGroup设置监听setOnCheckedChangeListener,改变上面的四个Fragment
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rb0:
viewPager.setCurrentItem(0);
break;
case R.id.rb1:
viewPager.setCurrentItem(1);
break;
case R.id.rb2:
viewPager.setCurrentItem(2);
break;
case R.id.rb3:
viewPager.setCurrentItem(3);
break;
}
}
2.ViewPager设置监听setOnPageChangeListener。改变RadioButton的选中
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
if (radioGroup.getCheckedRadioButtonId() != R.id.rb0) {
radioGroup.check(R.id.rb0);
}
break;
case 1:
if (radioGroup.getCheckedRadioButtonId() != R.id.rb1) {
radioGroup.check(R.id.rb1);
}
break;
case 2:
if (radioGroup.getCheckedRadioButtonId() != R.id.rb2) {
radioGroup.check(R.id.rb2);
}
break;
case 3:
if (radioGroup.getCheckedRadioButtonId() != R.id.rb3) {
radioGroup.check(R.id.rb3);
}
break;
}
}
3.ViewPager适配器
public class ViewPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
} @Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return fragments.get(position);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return fragments.size();
} }
方式二:RadioGroup+Fragment
1.RadioGroup设置监听setOnCheckedChangeListener。改变上面的四个Fragment
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb0:
manager.chReplaceFrag(fragment01, "fragment01", false);
break;
case R.id.rb1:
manager.chReplaceFrag(fragment02, "fragment02", false);
break;
case R.id.rb2:
manager.chReplaceFrag(fragment03, "fragment03", false);
break;
case R.id.rb3:
manager.chReplaceFrag(fragment04, "fragment04", false);
break;
}
}
2.假设你想要左右滑动,也是能够的,获取控件的实例,你要提出来。我这里是为了贴代码的完整性
private float startx;
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
super.onTouchEvent(event);
RadioButton rb0 = (RadioButton) findViewById(R.id.rb0);
RadioButton rb1 = (RadioButton) findViewById(R.id.rb1);
RadioButton rb2 = (RadioButton) findViewById(R.id.rb2);
RadioButton rb3 = (RadioButton) findViewById(R.id.rb3);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startx = event.getX();
break;
case MotionEvent.ACTION_UP:
float endx = event.getX();
if (startx - endx > 100) {//右边
switch (rg.getCheckedRadioButtonId()) {
case R.id.rb0:
rb1.performClick();
break;
case R.id.rb1:
rb2.performClick();
break;
case R.id.rb2:
rb3.performClick();
break;
case R.id.rb3:
rb0.performClick();
break;
}
}else if (endx - startx > 100 ) {
switch (rg.getCheckedRadioButtonId()) {
case R.id.rb0:
rb3.performClick();
break;
case R.id.rb1:
rb0.performClick();
break;
case R.id.rb2:
rb1.performClick();
break;
case R.id.rb3:
rb2.performClick();
break;
}
}
break;
}
return false;
}
整个代码的布局设计:
1.FrameLayout+RadioGroup
<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:layout_height="match_parent"> <FrameLayout
android:id="@+id/fragment_container"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_width="match_parent"/> <RadioGroup
android:id="@+id/main_rg"
android:layout_height="50dip"
android:layout_width="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb0"
android:checked="true"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_01_bg"
android:text="精选"/>
<RadioButton
android:id="@+id/rb1"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_02_bg"
android:text="分类"/>
<RadioButton
android:id="@+id/rb2"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_03_bg"
android:text="投资"/>
<RadioButton
android:id="@+id/rb3"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_04_bg"
android:text="我的"/>
</RadioGroup> </LinearLayout>
2.ViewPage+FrameLayout+RadioGroup
<?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:background="@android:color/white"
android:orientation="vertical" > <android.support.v4.view.ViewPager
android:id="@+id/viewpage"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_width="match_parent"/> <RadioGroup
android:id="@+id/main_rg"
android:layout_height="50dip"
android:layout_width="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb0"
android:checked="true"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_01_bg"
android:text="精选"/>
<RadioButton
android:id="@+id/rb1"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_02_bg"
android:text="分类"/>
<RadioButton
android:id="@+id/rb2"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_03_bg"
android:text="投资"/>
<RadioButton
android:id="@+id/rb3"
style="@style/main_tab_style"
android:drawableTop="@drawable/tab_04_bg"
android:text="我的"/>
</RadioGroup> </LinearLayout>
3.文本颜色选择器
<? xml version="1.0" encoding="utf-8"? >
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/green_common"/>
<item android:state_checked="false" android:color="@color/bg_gray_dark"/>
</selector>
4.背景图片选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/ic_main_tab_01_active"></item>
<item android:state_checked="false" android:drawable="@drawable/ic_main_tab_01_normal"></item>
</selector>
这里仅仅是贴出了核心的代码,须要整个可执行的代码,请点击以下的连接
ViewPage + RadioGroup + Fragment学习的更多相关文章
- 33.Android之Fragment学习
Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...
- RadioGroup+Fragment 使用Fragment的add()方法,防止使用replace每次都重新加载页面,造成资源浪费
radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下: getSupportFragmentManager().be ...
- Android RadioGroup Fragment Viewpager FragmentPagerAdapter 去哪网Fragment嵌套
RadioGroup中的各个选择器 <selector xmlns:android="http://schemas.android.com/apk/res/android"& ...
- Android之Fragment学习总结(1)
对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...
- Fragment学习笔记
Fragment为大量型号,尺寸,分辨率的设备提供了一种统一的UI优化方案.将Activity分解为多个Fragment,将极大地提高UI的灵活性,也更容易为一些新的设备配置带来更好的用户体验. on ...
- Android Fragment学习笔记(二)----Fragment界面添加和管理
Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...
- Android之Fragment学习笔记②(Fragment生命周期)
一. Fragment生命周期图 二.Fragment生命周期方法介绍 Fragment的生命周期和activity生命周期很像,其生 ...
- Android之Fragment学习笔记①
Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...
- ViewPage显示Fragment集合实现左右滑动并且出现tab栏--第三方开源--SlidingTabLayout和SlidingTabStrip实现
注意:有关Fragment的方法和ViewPager的全部是android.support.v4包的,否则会报很多的错误 MainActivity: package com.zzw.fragmentt ...
随机推荐
- Shell脚本里的双冒号是什么意思
这个是代码开发风格,其实也就是一个函数名,相当于下划线分割,但改读成包名之后就意义不一样.这个是根据Google的Shell开发规范进行定义的. 参考: https://google.github.i ...
- [重要更新][Quartus II][14.1正式版]
[Quartus II][14.1正式版] ----14.1版本最大的变化就是增加了2大系列的器件库: MAX 10和Arria 10.这2大系列据Altera中国区代理 骏龙科技的人说,就是为了和X ...
- Bootstrap 3之美06-Page Header、Breadcrumbs、Dropdowns、Button Dropdowns、用Button和Dropdowns模拟Select、Input Groups、Thumbnails、Panels、Wells
本篇主要包括: ■ Page Header■ Breadcrumbs■ Button Groups■ Dropdowns■ Button Dropdowns■ 用Button和Dropdo ...
- 服务器返回的“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0提供程序””错误解决
未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0提供程序”
- andriod 文本居中: android:gravity="center"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- UILabel混合显示动画效果
UILabel混合显示动画效果 效果 源码 https://github.com/YouXianMing/Animations // // MixedColorProgressViewControll ...
- Error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
32位系统:ln -s /opt/base/3.3/lib/libpq.so.5 /usr/lib/libpq.so.5 64位系统:ln -s /opt/base/3.3/lib/libpq.so. ...
- Pycharm 增加 run 控制台缓冲行数
Yes, you can change the value of idea.cycle.buffer.size in bin/idea.properties under the PyCharm ins ...
- 使用supervisor过程的坑
1.安装:由于使用的是公司的虚拟机,所以使用pip install supervisor的过程遇到很多权限问题. 中间尝试使用sudo pip install supervisor的方式安装,但是使用 ...
- Spring 远程服务
稍微看了一下Spring的远程服务章节,讲到了RMI,Hessian,Burlap,Http invoker以及JAX-WS 1.RMI 原理: 1)在Spring服务端使用RmiServiceExp ...