FragmentActivity_左右滑动的碎片
test1.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:background="#4ADF2B"
android:textColor="#ffffff"
android:textSize="20dp"
android:text="悦动"
/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent"
android:background="@drawable/yuedong22"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
Activity_text1.java要继承Fragment 跟上面test1.xml进行适配
package com.example.speed_app; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Activity_text1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.text1, container,false);
return view;
}
}
上面的Activity_text1.java 和 test1.xml双双多个 这里就弄一个,我的项目弄了多个 因为都是一样的方法
上面准备好了
接下来才是我们要去实现的
caidan_dibu.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="horizontal" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/ledong"
android:gravity="center"
android:textColor="@drawable/color"
android:text="悦动" /> <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/tiaozhan"
android:button="@null"
android:textColor="@drawable/color"
android:text="挑战" android:gravity="center"
/> <RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/faxian"
android:gravity="center"
android:textColor="@drawable/color"
android:text="发现" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/quanz"
android:text="圈子"
android:textColor="@drawable/color"
android:gravity="center"
/>
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:textColor="@drawable/color"
android:drawableTop="@drawable/wo3"
android:text="我"
android:gravity="center"
/>
</RadioGroup>
</LinearLayout>
结果图:

home.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
> <android.support.v4.view.ViewPager //这个是一个v4包,这个包放在你要切换布局里面在下图的框里
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/caidan_dibu"/>
</LinearLayout>
</LinearLayout>
结果:

MyFragmentPagerAdapter.java 实现适配器
package com.example.speed_app; import java.util.List; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
//FragmentPagerAdapter是Android-support-v4支持包里面出现的一个新的适配器
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter { private FragmentManager fragmentManager;
private List<Fragment> fragmentList;
public MyFragmentPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragmentList) {
super(fragmentManager);
this.fragmentManager = fragmentManager;
this.fragmentList = fragmentList;
} @Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return fragmentList.get(arg0);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return fragmentList.size();
} }
Activity_home.java 要继承FragmentActivity
package com.example.speed_app;
import java.util.ArrayList;
import java.util.List; import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RadioButton;
public class Activity_home extends FragmentActivity{
private int currIndex;//记录滑动的第几个
private ViewPager viewPager;//ViewPager滚动功能
private List<Fragment> fragmentList;//一共有多少个Fragment集合
private RadioButton radioButton1,radioButton2,radioButton3,radioButton4,radioButton5; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home); //z初始化 viewpager
viewPager = (ViewPager) findViewById(R.id.vp);
fragmentList = new ArrayList<Fragment>();
Fragment fragment1 = new Activity_text1();//滑动页面的布局1
Fragment fragment2 = new Activity_text2();//滑动页面的布局2
Fragment fragment3 = new Activity_text3();//滑动页面的布局3
Fragment fragment4 = new Activity_text4();//滑动页面的布局4
Fragment fragment5 = new Activity_text5();//滑动页面的布局5 fragmentList.add(fragment1);//添加到集合保存
fragmentList.add(fragment2);//添加到集合保存
fragmentList.add(fragment3);//添加到集合保存
fragmentList.add(fragment4);//添加到集合保存
fragmentList.add(fragment5);//添加到集合保存 //FragmentManager能够实现管理activity中fragment. 通过调用activity的getFragmentManager()取得它的实例
FragmentManager fragmentManager = getSupportFragmentManager();//V4的包 //FragmentPagerAdapter类
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fragmentManager, fragmentList);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);//默认从第1个开始 也就是Activity_text1()这个Fragment开始 viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override
public void onPageSelected(int arg0) { // TODO Auto-generated method stub
//Animation animation = new TranslateAnimation(0,0,0,0);
//animation.setFillAfter(true);
currIndex = arg0;//获取目前滑动的坐标 radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
radioButton5 = (RadioButton) findViewById(R.id.radioButton5); switch (arg0) {
case 0:
radioButton1.setChecked(true);//如果是0坐标,那radioButton1这个单选按钮为true,也就是点击状态
break;
case 1:
radioButton2.setChecked(true);
break;
case 2:
radioButton3.setChecked(true);
break;
case 3:
radioButton4.setChecked(true);
break;
case 4:
radioButton5.setChecked(true);
break;
default:
break;
} } @Override
public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override
public void onPageScrollStateChanged(int arg0) { }
}); //下面可以通过点击radioButton按钮实现切换页面
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
radioButton5 = (RadioButton) findViewById(R.id.radioButton5); radioButton1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
viewPager.setCurrentItem(0);//这个0代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text1() }
}); radioButton2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
viewPager.setCurrentItem(1);//这个1代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text2() }
}); radioButton3.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
viewPager.setCurrentItem(2);//这个2代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text3() }
}); radioButton4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(3);//这个3代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text4() }
}); radioButton5.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
viewPager.setCurrentItem(4);//这个4代表坐标,但点击radioButton1这个按钮之后会显示坐标为0的页面也就是 Activity_text5()
}
});
} }




FragmentActivity_左右滑动的碎片的更多相关文章
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- Android 学习笔记之AndBase框架学习(七) SlidingMenu滑动菜单的实现
PS:努力的往前飞..再累也无所谓.. 学习内容: 1.使用SlidingMenu实现滑动菜单.. SlidingMenu滑动菜单..滑动菜单在绝大多数app中也是存在的..非常的实用..Gith ...
- ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
前言:前几篇文章讲解了ViewPager的普通实现方法,但Android官方最推荐的一种实现方法却是使用fragment,下面我们使用fragment来重新实现一下第一篇<ViewPager 详 ...
- 【Andord真】SlideMenu+ViewPagerIndictor双滑动边栏+滑动导航条
采取SlideMenu达到的效果侧边栏: 间 setContentView是设置主背景的布局 setBehindContentView是设置左边菜单的布局 setSecondaryMenu是设置右边的 ...
- 【Android进阶】使用Andbase快速开发框架实现常见侧滑栏和滑动标签页组合效果
最近闲来无事,在网上寻找源代码看,突然发现了一个国内技术牛人开发的快速开发框架Andbase,花了一天时间研究了下源码和怎么使用,现将开发常见的侧滑栏和滑动标签页组合效果的使用介绍个大家,希望可以减少 ...
- TabTopAutoLayout【自定义顶部选项卡区域(带下划线)(动态选项卡数据且可滑动)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡布局LinearLayout类,实现带下划线且可滑动效果.[实际情况中建议使用RecyclerView] 备注:如果 ...
- FragmentTabHostAutoDemo【FragmentTabHost可滑动的选项卡】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(可滑动的效果)展现. 效果图 代码分析 1.该Demo中采用的是FragmentT ...
- TabLayoutViewPagerDemo【TabLayout+ViewPager可滑动】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用TabLayout搭配ViewPager实现可滑动的顶部选项卡效果. 效果图 代码分析 1.演示常规的设置. 2.通过自定义Vi ...
- Android学习之基础知识七—碎片的最佳实践
一.Android碎片(Fragment)的最佳实践——简易版新闻应用 第一步:新建FragmentBestPractice项目,在app/build.gradle当中添加:RecyclerView ...
随机推荐
- Xms Xmx PermSize MaxPermSize 区别
Eclipse崩溃,错误提示: MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) s ...
- c/c++ long long 和__64int区别
在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说,64位整型的定义方式有long long和__int64两种(VC还 ...
- linux 命令01
mkdir 创建目录 cd 进入目录 touch 创建文件 touch oldboy.txt vi 编辑器,相当于记事本,有编辑功能,较弱 vim 复杂编辑器,相当于,emeditor,editplu ...
- windows安装redis
下载安装包,由于redis不提供windows版本,但是通过官网了解,如下: The Redis project does not officially support Windows. Howeve ...
- jQuery sibings()的作用
jQuery sibings()的作用: siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的. 当我们要对一个<li></li>列表的操作的时候,只 ...
- eclipse中ctrl+h默认打开是JavaSearch,怎么设置成默认打开是FileSearch
window->preferences->General->keys. 找到File Search(有搜索框的,可以搜索),然后在下方 Binding按下ctrl +h .
- maven实战(04)_在pom中使用properties
使用常量不仅让代码变得简洁,更重要的是可以避免重复,在需要更改的时候,只需要修改一处,降低了错误发生的概率. <project> <modelVersion>4.0.0< ...
- ip地址转换
通常,我们用点分十进制字符串表示ipv4地址(192.168.1.1),用十六进制字符串表示ipv6地址(fe80::20c:29ff:fee9:4bcc). ipv4转换函数 #include &l ...
- SQL Server子查询实例
例子一 SELECT COUNT(*) FROM ( SELECT [PersonID] FROM [tbiz_AssScore] GROUP BY PersonID ) M 语法说明: 1).FRO ...
- matlab更改打开时候默认路径
每次打开matlab都会的修改默认路径,是一件有些烦恼的事情.所以,就想尝试更改默认路径 方法如下: 1.在matlab安装目录,找到toolbox文件夹,打开local文件件,打开matlabrc. ...