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_左右滑动的碎片的更多相关文章

  1. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  2. Android 学习笔记之AndBase框架学习(七) SlidingMenu滑动菜单的实现

    PS:努力的往前飞..再累也无所谓.. 学习内容: 1.使用SlidingMenu实现滑动菜单..   SlidingMenu滑动菜单..滑动菜单在绝大多数app中也是存在的..非常的实用..Gith ...

  3. ViewPager 详解(五)-----使用Fragment实现ViewPager滑动

    前言:前几篇文章讲解了ViewPager的普通实现方法,但Android官方最推荐的一种实现方法却是使用fragment,下面我们使用fragment来重新实现一下第一篇<ViewPager 详 ...

  4. 【Andord真】SlideMenu+ViewPagerIndictor双滑动边栏+滑动导航条

    采取SlideMenu达到的效果侧边栏: 间 setContentView是设置主背景的布局 setBehindContentView是设置左边菜单的布局 setSecondaryMenu是设置右边的 ...

  5. 【Android进阶】使用Andbase快速开发框架实现常见侧滑栏和滑动标签页组合效果

    最近闲来无事,在网上寻找源代码看,突然发现了一个国内技术牛人开发的快速开发框架Andbase,花了一天时间研究了下源码和怎么使用,现将开发常见的侧滑栏和滑动标签页组合效果的使用介绍个大家,希望可以减少 ...

  6. TabTopAutoLayout【自定义顶部选项卡区域(带下划线)(动态选项卡数据且可滑动)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义顶部选项卡布局LinearLayout类,实现带下划线且可滑动效果.[实际情况中建议使用RecyclerView] 备注:如果 ...

  7. FragmentTabHostAutoDemo【FragmentTabHost可滑动的选项卡】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(可滑动的效果)展现. 效果图 代码分析 1.该Demo中采用的是FragmentT ...

  8. TabLayoutViewPagerDemo【TabLayout+ViewPager可滑动】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用TabLayout搭配ViewPager实现可滑动的顶部选项卡效果. 效果图 代码分析 1.演示常规的设置. 2.通过自定义Vi ...

  9. Android学习之基础知识七—碎片的最佳实践

    一.Android碎片(Fragment)的最佳实践——简易版新闻应用 第一步:新建FragmentBestPractice项目,在app/build.gradle当中添加:RecyclerView ...

随机推荐

  1. 《软件设计师》——UML

    包含和扩展的区别,在于是否“必须”,如果是必须则是包含,如果不是必须则是扩展. 粗横线表示产生多个并行的任务.

  2. 写一个适应所有环境的js模块

    说下背景: 在ES6以前,JS语言没有模块化,如何让JS不止运行在浏览器,且能更有效的管理代码, 于是应运而生CommonJS这种规范,定义了三个全局变量: require,exports,modul ...

  3. 无中间变量交换swap(a,b)

    #include <stdio.h> /* 加减法 整型.浮点型(损失精度) */ void swap1(int *a,int *b) { *a=*a+*b; *b=*a-*b; *a=* ...

  4. sed命令详解

    搜索 纠正错误  添加实例 sed 功能强大的流式文本编辑器 补充说明 sed 是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时 ...

  5. JAVA基础学习——1.3 关于JAVA环境变量设定 Path,Java_Home,ClassPath

    在安装完JDK以后,要设定JAVA环境变量. 刚接触的时候,我很困惑,这特么什么玩意啊,然后知其然不知其所以然的 按照说明做完了却不知道为什么这么做,这里简单做一些总结说明. 首先,什么是环境变量 环 ...

  6. erlang 在线生成crashdump

    一般说来抓dump 4种 方式:      1. erlang:halt(“abort”).      2. 在erlang shell下输入CTRL C + “大写的A”      3.等着进程崩溃 ...

  7. tmux常用快捷键

    首先输入命令开始. $ tmux 然后,基本组合 Ctrl+b(同时按下ctrl和b),按完松开 松开后,按%(即同时按下shift和5),是水平分割 按“(即同时按shift和'),是纵向分割 按上 ...

  8. 如何将Sphinx生成的html文档集成进入Django

    参考 http://stackoverflow.com/questions/10594618/django-and-sphinx-how-to-view-the-html-sphinx-generat ...

  9. appium V1.5.x变化

    使用 npm安装 appium之后,会发现已经进入1.5 [Appium] Welcome to Appium v1.5.0 [Appium] Appium REST http interface l ...

  10. flask_sqlalchemy 命名遇到的一个小坑

    大概用了三个小时的时间. models.py class DriveRecord(db.Model): """drive record model"" ...