android viewpage的使用

2024-05-16 10:48
文章标签 android 使用 viewpage

本文主要是介绍android viewpage的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


android viewpage的使用


android animation layout listview encoding matrix

在使用之前要加support.v4包哦,一般在D:\android-sdk-windows\extras\android\support\v4目录下面,好像4.0以后见工程的时候自动加载的......

下面贴代码及效果图:


mainactivity类代码如下:

[html]  view plain copy print ?
  1. package com.xy.viewpager;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.os.Bundle;  
  7. import android.os.Parcelable;  
  8. import android.support.v4.view.PagerAdapter;  
  9. import android.support.v4.view.ViewPager;  
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  11. import android.util.DisplayMetrics;  
  12. import android.util.Log;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.view.animation.Animation;  
  17. import android.view.animation.TranslateAnimation;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20. import android.widget.RelativeLayout;  
  21. import android.widget.TextView;  
  22. import android.app.Activity;  
  23. import android.app.AlertDialog;  
  24. import android.content.DialogInterface;  
  25. import android.graphics.BitmapFactory;  
  26.   
  27. public class MainActivity extends Activity {  
  28.     private ViewPager mPager;// 页卡内容  
  29.     private List<View> listViews; // Tab页面列表  
  30.     private ImageView cursor;// 动画图片  
  31.     private TextView t1, t2, t3;// 页卡头标  
  32.     private int offset = 0;// 动画图片偏移量  
  33.     private int currIndex = 0;// 当前页卡编号  
  34.     private int bmpW;// 动画图片宽度  
  35.     MyPagerAdapter adapter;  
  36.     LayoutInflater mInflater;  
  37.   
  38.     RelativeLayout rel;  
  39.   
  40.     /** Called when the activity is first created. */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.activity_main);  
  45.         Log.i("Viewpage", "--onCreate--");  
  46.         initImageView();  
  47.         initTextView();  
  48.         initPageView();  
  49.   
  50.     }  
  51.   
  52.     private void initPageView() {  
  53.         mInflater = getLayoutInflater();  
  54.         listViews = new ArrayList<View>();  
  55.         listViews.add(mInflater.inflate(R.layout.layou1, null));  
  56.         listViews.add(mInflater.inflate(R.layout.layou2, null));  
  57.         listViews.add(mInflater.inflate(R.layout.layou3, null));  
  58.         adapter = new MyPagerAdapter(listViews);  
  59.         mPager = (ViewPager) findViewById(R.id.page);  
  60.         mPager.setAdapter(adapter);  
  61.         mPager.setCurrentItem(0);  
  62.         mPager.setOnPageChangeListener(new MyOnPageChangeListener());  
  63.     }  
  64.   
  65.     private void initTextView() {  
  66.         t1 = (TextView) findViewById(R.id.tab1);  
  67.         t2 = (TextView) findViewById(R.id.tab2);  
  68.         t3 = (TextView) findViewById(R.id.tab3);  
  69.         t1.setOnClickListener(new MyOnClickListener(0));  
  70.         t2.setOnClickListener(new MyOnClickListener(1));  
  71.         t3.setOnClickListener(new MyOnClickListener(2));  
  72.     }  
  73.   
  74.     private void initImageView() {  
  75.         cursor = (ImageView) findViewById(R.id.cursor);  
  76.         rel = (RelativeLayout) findViewById(R.id.layout);  
  77.           
  78.   
  79.         bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.png)  
  80.                 .getWidth();  
  81.         DisplayMetrics dm = new DisplayMetrics();  
  82.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  83.         int screenW = dm.widthPixels;  
  84.         offset = (screenW / 3 - bmpW) / 2;  
  85.         // Matrix matrix = new Matrix();  
  86.         // matrix.postTranslate(offset, 0);  
  87.         cursor.setBackgroundResource(R.drawable.png);  
  88.         // cursor.setScaleType(ScaleType.MATRIX);  
  89.         // cursor.setImageMatrix(matrix);  
  90.         rel.setPadding(offset, 0, 0, 0);  
  91.   
  92.     }  
  93.   
  94.     public class MyOnClickListener implements View.OnClickListener {  
  95.         private int index = 0;  
  96.   
  97.         public MyOnClickListener(int i) {  
  98.             index = i;  
  99.         }  
  100.   
  101.         @Override  
  102.         public void onClick(View v) {  
  103.             // TODO Auto-generated method stub  
  104.             mPager.setCurrentItem(index);  
  105.         }  
  106.     }  
  107.   
  108.     public class MyPagerAdapter extends PagerAdapter implements OnClickListener {  
  109.         public List<View> mListViews;  
  110.         public View v1;  
  111.         public View v2;  
  112.         public View v3;  
  113.         public Button mButton;  
  114.   
  115.         public MyPagerAdapter(List<View> mListViews) {  
  116.             this.mListViews = mListViews;  
  117.             getViewClickListener(mListViews);  
  118.         }  
  119.   
  120.         public void getViewClickListener(List<View> listview) {  
  121.             v1 = listview.get(0);  
  122.             v2 = listview.get(1);  
  123.             v3 = listview.get(2);  
  124.             mButton = (Button) v1.findViewById(R.id.button);  
  125.             mButton.setOnClickListener(this);  
  126.         }  
  127.   
  128.         public void destroyItem(View arg0, int arg1, Object arg2) {  
  129.             ((ViewPager) arg0).removeView(mListViews.get(arg1));  
  130.         }  
  131.   
  132.         public void finishUpdate(View arg0) {  
  133.         }  
  134.   
  135.         @Override  
  136.         public int getCount() {  
  137.             return mListViews.size();  
  138.         }  
  139.   
  140.         @Override  
  141.         public Object instantiateItem(View arg0, int arg1) {  
  142.             ((ViewPager) arg0).addView(mListViews.get(arg1), 0);  
  143.             return mListViews.get(arg1);  
  144.         }  
  145.   
  146.         @Override  
  147.         public boolean isViewFromObject(View arg0, Object arg1) {  
  148.             return arg0 == (arg1);  
  149.         }  
  150.   
  151.         @Override  
  152.         public void restoreState(Parcelable arg0, ClassLoader arg1) {  
  153.         }  
  154.   
  155.         @Override  
  156.         public Parcelable saveState() {  
  157.             return null;  
  158.         }  
  159.   
  160.         @Override  
  161.         public void startUpdate(View arg0) {  
  162.         }  
  163.   
  164.         @Override  
  165.         public void onClick(View v) {  
  166.             AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)  
  167.                     .setIcon(null)  
  168.                     .setTitle("dialog")  
  169.                     .setMessage("nihao")  
  170.                     .setPositiveButton("确定",  
  171.                             new DialogInterface.OnClickListener() {  
  172.   
  173.                                 @Override  
  174.                                 public void onClick(DialogInterface arg0,  
  175.                                         int arg1) {  
  176.   
  177.                                     MainActivity.this.finish();  
  178.   
  179.                                 }  
  180.   
  181.                                 })  
  182.                     .setNegativeButton("取消",  
  183.                             new DialogInterface.OnClickListener() {  
  184.   
  185.                                 @Override  
  186.                                 public void onClick(DialogInterface arg0,  
  187.                                         int arg1) {  
  188.   
  189.                                 }  
  190.   
  191.                             }).create();  
  192.   
  193.             // 显示对话框也可以使用showDialog(int id)方法显示对话框  
  194.   
  195.             dialog.show();  
  196.         }  
  197.     }  
  198.   
  199.     public class MyOnPageChangeListener implements OnPageChangeListener {  
  200.   
  201.         int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量  
  202.         int two = one * 2;// 页卡1 -> 页卡3 偏移量  
  203.   
  204.         @Override  
  205.         public void onPageSelected(int arg0) {  
  206.             Animation animation = null;  
  207.             switch (arg0) {  
  208.             case 0:  
  209.                 if (currIndex == 1) {  
  210.                     animation = new TranslateAnimation(one, 0, 0, 0);  
  211.                 } else if (currIndex == 2) {  
  212.                     animation = new TranslateAnimation(two, 0, 0, 0);  
  213.                 }  
  214.                 break;  
  215.             case 1:  
  216.                 if (currIndex == 0) {  
  217.                     animation = new TranslateAnimation(offset, one, 0, 0);  
  218.                 } else if (currIndex == 2) {  
  219.                     animation = new TranslateAnimation(two, one, 0, 0);  
  220.                 }  
  221.                 break;  
  222.             case 2:  
  223.                 if (currIndex == 0) {  
  224.                     animation = new TranslateAnimation(offset, two, 0, 0);  
  225.                 } else if (currIndex == 1) {  
  226.                     animation = new TranslateAnimation(one, two, 0, 0);  
  227.                 }  
  228.                 break;  
  229.             }  
  230.             currIndex = arg0;  
  231.             animation.setFillAfter(true);// True:图片停在动画结束位置  
  232.             animation.setDuration(300);  
  233.             rel.startAnimation(animation);  
  234.         }  
  235.   
  236.         @Override  
  237.         public void onPageScrolled(int arg0, float arg1, int arg2) {  
  238.               
  239.         }  
  240.   
  241.         @Override  
  242.         public void onPageScrollStateChanged(int arg0) {  
  243.               
  244.         }  
  245.     }  
  246. }  

mainlayout.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/nav"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="50dp"  
  11.         android:background="#efefef" >  
  12.   
  13.         <TextView  
  14.             android:id="@+id/tab1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:layout_weight="1.0"  
  18.             android:gravity="center"  
  19.             android:text="页片1"  
  20.             android:textColor="#000000" />  
  21.   
  22.         <TextView  
  23.             android:id="@+id/tab2"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="fill_parent"  
  26.             android:layout_weight="1.0"  
  27.             android:gravity="center"  
  28.             android:text="页片2"  
  29.             android:textColor="#000000" />  
  30.   
  31.         <TextView  
  32.             android:id="@+id/tab3"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="fill_parent"  
  35.             android:layout_weight="1.0"  
  36.             android:gravity="center"  
  37.             android:text="页片3"  
  38.             android:textColor="#000000" />  
  39.     </LinearLayout>  
  40.   
  41.     <RelativeLayout  
  42.         android:id="@+id/layout"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content" >  
  45.   
  46.         <ImageView  
  47.             android:id="@+id/cursor"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content" />  
  50.     </RelativeLayout>  
  51.   
  52.     <android.support.v4.view.ViewPager  
  53.         android:id="@+id/page"  
  54.         android:layout_width="fill_parent"  
  55.         android:layout_height="fill_parent" >  
  56.     </android.support.v4.view.ViewPager>  
  57.   
  58. </LinearLayout>  

layout1.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#565656"  
  6. android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="点击事件" />  
  13.   
  14. </LinearLayout>  

layout2.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="fill_parent"    
  4.     android:layout_height="fill_parent"    
  5.     android:orientation="vertical"    
  6.     android:background="#abab00">    
  7. </LinearLayout>    

layout3.xml:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#00abcd"  
  6.     android:orientation="vertical" >  
  7.   
  8. </LinearLayout>  




quot;fill_parent

这篇关于android viewpage的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994695

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND