安卓之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();
		}
		
	}
}

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • php vox转码,php base64 编码图片,音频,视频

    php vox转码,php base64 编码图片,音频,视频发表于2018-10-2611:38:02by月小升header(“Content-Type:text/html;charset=UTF-8”);$file=’h5/usa.mp3′;$mp3=Base64Mp3($file);$img=Base64EncodeImage(“h5/glasses.jpg”);functionBase64Mp3($file){$fp=fo…

    2022年5月3日
    41
  • getmethods和getdeclaredmethods_java中的method

    getmethods和getdeclaredmethods_java中的methodMethodgetDeclaredMethod(Stringname,Class…parameterTypes)d返回一个Method对象,该对象反映此Class对象所表示的类或接口的指定已声明方法。Method[]getDeclaredMethods()返回Method对象的一个数组,这些对象反映此Class对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访…

    2022年9月23日
    0
  • opengrok使用教程之单工程(上)

    opengrok使用教程之单工程(上)OpenGrok简介有些情况下,我们需要在GB级别甚至几十GB级别且随时不断更新的大型代码(比如Android源码)里面搜索阅读源码,那么我们自然而然会有以下一些基本的需求:能够快速搜索代码代码可存放于本地/服务器代码之间可以跳转跨平台易于维护…显然SourceInsight、ctags、grep等工具在这些场景下就不太适合,于是有了OpenGrok这类代码搜索引擎。先看看维…

    2022年5月29日
    101
  • struts2 核心控制器:FilterDispatcher (写的真心清晰)

    struts2 核心控制器:FilterDispatcher (写的真心清晰)原文:http://mopishv0.blog.163.com/blog/static/54455932200981295843192/ 1.    在 struts1.x 系列中 , 所有的请求是通过一个 servlet(ActionServlet) 来管理控制的 , 在 Struts2.X 而是经过一个Filter 来处理请求的。 Struts2 将核心控制器设计成 Filt

    2022年8月16日
    5
  • JS跳转代码_javascript跳转代码

    JS跳转代码_javascript跳转代码<scriptlanguage=”javascript”type=”text/javascript”>functionrequest(paras){varurl=location.href;varparaString=url.substring(url.indexOf(“?”)+1,url.length).split(“&”);varparaObj={}for(i=0;j=paraString[i];i++){paraObj[j.substrin

    2022年8月13日
    5
  • int8 int16 int32 int64 float16 float32

    int8 int16 int32 int64 float16 float32int8取值范围是-128-127Int16意思是16位整数(16bitinteger),相当于short占2个字节-32768~32767Int32意思是32位整数(32bitinteger),相当于int占4个字节-2147483648~2147483647Int64意思是64位整数(64bitinterger),相当于longlong占8个字节-9223372036854775808~92233720368547

    2022年8月15日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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