效果图:

文件夹结构:

代码分析:

MainActivity.java

package com.example.myfragment;

/**
* @author Arthur Lee
* @time 04/08/2014
* */
import java.util.ArrayList; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton; import com.fragment.FirstFragment;
import com.fragment.FourthFragment;
import com.fragment.SecondFragment;
import com.fragment.ThirdFragment; public class MainActivity extends FragmentActivity { ArrayList<Fragment> fragment;
public static ViewPager viewpager;
public static ArrayList<RadioButton> rb;
LinearLayout lLayout;
// ViewPager的适配器。但为了和Fragment相结合。要自己定义适配器
MyAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initButton();
initAdapter(); } public void initView() {
viewpager = (ViewPager) findViewById(R.id.main_viewpager);
lLayout = (LinearLayout) findViewById(R.id.zone_rbt);
fragment = new ArrayList<Fragment>();
fragment.add(new FirstFragment());
fragment.add(new SecondFragment());
fragment.add(new ThirdFragment());
fragment.add(new FourthFragment());
} public void initAdapter() {
adapter = new MyAdapter(getSupportFragmentManager(), fragment);
viewpager.setAdapter(adapter);
viewpager.setOnPageChangeListener(myOnPageChangeListener);
// 默认显示的页面
viewpager.setCurrentItem(0);
rb.get(0).setChecked(true); } public void initButton() {
rb = new ArrayList<RadioButton>();
rb.add((RadioButton) findViewById(R.id.rb_first));// 1号
rb.add((RadioButton) findViewById(R.id.rb_second));// 2号
rb.add((RadioButton) findViewById(R.id.rb_third));// 3号
rb.add((RadioButton) findViewById(R.id.rb_fourth));// 4号
for (int i = 0; i < rb.size(); i++) {
rb.get(i).setOnClickListener(new MyListener(i));
}
} public class MyListener implements OnClickListener {
private int index; public MyListener(int index) {
this.index = index;
} @Override
public void onClick(View v) {
viewpager.setCurrentItem(index);
rb.get(index).setChecked(true);
} } OnPageChangeListener myOnPageChangeListener = new OnPageChangeListener() { // 页卡状态改变时候。
@Override
public void onPageScrollStateChanged(int arg0) {
Log.i("Main", "onPageScrollStateChanged"); } // 页卡切换过程中
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.i("Main", "onPageScrolled");
} // 当radioButton被选中
@Override
public void onPageSelected(int position) { Log.i("Main", "onPageSelected");
rb.get(position).setChecked(true);
}
}; }

MyAdapter.java

package com.example.myfragment;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class MyAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragment; public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
} public MyAdapter(FragmentManager fm, ArrayList<Fragment> fragment) {
super(fm);
// TODO Auto-generated constructor stub
this.fragment = fragment;
} @Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return fragment.get(position);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return fragment.size();
} }

FirstFragment.java

package com.fragment;
/**
* @author Arthur Lee
* @time 04/08/2014
* */
import com.example.myfragment.R; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FirstFragment extends Fragment{
//缓存视图
private View view; @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//假设当前视图为空,初始化视图
if(view == null){
//指定当前视图在viewpager中显示的是view_first。xml,通过LayoutInflater来指定.
view = inflater.inflate(R.layout.view_first, null);
}
//指定当前视图的父类,以便调用父类的移除功能。 ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
} return view;
}
}

其它的三个与这个类似。不再提供。

以下是布局文件

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <!-- 纵向分布两个,以AB标记 --> <!-- A --> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" > <android.support.v4.view.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<!-- B --> <LinearLayout
android:id="@+id/zone_rbt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="horizontal" > <RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:text="1号" /> <RadioButton
android:id="@+id/rb_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_green_light"
android:text="2号" /> <RadioButton
android:id="@+id/rb_third"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:text="3号" /> <RadioButton
android:id="@+id/rb_fourth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_green_light"
android:text="4号" />
</RadioGroup>
</LinearLayout> </LinearLayout>

view_first.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" >
<!-- @author Arthur Lee -->
<TextView
android:id="@+id/view_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一页面"
android:textSize="20sp" /> </LinearLayout>

这一类的其他三个布局文件似。它不再可用。

版权声明:本文博主原创文章,博客,未经同意不得转载。

兔子--Fragment与ViewPager要切换滑动效果的更多相关文章

  1. 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析   1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3   2.Fragment生命周期以及与Activ ...

  2. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  3. 使用ViewPager实现屏幕滑动效果

    oncreate中设置viewPager viewPager.setPageTransformer(true,new DepthPageTransformer());//设置页面过滤动画效果 page ...

  4. android SlidingTabLayout实现ViewPager页卡滑动效果

    先来张效果图(能够滑动切换页卡) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fi ...

  5. Fragment利用ViewPager实现左右滑动--第三方开源--SlidingTabLayout和SlidingTabStrip实现

    MainActivity: package com.zzw.fragmentteb; import java.util.ArrayList; import android.graphics.Color ...

  6. 011 Android TabLayout+ViewPager实现顶部滑动效果(多个页面)

    1.TabLayout介绍 TabLayout提供了一个水平的布局用来展示Tabs,很多应用都有这样的设计,典型的有网易新闻,简书,知乎等.TabLayout就可以很好的完成这一职责,首先TabLay ...

  7. 使用ViewPager实现广告滑动效果

    效果图:               watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSk1DNjAx/font/5a6L5L2T/fontsize/400/ ...

  8. Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换

    一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...

  9. Android——ViewPager多页面滑动切换以及动画效果

    一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式,白色横条会移动到相应的页卡头标下.这是一个动画效果,白条是缓慢滑动过去的.好了, ...

随机推荐

  1. android面试题 不仅仅是面试是一个很好的学习

    下面的问题是在网上找到的总结,感谢您分享!希望,我们的共同进步,找到自己心仪的公司,: 1.android dvm 流程和Linux这个过程.无论是应用程序对同一概念: 答案:dvm是dalivk虚拟 ...

  2. enum可以做索引

    enum可以做索引 enum可以做索引, 配上虚函数,或者函数指针,可以实现上层的统一封装和快速索引. 点击(此处)折叠或打开 MoTbl.cpp #include <stdio.h> # ...

  3. C语言笔记之结构体

    结构的本质是C语言的一种数据抽象,通俗的说,是基本数据类型的重组. 为什么要重组呢?由于基本数据类型不够用了.为什么不够用了呢?由于须要的信息类型太多了. 这是一个非常大的话题.信息本来是没有什么类型 ...

  4. mysql left join,right join,inner join用法分析

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  5. 在word 中复选框划勾或叉的方法

    输入大写字母R.大写字母Q ,然后将字体改为Wingdings 2, 就分离得到带框的勾和叉.

  6. ftk学习记(waitbox篇)

    [声明:版权全部.欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 前面说到了脚本.那么就看看ftk中demo与script搭配的效果是什么样的? 上面的效果图就相 ...

  7. xcode多target

    原文:http://www.codza.com/free-iphone-app-version-from-the-same-xcode-project There are more than 15,0 ...

  8. JAVA 读取图片储存至本地

    需求:serlvet经过处理通过报表工具返回一张报表图(柱状图 折线图). 现在需要把这个图存储到本地 以便随时查看 // 构造URL URL url = new URL(endStr); // 打开 ...

  9. Linux智能小开关rfkill

    Linux智能小开关rfkill Rfkill,当中rf是Radio frequency(射频).主要作用是一个专门管理开关的子系统,举例说明Android手机的通知栏能够方便地开关Airplane/ ...

  10. (Android+IOS)我们正在做一个新闻App,做几乎一样的,倾听您的建议 (画画)

    (Android+IOS)我们正在做一个新闻App,做几乎一样的,倾听您的建议! 新闻采访是做,前端展示APP界面感觉还不是非常好,还须要改进改进,希望公布(Android和IOS版本号)前听听大家的 ...