ViewPager的使用方法和实现过程

看图先:

        

页面中填充内容是随机关键词飞入和飞出动画效果,随后会更新,现在请先无视吧

 

首先是 导入jar包   下载地址: android-support-v4.jar

 

 

布局文件里添加viewPager布局

 

[html] view plaincopyprint?
  1. <android.support.v4.view.ViewPager  
  2.     android:id="@+id/search_viewpager"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_gravity="center" >  
  6. </android.support.v4.view.ViewPager>  

再创建两个item布局用于填充在ViewPager里

 

然后就是Activity了,主要写了左右滑动切换页面,还有一个小图片随页面切换 位移的动画效果

 

[java] view plaincopyprint?
  1. public class SearchAllcityActivity extends Activity {  
  2.   
  3.     private KeywordsFlow keywordsFlow;  
  4.     private ViewPager viewPager;  
  5.     private ImageView imageView;  
  6.     private List<View> lists = new ArrayList<View>();  
  7.     private ViewPagerAdapter adapter;  
  8.     private Bitmap cursor;  
  9.     private int offSet;  
  10.     private int currentItem;  
  11.     private Matrix matrix = new Matrix();  
  12.     private int bmWidth;  
  13.     private Animation animation;  
  14.     private Button shuaxin_sq, shuaxin_fl;  
  15.   
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.search_allcity);  
  19.   
  20.         // 随页面滑动图片   
  21.         imageView = (ImageView) findViewById(R.id.viewpaget_img);  
  22.         // 热门商圈和热门分类 页面添加到viewPager集合   
  23.         lists.add(getLayoutInflater().inflate(R.layout.search_hot_shangqu, null));  
  24.         lists.add(getLayoutInflater().inflate(R.layout.search_hot_fenlei, null));  
  25.         // 初始化滑动图片位置   
  26.         initeCursor();  
  27.         adapter = new ViewPagerAdapter(lists);  
  28.         viewPager = (ViewPager) findViewById(R.id.search_viewpager);  
  29.         viewPager.setAdapter(adapter);  
  30.         // ViewPager滑动监听器   
  31.         viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  
  32.               
  33.             @Override  
  34.             public void onPageSelected(int arg0) {  
  35.                 // TODO Auto-generated method stub   
  36.                 // 当滑动时,顶部的imageView是通过animation缓慢的滑动   
  37.                 switch (arg0) {  
  38.                 case 0:  
  39.                     if (currentItem == 1) {  
  40.                         animation = new TranslateAnimation(offSet * 2 + bmWidth, 00,0);  
  41.                     } else if (currentItem == 2) {  
  42.                         animation = new TranslateAnimation(offSet * 4 + 2 * bmWidth, 0,00);  
  43.                     }  
  44.                       
  45.                     break;  
  46.                 case 1:  
  47.                     if (currentItem == 0) {  
  48.                         animation = new TranslateAnimation(0, offSet * 2 + bmWidth, 0,0);  
  49.                     } else if (currentItem == 2) {  
  50.                         animation = new TranslateAnimation(4 * offSet + 2 * bmWidth,offSet * 2 + bmWidth, 00);  
  51.                     }  
  52.                       
  53.                       
  54.                     break;  
  55.                 }  
  56.                 currentItem = arg0;  
  57.                 animation.setDuration(500);  
  58.                 animation.setFillAfter(true);  
  59.                 imageView.startAnimation(animation);  
  60.   
  61.             }  
  62.               
  63.             @Override  
  64.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  65.                 // TODO Auto-generated method stub   
  66.                   
  67.             }  
  68.               
  69.             @Override  
  70.             public void onPageScrollStateChanged(int arg0) {  
  71.                 // TODO Auto-generated method stub   
  72.                   
  73.             }  
  74.         });  
  75.           
  76.     }  
  77.   
  78.       
  79.     /** 
  80.      * 计算滑动的图片的位置 
  81.      */  
  82.     private void initeCursor() {  
  83.         cursor = BitmapFactory.decodeResource(getResources(),R.drawable.viewpager_img);  
  84.         bmWidth = cursor.getWidth();  
  85.         DisplayMetrics dm;  
  86.         dm = getResources().getDisplayMetrics();  
  87.         offSet = (dm.widthPixels - 2 * bmWidth) / 4;  
  88.         matrix.setTranslate(offSet, 0);  
  89.         imageView.setImageMatrix(matrix); // 需要iamgeView的scaleType为matrix   
  90.         currentItem = 0;  
  91.     }  
  92.   
  93. }  
public class SearchAllcityActivity extends Activity {private KeywordsFlow keywordsFlow;private ViewPager viewPager;private ImageView imageView;private List<View> lists = new ArrayList<View>();private ViewPagerAdapter adapter;private Bitmap cursor;private int offSet;private int currentItem;private Matrix matrix = new Matrix();private int bmWidth;private Animation animation;private Button shuaxin_sq, shuaxin_fl;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.search_allcity);// 随页面滑动图片imageView = (ImageView) findViewById(R.id.viewpaget_img);// 热门商圈和热门分类 页面添加到viewPager集合lists.add(getLayoutInflater().inflate(R.layout.search_hot_shangqu, null));lists.add(getLayoutInflater().inflate(R.layout.search_hot_fenlei, null));// 初始化滑动图片位置initeCursor();adapter = new ViewPagerAdapter(lists);viewPager = (ViewPager) findViewById(R.id.search_viewpager);viewPager.setAdapter(adapter);// ViewPager滑动监听器viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageSelected(int arg0) {// TODO Auto-generated method stub// 当滑动时,顶部的imageView是通过animation缓慢的滑动switch (arg0) {case 0:if (currentItem == 1) {animation = new TranslateAnimation(offSet * 2 + bmWidth, 0, 0,0);} else if (currentItem == 2) {animation = new TranslateAnimation(offSet * 4 + 2 * bmWidth, 0,0, 0);}break;case 1:if (currentItem == 0) {animation = new TranslateAnimation(0, offSet * 2 + bmWidth, 0,0);} else if (currentItem == 2) {animation = new TranslateAnimation(4 * offSet + 2 * bmWidth,offSet * 2 + bmWidth, 0, 0);}break;}currentItem = arg0;animation.setDuration(500);animation.setFillAfter(true);imageView.startAnimation(animation);}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub}});}/*** 计算滑动的图片的位置*/private void initeCursor() {cursor = BitmapFactory.decodeResource(getResources(),R.drawable.viewpager_img);bmWidth = cursor.getWidth();DisplayMetrics dm;dm = getResources().getDisplayMetrics();offSet = (dm.widthPixels - 2 * bmWidth) / 4;matrix.setTranslate(offSet, 0);imageView.setImageMatrix(matrix); // 需要iamgeView的scaleType为matrixcurrentItem = 0;}}

 

最后,不能忘了ViewPager的Adapter

 

 

[java] view plaincopyprint?
  1. public class ViewPagerAdapter extends PagerAdapter{  
  2.   
  3.     List<View> viewLists;  
  4.       
  5.     public ViewPagerAdapter(List<View> lists)  
  6.     {  
  7.         viewLists = lists;  
  8.     }  
  9.   
  10.     @Override  
  11.     public int getCount() {                                                                 //获得size   
  12.         // TODO Auto-generated method stub   
  13.         return viewLists.size();  
  14.     }  
  15.   
  16.     @Override  
  17.     public boolean isViewFromObject(View arg0, Object arg1) {                           
  18.         // TODO Auto-generated method stub   
  19.         return arg0 == arg1;  
  20.     }  
  21.       
  22.     @Override  
  23.     public void destroyItem(View view, int position, Object object)                       //销毁Item   
  24.     {  
  25.         ((ViewPager) view).removeView(viewLists.get(position));  
  26.     }  
  27.       
  28.     @Override  
  29.     public Object instantiateItem(View view, int position)                                //实例化Item   
  30.     {  
  31.         ((ViewPager) view).addView(viewLists.get(position), 0);  
  32.           
  33.         return viewLists.get(position);  
  34.     }  
  35.       
  36. }  

转载于:https://www.cnblogs.com/Free-Thinker/p/3391459.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376327.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何通过浏览器在所有响应内容中查找文本

使用浏览器的开发者工具查找响应文件的内容 ** Chrome ** 版本&#xff1a; 快捷键&#xff1a;CtrlShiftF 可以看到已经查找出来了 ** firefox ** 版本

【Leetcode】【Easy】Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解题&#xff1a; 本题为典型的KMP算法考察题&#xff0c;KMP算法描述为&#xff1a; 设主串S&#xff0c;匹配串P&#xff0c;i为S的索引下…

Android Animations动画使用详解

一、动画类型 Android的animation由四种类型组成&#xff1a;alpha、scale、translate、rotate XML配置文件中 alpha渐变透明度动画效果scale渐变尺寸伸缩动画效果translate画面转换位置移动动画效果rotate画面转移旋转动画效果Java Code代码中 AlphaAnimation渐变透明度动画效…

Jenkins入门指南

新手学习使用Jenkins 安装好Jenkins后如何运行脚本 1.新建item 2.输入任务名称&#xff0c;选择项目类型&#xff0c;点击确定 3.填个描述就好了&#xff0c;新手学jenkins&#xff0c;其他都不看&#xff0c;跑起来再说 4.点这个高级&#xff0c;选择你要运行的脚本所在…

Sublime Text 3 史上最性感的编辑器

↑ ↑ ↑ ↑ ↑ 请看文件夹 ↑ ↑ ↑ ↑ ↑ 下载 / 安装 windows / MAC OS 官网下载&#xff0c;双击安装&#xff0c;这个都会吧&#xff5e; linux linux下安装&#xff0c;一种办法是从官网下载 tar.bz &#xff0c;手动安装。 这里介绍用 apt-get 自己主动安装方法&#xf…

[转]怎么查看和修改 MySQL 的最大连接数?

使用 MySQL 数据库的站点&#xff0c;当访问连接数过多时&#xff0c;就会出现 "Too many connections" 的错误。出现这种错误有两种情况&#xff0c;一种是网站访问量实在太大&#xff0c;服务器已经负担不起&#xff0c;此时就应该考虑负载均衡或者其它减少服务器压…

对qps、tps、pv、uv的理解

QPS &#xff08;Queries Per Second&#xff09;&#xff1a;每秒查询数&#xff08;个别地方叫每秒查询率&#xff1f;每秒查询率是个奇怪的东西&#xff0c;每小时时速&#xff1f;&#xff09;&#xff0c;表示系统在一秒内处理的查询次数。 TPS&#xff08;Transactions …

swift入门之TableView

IOS8更新了&#xff0c;oc还将继续但新增了swift语言&#xff0c;能够代替oc编写ios应用&#xff0c;本文将使用swift作为编写语言&#xff0c;为大家提供step by step的教程。 工具 ios每次更新都须要更新xcode&#xff0c;这次也不例外&#xff0c;但使用xcode6&#xff0c;须…

Training-ActionBar

阅读&#xff1a;http://developer.android.com/training/basics/actionbar/index.html 对于API11以下的兼容&#xff1a; Update your activity so that it extends ActionBarActivity. For example: public class Main Activit yextends ActionBarActivity{...} In your mani…

Jmeter BeanShell学习(一) - BeanShell取样器(一)

通过利用BeanShell取样器设置请求发送的参数。 第一步&#xff1a;添加BeanShell取样器 第二步&#xff1a;在BeanShell中输入执行的代码 log.info("脚本开始执行"); //意思是将字符串输出到日志消息中 vars.put("username","123163.com");//…

【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型...

原文网址&#xff1a;http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用来说明脚本语言是python的 是要用/usr/bin下面的程序&#xff08;工具&#xff09;python&#xff0c;这个解释器&#xff0c;来解释python脚本&#…

分布式系统介绍-PNUTS

PNUTS是Yahoo!的分布式数据库系统&#xff0c;支持地域上分布的大规模并发操作。它根据主键的范围区间或者其哈希值的范围区间将表拆分为表单元&#xff08;Tablet&#xff09;&#xff0c;多个表单元存储在一个服务器上。一个表单元控制器根据服务器的负载情况&#xff0c;进行…

Jmeter BeanShell学习(一) - BeanShell取样器(二)

利用BeanShell取样器获取接口返回的JSON格式的结果&#xff0c;并将该结果写入到文件。 第一步&#xff1a;添加BeanShell取样器 前面几个取样器的内容查看&#xff1a; https://blog.csdn.net/goodnameused/article/details/96985514 第二步&#xff1a;查看返回的结果格式 …

在数据库中outlet、code、outline为联合组件。hibarnate插入可如此插入

hibarnate对象的映射文件如下 <id name"outlet" type"string"> <column name"OUTLET" length"10" /> <generator class"assigned" /> </id> <!-- <property name"code" type"…

日怎么没人告诉我这博客可以改博文界面的显示宽度的

于是我妥妥的回归了。 weebly虽然定制功能强大&#xff0c;还能穿越时空发博文&#xff0c;但是太麻烦了&#xff0c;而且用着也不像一个博客。 既然解决了这个问题&#xff0c;那Lofter除了行间距也没什么缺点了&#xff0c;接着用吧&#xff0c;反正weebly也传不了大图&#…

160 - 50 DueList.5

环境&#xff1a; Windows xp sp3 工具&#xff1a; Ollydbg exeinfope 0x00 查壳 可以看出程序有加壳&#xff0c;那么我们下一步就是脱壳了。 0x01 脱壳 看上去没什么特别的地方&#xff0c;就直接 单步跟踪法 来脱壳吧 近call F7&#xff0c;远call F8 来到这里 哈&…

firefox浏览器中silverlight无法输入问题

firefox浏览器中silverlight无法输入问题今天用firefox浏览silverlight网页&#xff0c;想在文本框中输入内容&#xff0c;却没想到silverlight插件意外崩溃了。google一下&#xff0c;发现这是firefox的设置问题&#xff0c;解决方法如下&#xff1a; 1、在Firefox浏览器地址栏…

关键路径的概念和算法

AOE网&#xff1a;在一个表示工程的带权有向图中&#xff0c;用顶点表示事件&#xff0c;用有向边表示活动&#xff0c;边上的权值表示活动的持续时间&#xff0c;称这样的有向图叫做边表示活动的网&#xff0c;简称AOE网。AOE网中没有入边的顶点称为始点&#xff08;或源点&am…

160 - 51 DueList.6

环境&#xff1a; Windows xp sp3 工具&#xff1a; Ollydbg exeinfope 0x00 查壳 发现程序没有加壳&#xff0c;那么我们可以直接分析了。 0x01 分析 运行程序看一看 看到错误信息的字符串后我们可以直接搜索了。 可以看到程序会比较输入的长度是否为8位&#xff0c;如…

宽带上行速率和下行速率的区别

本文由广州宽带网http://www.ymeibai.com/整理发布&#xff0c;广州电信宽带报装&#xff0c;上广州宽带网。 我们一般所说的4M宽带&#xff0c;6M宽带&#xff0c;都是指宽带的下行速率&#xff0c;可以理解为就是下载的速度&#xff0c;平时我们用迅雷、或者网页下载软件时&a…