安卓之ViewPager详解_ViewPager怎么用_ViewPager仿微博特效

首先,展示一下ViewPager是什么样子的,用过新浪微博客户端的应该对下面的画面很熟悉,(画面不是很美观,主要就是那么个意思,将就着看吧….)下面那个允许你来回滑动显示不同页面的区域就是一个ViewPager,在这里就不解释了.布局文件如下:activity_weibo.xml

大家好,又见面了,我是全栈君。

首先,展示一下ViewPager是什么样子的,用过新浪微博客户端的应该对下面的画面很熟悉,(画面不是很美观,主要就是那么个意思,将就着看吧….)下面那个允许你来回滑动显示不同页面的区域就是一个ViewPager,在这里就不解释了.

安卓之ViewPager详解_ViewPager怎么用_ViewPager仿微博特效安卓之ViewPager详解_ViewPager怎么用_ViewPager仿微博特效

布局文件如下:

activity_weibo.xml

<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">
	<LinearLayout 
	    android:id="@+id/ll"
	    android:layout_width="fill_parent"
	    android:layout_height="40dp"
	    android:background="#ffffff">
	    <TextView 
	        android:id="@+id/tt1"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
	        android:text="@string/tt1"
	        android:gravity="center"
	        android:textColor="#000000"
	        android:textSize="20dp"/>
	    <TextView 
	        android:id="@+id/tt2"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
	        android:text="@string/tt2"
	        android:textSize="20dp"
	        android:textColor="#000000"/>
	    <TextView android:id="@+id/tt3"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:layout_weight="1"
	        android:text="@string/tt3"
	        android:textSize="20dp"
	        android:textColor="#000000"/>
	</LinearLayout>
	<ImageView
	    android:id="@+id/cursor"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:scaleType="matrix"
	    android:src="@drawable/image"/>
   <android.support.v4.view.ViewPager
       android:id="@+id/vPager"
       android:layout_width="wrap_content"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:layout_gravity="center"
       android:background="#000000"
       android:flipInterval="30"
       android:persistentDrawingCache="animation"
       />
</LinearLayout>

lay1.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:orientation="vertical" >
    <View 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#66FF33"/>

</LinearLayout>

lay2.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:orientation="vertical" >
    <View 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF3333"/>

</LinearLayout>

lay3.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:orientation="vertical" >
    <View 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#3300FF"/>

</LinearLayout>

 

下面是java代码:

package jason.viewpagerdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WeiboActivity extends Activity {
	
	private ViewPager viewPager;//可滑动的页卡内容,也就是我们主要练习的目标
	private ImageView imageView;//类似游标的动画图片,这个就是那个左右滑动的小滑块
	private TextView textView1,textView2,textView3;
	private List<View> views;//页面列表
	private int offset = 0;//游标移动的偏移量
	private int currentIndex = 0;//当前页面号码
	private int bmpW;//游标宽度
	private View view1,view2,view3;//各个页面卡片
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weibo);
		InitImageView();
		InitTextView();
		InitViewPager();
	}

	private void InitViewPager() {
		// TODO Auto-generated method stub
		viewPager = (ViewPager) findViewById(R.id.vPager);
		views = new ArrayList<View>();
		LayoutInflater inflater = getLayoutInflater();//这个在我前面写的几篇里面有介绍Inflater
		view1 = inflater.inflate(R.layout.lay1, null);
		view2 = inflater.inflate(R.layout.lay2, null);
		view3 = inflater.inflate(R.layout.lay3, null);
		views.add(view1);
		views.add(view2);
		views.add(view3);
		viewPager.setAdapter(new MyViewPagerAdapter(views));//ViewPager跟ListView一样,也需要一个适配器,后面对PagerAdapter进行重写
		viewPager.setCurrentItem(0);//默认显示第一个卡片页
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());//给ViewPager加监听器
	}

	private void InitTextView() {
		textView1 = (TextView) findViewById(R.id.tt1);
		textView2 = (TextView) findViewById(R.id.tt2);
		textView3 = (TextView) findViewById(R.id.tt3);
		textView1.setOnClickListener(new MyOnClickLis(0));
		textView2.setOnClickListener(new MyOnClickLis(1));
		textView3.setOnClickListener(new MyOnClickLis(2));//这些监听器保证在点击头部的标签时候页面也能滑动
	}

	private void InitImageView() {
		imageView = (ImageView) findViewById(R.id.cursor);
		bmpW = imageView.getWidth();
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		int screen = displayMetrics.widthPixels;
		offset = (screen - bmpW *3)/6;//这个地方就是给每个标签左右各留出一块offset,共3个标签,6个offset,
		Matrix matrix = new Matrix();//后面我会再写一篇简要介绍Matrix的文章
		matrix.postTranslate(offset, 0);这个就是把游标放在了第一个标签下面
		imageView.setImageMatrix(matrix);
	}
	
	private class MyOnClickLis implements OnClickListener{
		private int index;
		
		public MyOnClickLis(int index) {//注意,这里的监听器有一个默认的带参数的构造器,用来确定你点击的是哪一个标签
			this.index = index;
		}

		@Override
		public void onClick(View v) {
			viewPager.setCurrentItem(index);
		}
		
	}
	//下面是比较重点的了,就是之前提到过的重写PagerAdapter,重写时候我们至少需要重写一下4个方法,当然这里我们也只写了这4个方法.
	public class MyViewPagerAdapter extends PagerAdapter{
		private List<View> mListView;
		public MyViewPagerAdapter(List<View> views) {
			// TODO Auto-generated constructor stub
			this.mListView = views;
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			container.removeView(mListView.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mListView.get(position));
			return mListView.get(position);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mListView.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
	}
	
	public class MyOnPageChangeListener implements OnPageChangeListener{
		int one = offset *2 + bmpW;//卡片1 --> 卡片2 偏移量
		int two = one * 2;//卡片2 --> 卡片3 偏移量
		@Override
		public void onPageScrollStateChanged(int arg0) {
			//arg0 ==1的时候表示正在滑动,arg0==2的时候表示滑动完毕了,arg0==0的时候表示什么都没做,就是停在那。
		}			

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		     //表示在前一个页面滑动到后一个页面的时候,在前一个页面滑动前调用的方法 
		}

		@Override
		public void onPageSelected(int arg0) {//arg0是表示你当前选中的页面
			Animation animation = new TranslateAnimation(one*currentIndex, one * arg0, 0, 0);
			currentIndex = arg0;
			animation.setFillAfter(true);//true:图片停留在动画结束的位置
			animation.setDuration(300);
			imageView.startAnimation(animation);
			Toast.makeText(WeiboActivity.this, "卡片移向了第" + arg0 + "页", Toast.LENGTH_SHORT).show();
		}
		
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121463.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • windows 设置脚本IP

    windows 设置脚本IP

  • YUI中js的继承示例

    YUI中js的继承示例无标题文档一个简单的例子。在这个例子中,可以看到,用var定义的私有变量,是不能被继承的。所有能被继承的,一定是通过this关键字,在内在地址中和这个对象的地址捆在一起的变量。因为复合对象传的不是值

  • Python open函数打开文件路径「建议收藏」

    Python open函数打开文件路径「建议收藏」要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符,标示符’r’表示读。 &gt;&gt;&gt;f=open(‘D:/test.txt’,’r’) 注意了,对初学python的同学而言,open()函数着实存在一个不大不小的坑,而且十分不容易发现。错误演示:   &gt;&gt;&gt;f=open(‘…

  • HDU1285 确定比赛名次

    HDU1285 确定比赛名次

  • xshell连接虚拟机步骤_安装虚拟机后如何使用

    xshell连接虚拟机步骤_安装虚拟机后如何使用Xshell连接虚拟机1、打开虚拟机终端,输入下面命令,找到ens33对应的IP地址,如图ifconfig2、打开Xshell,打开文件–>新建3、填写名称,主机这里填入刚才的IP,点击连接4、输入登录的用户名,点击确定5、输入密码,点击确定6、连接成功,可以开始使用…

  • 什么是devops思想在运维方面的具体实践_devops四个维度

    什么是devops思想在运维方面的具体实践_devops四个维度DevOps是最近非常火的一个概念,谈IT流程建设不说点DevOps都不好意思和人打招呼。但是DevOps究竟是个什么东西,这个东西能不能用?怎么用?什么样的情况才叫做DevOps落地成功?对于这些问题的答案,虽然网上有铺天盖地的文章和教程,但是一般来说都是从理论或者方法论上去阐述,也有大厂的实施经历。个人就感觉这里的它山之石,很难攻玉了。最终还是得思考下DevOps的由来,综合自己所在企业的现实…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号