Fragment前篇:

《Android Fragment初探:静态Fragment组成Activity》

ViewPager前篇:

《Android ViewPager初探:让页面滑动起来》

《Android ViewPager再探:增加滑动指示条》

这篇算是对之前学习写下的3篇博客知识总结吧~

程序的总体结构如下:

(其中listview.xml为测试项,可忽略)

其中,layout1对应Fragment1,以此类推;layout1中有listview,layout2和layout3只有根布局LinearLayout,区别是背景颜色。

layout1.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:background="#ffffff"
android:orientation="vertical" > <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

activity_main.xml和《Android ViewPager再探:增加滑动指示条》 一文中一样,下面是代码:

 <RelativeLayout 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"
tools:context=".MainActivity"> <LinearLayout
android:id="@+id/ll_tab"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#FFFFFF" > <TextView
android:id="@+id/tv_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="First"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Second"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_third"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Third"
android:textSize="20sp" />
</LinearLayout> <ImageView
android:id="@+id/cursor"
android:layout_below="@id/ll_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@mipmap/slidebar"
android:contentDescription="slidebar"/> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_below="@id/cursor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:flipInterval="30"
android:persistentDrawingCache="animation" /> </RelativeLayout>

再来看布局的实现部分:

因为layout2和layout3里没什么东西,所以以Fragment1为例:

 package com.example.fragmentviewpager.fragmentviewpager;

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; /**
* Created by LT on 2015/7/29.
*/
public class Fragment1 extends Fragment { private ListView listView; public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.layout1, container, false);
listView = (ListView)view.findViewById(R.id.lv);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,getData());
listView.setAdapter(arrayAdapter); return view;
} private List<String> getData(){
List<String> data = new ArrayList<String>();
for(int i = 0;i <20;i++) {
data.add(i+"");
}
return data;
}
}

此处给layout1里的listview添加了20项,名字分别是1——20,用的适配器是ArrayAdapter。

考虑到有三个fragment存在(Fragment1,Fragment2,Fragment3),为了方便viewpager管理,我们自定义一个适配器,继承自FragmentPagerAdapter:

FPAdapter.java:

 package com.example.fragmentviewpager.fragmentviewpager;

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import java.util.List; /**
* Created by LT on 2015/7/29.
*/
public class FPAdapter extends FragmentPagerAdapter {
private List<Fragment> pFragment; public FPAdapter(FragmentManager fragmentManager,List<Fragment> fragments){
super(fragmentManager);
this.pFragment = fragments;
} @Override
public Fragment getItem(int arg0){
return pFragment.get(arg0);
} @Override
public int getCount(){
return pFragment.size();
}
}

FragmentPagerAdapter只需要重写

public Fragment getItem(int arg0)

public int getCount()

这两个函数即可。

与之对应的适配器初始化和设定代码如下:

 //构造适配器
fragments=new ArrayList<Fragment>();
fragments.add(new Fragment1());
fragments.add(new Fragment2());
fragments.add(new Fragment3());
FPAdapter adapter = new FPAdapter(getSupportFragmentManager(), fragments); //设定适配器
vp = (ViewPager)findViewById(R.id.viewpager);
vp.setAdapter(adapter);

和上一篇《Android ViewPager再探:增加滑动指示条》的代码相结合,最终的MainActivity如下:

 package com.example.fragmentviewpager.fragmentviewpager;

 import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends FragmentActivity {
private TextView first,second,third; /*滑动条相关定义*/
private ImageView cursor;
private int bmp_width = 0;//游标宽度
private int offset = 0;//游标图片偏移量
private int number = 0;//当前页面编号
private ViewPager vp;
private List<Fragment> fragments; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //TAB初始化
first = (TextView)findViewById(R.id.tv_first);
second = (TextView)findViewById(R.id.tv_second);
third = (TextView)findViewById(R.id.tv_third); //构造适配器
fragments=new ArrayList<Fragment>();
fragments.add(new Fragment1());
fragments.add(new Fragment2());
fragments.add(new Fragment3());
FPAdapter adapter = new FPAdapter(getSupportFragmentManager(), fragments); //初始化指示器位置
initCursorPos(); //设定适配器
vp = (ViewPager)findViewById(R.id.viewpager);
vp.setAdapter(adapter); vp.setCurrentItem(0);//设置当前页
vp.setOnPageChangeListener(new NewPageChangeListener());//监听页面改变 /*Tab页面监听*/
first.setOnClickListener(new TabOnClickListener(0));
second.setOnClickListener(new TabOnClickListener(1));
third.setOnClickListener(new TabOnClickListener(2));
} //初始化指示器位置
public void initCursorPos() {
// 初始化动画
cursor = (ImageView) findViewById(R.id.cursor);
bmp_width = BitmapFactory.decodeResource(getResources(), R.mipmap.slidebar)
.getWidth();// 获取图片宽度 DisplayMetrics dm = new DisplayMetrics();//初始化DisplayMetrics对象
getWindowManager().getDefaultDisplay().getMetrics(dm);//将当前窗口信息放入DisplayMetrics类中
int s_width = dm.widthPixels;// 获取分辨率宽度 offset = (s_width / fragments.size() - bmp_width) / 2;// 计算偏移量(保证滑动条在该tab下正中间) Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
cursor.setImageMatrix(matrix);// 设置动画初始位置
} //页面改变监听器
public class NewPageChangeListener implements ViewPager.OnPageChangeListener { int one = offset * 2 + bmp_width;// 页卡1 -> 页卡2 偏移量
int two = one * 2;// 页卡1 -> 页卡3 偏移量 @Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0:
if (number == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
} else if (number == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
}
break;
case 1:
if (number == 0) {
animation = new TranslateAnimation(offset, one, 0, 0);
} else if (number == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
}
break;
case 2:
if (number == 0) {
animation = new TranslateAnimation(offset, two, 0, 0);
} else if (number == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
}
break;
}
number = arg0;
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(300);
cursor.startAnimation(animation);
} @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
} @Override
public void onPageScrollStateChanged(int arg0) {
}
} /*Tab页面点击监听*/
public class TabOnClickListener implements View.OnClickListener{
private int num = 0; public TabOnClickListener(int index){
num = index;
} @Override
public void onClick(View v){
vp.setCurrentItem(num);
}
} }

ViewPager+Fragment再探:和TAB滑动条一起三者结合的更多相关文章

  1. Android ViewPager再探:增加滑动指示条

    上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...

  2. Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换

    一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...

  3. 转:ViewPager+Fragment基本使用方法(附源码)

    ViewPager+Fragment可以做出多页面滑动效果,让我们的应用程序界面操作起来更加灵活 对于ViewPager和Fragment组件还不熟悉的朋友,可以先看看相关的资料 首先在activit ...

  4. ViewPager+Fragment基本使用方法(附源码)

    ViewPager+Fragment可以做出多页面滑动效果,让我们的应用程序界面操作起来更加灵活 对于ViewPager和Fragment组件还不熟悉的朋友,可以先看看相关的资料. 首先在activi ...

  5. Android中ViewPager实现滑动条及与Fragment结合的实例教程

    ViewPager类主要被用来实现可滑动的视图功能,这里我们就来共同学习Android中ViewPager实现滑动条及与Fragment结合的实例教程,需要的朋友可以参考下 自主实现滑动指示条先上一个 ...

  6. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  7. 仿百度壁纸客户端(一)——主框架搭建,自定义Tab+ViewPager+Fragment

    仿百度壁纸客户端(一)--主框架搭建,自定义Tab+ViewPager+Fragment 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Fragment ...

  8. 使用ViewPager+Fragment来实现带滚动条的多屏滑动-IndicatorFragmentActivity

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17201587 介绍 在android应用中,多屏滑动是一种很常见的风格,博主 ...

  9. Android两种为ViewPager+Fragment添加Tab的方式

    在Android开发中ViewPager的使用是非常广泛的,而它不仅仅能够实现简单的开始引导页,还可以结合Fragment并添加Tab作为选项卡或为显示大批量页面实现强大的顺畅滑动 下面介绍两种为Vi ...

随机推荐

  1. Mybatis分页插件 - PageHelper

    1.说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种 ...

  2. 网上图书商城3--Book模块

    小技巧一:分页 ①PageBean<Book> findByCriteria(List<Expression> exprList, int pc)  --- 通用的查询方法(p ...

  3. WebSocket 学习笔记--IE,IOS,Android等设备的兼容性问题与代码实现

    一.背景 公司最近准备将一套产品放到Andriod和IOS上面去,为了统一应用的开发方式,决定用各平台APP嵌套一个HTML5浏览器来实现,其中数据通信,准备使用WebSocket的方式.于是,我开始 ...

  4. Codrops 教程:实现内容倾斜的 3D 幻灯片效果

    今天给大家分享的优秀教程来自 Codrops 网站,实现一个内容倾斜的 3D 幻灯片效果.我们平常见到的都是那种水平或者垂直滚动的效果,这个倾斜的内容滑动效果相信会让你眼前一亮.因为使用了 CSS 3 ...

  5. FM四舍五入_从小数点最后一位进位

    原贴地址:http://jiahongguang12.blog.163.com/blog/static/334665720071060551591/ 输入参数12.5445,因此FM从小数点最后一位进 ...

  6. ae显示标注

    //添加标注,比TextElment功能更强大 public static void ToAddAnnotate(ILayer layer, string fieldName) { IGeoFeatu ...

  7. ARCGIS server没有服务、silverlight不能调试、windows server2008安装时跳出“安装程序无法创建新的系统分区也无法定位现有的系统分区”的解决方案

    1.某个系统服务没开启 2.默认浏览器设置为IE.(IE内核有时候也不能调试) 3.BIOS里面的SATA设置为开启.

  8. 解决IIS进程回收后第一次访问慢的问题

    IIS 有一种机制,默认会在IIS空闲一定时间段后,将应用程序池进行回收,这个时间段在IIS6中默认是20分钟,在IIS7中默认是1740分钟.两个配置都不合理,都会导致当应用程序池被回收后,第一次访 ...

  9. 计算c字符的长度,保证不超过2^30

    来自sqlite3源码 /* ** Compute a string length that is limited to what can be stored in ** lower 30 bits ...

  10. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...