android:ViewPager实现Tabs滑动切换效果

摘要:
//架构。安卓com/apk/res.com上。LoveBus“android:layout_weight=”1.0“android:text=”私人消息“android:textColor=”#000000“android.textSize=”18.0dip“/&gt:layout_height=“wrap_content”android:

进入ViewPager官方文档

public class ViewPager extends ViewGroup
java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup
     ↳android.support.v4.view.ViewPager

ViewPager是google SDk中附加包的一个类,可以用来实现屏幕间的切换,类似于Action Bar。

从上面可以看出是android-support-v4.jar包

效果如下:

android:ViewPager实现Tabs滑动切换效果第1张

具体的一个很好的应用就是新浪博客客户端:

android:ViewPager实现Tabs滑动切换效果第2张

下面我们就来做一个这种效果

准备工作:从网上找android-support-v4.jar并导入到工程。(Properties -> java build path -> Libraries -> add XXX)

第一步 xml界面定义 

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android: 
        android:layout_width="fill_parent"
        android:layout_height="40.0dip"
        android:background="#FFFFFF" >

        <TextView
            android: 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="\@我"
            android:textColor="#000000"
            android:textSize="18.0dip" />

        <TextView
            android: 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="评论"
            android:textColor="#000000"
            android:textSize="18.0dip" />

        <TextView
            android: 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="私信"
            android:textColor="#000000"
            android:textSize="18.0dip" />
    </LinearLayout>

    <ImageView
        android: 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:background="#FFFFFF"
        android:src="http://t.zoukankan.com/@drawable/a" />

    <android.support.v4.view.ViewPager
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#000000"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>

comment.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="#d3d7d4"
    android:orientation="vertical" >



    <TextView
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="comment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 message.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="#d3d7d4"
    android:orientation="vertical" >

    <TextView
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="message"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

reference.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="#d3d7d4"
    android:orientation="vertical" >

    <TextView
        android: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="\@me"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 接下来就是写对应的activity

MainActivity.java

public class MessageHomeActivity extends Activity{
	// ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。
	// android-support-v4.jar
	private ViewPager mPager;//页卡内容
	private List<View> listViews; // Tab页面列表
	private ImageView cursor;// 动画图片
	private TextView t1, t2, t3;// 页卡头标
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		InitImageView();
		InitTextView();
		InitViewPager();
	}

	/**
	 * 初始化头标
	 */
	private void InitTextView() {
		t1 = (TextView) findViewById(R.id.text1);
		t2 = (TextView) findViewById(R.id.text2);
		t3 = (TextView) findViewById(R.id.text3);

		t1.setOnClickListener(new MyOnClickListener(0));
		t2.setOnClickListener(new MyOnClickListener(1));
		t3.setOnClickListener(new MyOnClickListener(2));
	}

	/**
	 * 初始化ViewPager
	 */
	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
		listViews = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();
		listViews.add(mInflater.inflate(R.layout.reference, null));
		listViews.add(mInflater.inflate(R.layout.comment, null));
		listViews.add(mInflater.inflate(R.layout.message, null));
		mPager.setAdapter(new MyPagerAdapter(listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/**
	 * 初始化动画
	 */
	private void InitImageView() {
		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
				.getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	/**
	 * 头标点击监听
	 */
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		int two = one * 2;// 页卡1 -> 页卡3 偏移量

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(one, 0, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, 0, 0, 0);
				}
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, one, 0, 0);
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(two, one, 0, 0);
				}
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(offset, two, 0, 0);
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(one, two, 0, 0);
				}
				break;
			}
			currIndex = 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) {
		}
	}
}

 

资源:

android:ViewPager实现Tabs滑动切换效果第3张

效果图:

android:ViewPager实现Tabs滑动切换效果第4张

android:ViewPager实现Tabs滑动切换效果第5张

android:ViewPager实现Tabs滑动切换效果第6张

参考:http://developer.android.com/reference/android/support/v4/view/ViewPager.html

        http://lib.open-open.com/view/1330341188375

来源:http://www.cnblogs.com/dwinter/archive/2012/02/27/2369590.html

免责声明:文章转载自《android:ViewPager实现Tabs滑动切换效果》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇机器学习算法中的评价指标(准确率、召回率、F值、ROC、AUC等)深入理解JavaScript闭包下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Perl基础速成

本文是针对没有Perl基础,但想用perl一行式命令取代grep/awk/sed的人,用于速学Perl基础知识。 Perl一行式系列文章:Perl一行式程序 perl的-e选项 perl命令的-e选项后可以书写表达式,例如: perl -e 'print "hello world "' Perl中的函数调用经常可以省略括号,所以print "hello...

坐标转换,这次是反过来,屏幕坐标转换成世界坐标

今天是做 安卓屏幕滑动交互的时候,发现原来安卓屏幕的坐标是屏幕坐标  坐标为 屏幕左下角0,0  右上角  1920*1080  是以像素为单位的。 那么每个手机都不一样 所以如果以这个坐标来判断是左手滑动屏幕还是右手滑动屏幕会产生问题。 而我这款游戏 摄像机是不动的  摄像机一直对着前方 摄像机的中心就是世界坐标的圆心 , 那么 我把屏幕坐标换成世界坐标...

Appium(二)---启动App+模拟滑动

环境搭建好了,就可以实现基本的操作,比如启动App和模拟滑动。这里我实现的是在真机(乐视1s)上启动抖音App,并滑动抖音的视频列表,代码如下: from appium importwebdriver from time importsleep classAction(): def __init__(self): #初始化配置,这里设置的...

Duplicate 复制数据库 搭建Dataguard

1 操作系统环境   此处隐藏具体信息 System IP-address db_name db_version Comment         Target DB         Auxiliary DB 2 复制数据库前的准备工作   2.1 standby 端安装数据库软件 2.2 primary 与stand...

spring自动识别数据库并切换数据源

一、开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能。所以在出来数据库方言的时候基本上没有什么问题,但唯一可能出现问题的就是在hibernate做添加操作生成主键策略的时候。因为我们都知道hibernate的数据库本地方言会针对不同的数据...

C#中使用Surfer

做地理信息或者绘制等值线,都会选择Surfer这个软件。这个软件对我们的作用有两个(1)插值(2)绘图。 软件:Windows 7 x64,Microsoft Visual Studio 2012 RC,Surfer 10 一、添加引用,Surfer在COM组件中。 然后在代码中加入 using Surfer; 二、创建一组原始数据文件命名为data.d...