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 ...
随机推荐
- 【IDEA 2016】intellij idea tomcat jsp 热部署
刚开始用IDEA,落伍的我,只是觉得IDEA好看.可以换界面.想法如此的low. 真是不太会用啊,弄好了tomcat.程序启动竟然改动一下就要重启,JSP页面也一样. IDEA可以配置热部署,打开to ...
- 一个ListView怎么展示两种样式
private class MyBaseMsgAdapter extends BaseAdapter { //获取数据适配器中条目类型的总数,修改成两种(纯文本,输入+文字) @Override pu ...
- jq 根据值的正负变色
效果图这样: 意思就是根据最后的百分值变色,值为负变绿色,值为正变红色. 所以只要取到那个标签里的值了,就能根据正负的判断决定颜色. 我的html部分这样: /*不过他们都说我的dom结构不太合理,同 ...
- Linux查找命令
最近,我在学习Linux,下面是一些笔记. 使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序 ...
- 一些VS2013下使用QT和MFC的错误解决方案
http://blog.csdn.net/lovejiayang/article/details/51853191
- python table转空格
有需求: 预留,先上代码: import os def Table_Space(file_name,lis_out,tab_num = 4): file_str = open(file_name,&q ...
- AM335x kernel 4.4.12 i2c eeprom AT24c02驱动移植
kernel 4.4.12 i2c eeprom AT24c02驱动移植 在kernel make menuconfig ARCH=ARM 中打开: Device Drivers ---> Mi ...
- Mongodb命令集合
一.数据库常用命令 1.Help查看命令提示 复制代码代码如下: help db.help(); db.yourColl.help(); db.youColl.find().help(); rs.he ...
- 设置html title标题左侧的小图标
网页title旁边的小图片设置,图片要求格式必须是.ico,可以使用在线的转换工具把jpg和png图片转换为ico图片,工具地址:http://www.ico.la/ 在html文件中的<hea ...
- c#程序如何从海康ipserver查询获取DVR设备ip地址
海康威视提供的ipserver可以记录DVR设备的IP地址,并提供一个7071的端口给客户端查询用,我们在c#程序中可以用海康SDK包中的NET_DVR_GetDVRIPByResolveSvr函数来 ...