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,?0,?0,0);??
  41. ????????????????????}?else?if?(currentItem?==?2)?{??
  42. ????????????????????????animation?=?new?TranslateAnimation(offSet?*?4?+?2?*?bmWidth,?0,0,?0);??
  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,?0,?0);??
  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.pswp.cn/news/376327.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376327.shtml
英文地址,請注明出處:http://en.pswp.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…