ViewPager的简单使用
1、布局文件
a、主布局文件
<?xml version="1.0" encoding="utf-8"?>
<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="lpc.com.animation2demo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:id="@+id/tv1"
android:text="苹果"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv2"
android:text="香蕉"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:text="梨"
android:id="@+id/tv3"
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:src="@mipmap/line"
android:scaleType="matrix"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager1"
android:layout_weight="1"
android:layout_width="match_parent"
android:flipInterval="30"
android:layout_gravity="center"
android:persistentDrawingCache="animation"
android:layout_height="0dp">
</android.support.v4.view.ViewPager>
</LinearLayout>

b、子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="@drawable/a1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

2、主Java文件
package lpc.com.animation2demo;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
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;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{
private TextView tv1; //文本对象1
private TextView tv2; //文本对象2
private TextView tv3; //文本对象3
private ImageView iv; //图片对象(用于移动条)
private ViewPager vp; //Viewpager对象
private ArrayList<View> viewArrayList; //创建一个View储存数组
private int offset = 0; //移动条图片的偏移量
private int currentIndex = 0; //当前页面
private int bmpWidth ; //图片宽度
private int one = 0; //移动条滑动一页的距离
private int two = 0; //滑动条移动两页的距离
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
iv = (ImageView) findViewById(R.id.iv);
vp = (ViewPager) findViewById(R.id.viewPager1);
//下划线动画的相关设置:
bmpWidth = BitmapFactory.decodeResource(getResources(), R.mipmap.line).getWidth();// 获取图片宽度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// 获取分辨率宽度
offset = (screenW / 3 - bmpWidth) / 2;// 计算偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
iv.setImageMatrix(matrix);// 设置动画初始位置
//移动的距离
one = offset * 2 + bmpWidth;// 移动一页的偏移量,比如1->2,或者2->3
two = one * 2;// 移动两页的偏移量,比如1直接跳3
//往ViewPager填充View,同时设置点击事件与页面切换事件
viewArrayList = new ArrayList<View>();
LayoutInflater inflater = getLayoutInflater();
viewArrayList.add(inflater.inflate(R.layout.view1,null,false));
viewArrayList.add(inflater.inflate(R.layout.view2,null,false));
viewArrayList.add(inflater.inflate(R.layout.view3,null,false));
vp.setAdapter(new MyAdapter(viewArrayList));
vp.setCurrentItem(0); //设置ViewPager当前页,从0开始算
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
vp.addOnPageChangeListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv1:
vp.setCurrentItem(0);
break;
case R.id.tv2:
vp.setCurrentItem(1);
break;
case R.id.tv3:
vp.setCurrentItem(2);
break;
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Animation animation = null;
switch (position){
case 0:
if (currentIndex == 1){
animation = new TranslateAnimation(one,0,0,0);
}else if (currentIndex == 2){
animation = new TranslateAnimation(two,0,0,0);
}
break;
case 1:
if (currentIndex == 0){
animation = new TranslateAnimation(offset,one,0,0);
}else if (currentIndex == 2){
animation = new TranslateAnimation(two,one,0,0);
}
break;
case 2:
if (currentIndex == 0){
animation = new TranslateAnimation(offset,two,0,0);
}else if (currentIndex == 1){
animation = new TranslateAnimation(one,two,0,0);
}
break;
}
currentIndex = position;
animation.setFillAfter(true);// true表示图片停在动画结束位置
animation.setDuration(300); //设置动画时间为300毫秒
iv.startAnimation(animation);//开始动画
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
3、适配器文件
package lpc.com.animation2demo;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* Created by Administrator on 2016/3/16.
* 适配器
*/
public class MyAdapter extends PagerAdapter {
//创建一个对象数组 存储View
private ArrayList<View> viewArrayList;
//空构造函数
public MyAdapter() {
}
//初始化对象数组
public MyAdapter(ArrayList<View> viewArrayList) {
this.viewArrayList = viewArrayList;
}
//获取对象数组的大小
@Override
public int getCount() {
return viewArrayList.size();
}
//将视图转化成对象
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
//获取选定 对象数组并将其添加到viewpager中
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewArrayList.get(position));
return viewArrayList.get(position);
}
//移除选定的对象数组对象
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewArrayList.get(position));
}
}
4、manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lpc.com.animation2demo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5、实现效果

ViewPager的简单使用的更多相关文章
- ViewPager的简单用法+适配器+监听器的介绍
之前的actionbar+fragment文章中写过viewpager的简单用法,但因为是融合的文章,所以今天把viewpager提取出来写了.方便查询浏览~ 思路: 1.在布局文件中设置viewpa ...
- viewpager的简单使用,以及ValueAnimator的用法示例
之前在网上看到一篇viewpager简单使用的例子程序,主要采用了上部标签button+中间指示作用的imageview+下部viewpager的结构,点击上部标签,或者滑动viewpager,均可以 ...
- Android随笔--使用ViewPager实现简单地图片的左右滑动切换
Android中图片的左右切换随处可见,今天我也试着查阅资料试着做了一下,挺简单的一个小Demo,却也发现了一些问题,话不多说,上代码~: 使用了3个xml文件作为ViewPager的滑动page,布 ...
- ViewPager的简单例子
这个例子是按照官网上的例子写的,有点抄袭的嫌疑,但是自己单独写一下会加深自己的印象. 首先是MainAcitivity.xml: <LinearLayout xmlns:android=&quo ...
- ViewPager的简单使用说明
前提:工程中使用ViewPager,需要导入google提供的jar包(android-support-v4.jar). 要学习ViewPager的使用,建议直接看官方文档 Creating Swip ...
- Android开发之ViewPager的简单使用
ViewPager是V4包中的,如果你的编译器敲不出ViewPager,那么你就需要添加,看下面: 第一步:点击+号 第二步:选择第一个Library 第三步:添加这个包: 然后点击ok-->o ...
- Android ViewPager的简单实现
研究了两天ViewPager,看了几篇网上的帖子,但总的来说看得一头雾水,理不清头绪,偶然发现了一篇简单易懂的帖子,讲的调理比较清晰,原文链接附在文后. 在本例中使用ViewPager + Fra ...
- android学习ViewPager的简单使用
使用ViewPager需要引入android.support.v4.View.ViewPager这样的jar包,谷歌公司为解决当前版本碎片化的问题,提供的兼容的包.主要目的就是解决向下兼容问题. 1, ...
- TabLayout+ViewPager的简单使用
1. build.gradle文件中加入 compile 'com.android.support:design:22.2.0' 2.写Xml文件,注意TabLayout的三个属性 app:tab ...
随机推荐
- HEVC学习之二CTU, CU, CTB, CB, PB, TB
在H264标准中,编码层的核心是宏块,一个宏块大小为16X16,包含一个16X16的亮度块,以及对于常用的4:2:0采样格式来说还包含两个8X8的色度块.相对应的在HEVC中类似的结构为编码树单元(C ...
- 将Web站点由IIS6迁移至IIS7
最近开始着手逐步将所有的Web站点由Win2003+IIS6迁移至64位Win2008+IIS7,基本还算顺利.这里就把相关内容整理总结一下.首先自然是要安装基本运行环境,包括iis,.net fra ...
- Jsoup使用随记
这段时间工作比较空闲,在网上找资料学习的时候看到数据抓取这一块,来了兴趣 用jsoup实现数据抓取着实方便,唯一美中不足的是官方API是英文版的,对我这样英语水平不好的程序员来说着实困扰,只能一点点的 ...
- mongoDB学习笔记:了解与安装
初次使用mongoDB是在2013年,项目组为了新产品的研发,使用了mongoDB,作为项目组的一名测试员,也就跟着学起来了. 1.了解mongoDB Mongo DB ,是目前在IT行业非常流行的一 ...
- C 程序解决实际文件案例
1,C程序参数(编写带参数 的C--argc,argv[]程序),带参数的Main程序 程序功能说明: 把命令行参数中的前一个文件名标识 的文件,复制到后一个文件名标识的文件中,如只有一个则把该文件写 ...
- zTree默认勾选指定ID并执行事件
try { var arrs = $('#subjectClassID').val().split(","); var treeObj = $.fn.zTree.getZTreeO ...
- 使用spring通知时,代理出错
动态代理是基于接口的,spring配置是基于类的!!!!!!!!!! 注意:JDK的动态代理,只能对实现接口的类实现代理,生成代理对象,如果这个类没有实现接口,是生成不了代理对象的.如本例UserMa ...
- C# 软件绑定QQ群类开源放出
周天闲来无事写个公共类,可以添加到你们自己项目中限制必须加入你QQ群才可以使用. 代码简单,高手勿喷,有哪里不合理的请回帖让大家学习学习. using System; using System.Tex ...
- git和nginx安装
原始地址: https://www.zybuluo.com/freeethy/note/192109 git安装 设置git的username和email (注册gitlab的账号密码) $$ git ...
- Nginx配置文件说明
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释: #运行用户user www-data; #启动进程,通常设置成和cpu的数量相等worker_processes 1 ...