ViewPager与PagerAdapter

时间:2023-03-08 16:34:20

ViewPager是一个可以用来滑动内部View的组件,他有一个老搭档PagerAdapter,我们这次就来看看他们这两位拍档的本事。

我们要使用ViewPager与PagerAdapter结合


首先布局文件

<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

然后实现自己的adapter继承PagerAdapter。实现PagerAdapter的方法

1、public int getCount();  //获取总View的数量

2、public void startUpdate(ViewGroup container)  ;
3、public Object instantiateItem(ViewGroup container, int position)  ; //实例化当前的View,添加到contrainer中,然后返回生成的View或关联的Object (用于在isViewFromObject中检查)

4、public void destroyItem(ViewGroup container, int position, Object object);  //从container中移除指定的View (释放内存)
5、public void finishUpdate(ViewGroup container)  ;

6、 public boolean isViewFromObject(View view, Object object) ;// 检查View是否与一个Object关联,

下面展示一下demo

package com.example.communityfunction.myView;

import java.util.List;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView; public class ImgViewPagerAdapter extends PagerAdapter
{
private List<Bitmap> imgBm;
private Context mContext;
private ViewPager pager; public ImgViewPagerAdapter(Context context,List<Bitmap>imgBm,ViewPager pager)
{
this.imgBm=imgBm;
this.mContext=context;
this.pager=pager;
}
@Override
public int getCount()
{
return imgBm.size();
}
@Override
public Object instantiateItem(final ViewGroup container, int position)
{
ImageView imageView=new ImageView(mContext);
imageView.setImageBitmap(imgBm.get(position));
container.addView(imageView,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
imageView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
startAnim();
}
});
return imageView;
}
   // PagerAdapter只缓存三张要显示的图片,如果滑动的图片超出了缓存的范围,就会调用这个方法,将图片销毁
@Override public void destroyItem(ViewGroup container, int position, Object object) 
{ container.removeView((View)object); };
@Override public boolean isViewFromObject(View arg0, Object arg1)
{ return arg0==arg1; //官方提示写法 }
/*
*动画效果
*/
public void startAnim()
{
AnimatorSet animSet = new AnimatorSet();
animSet.play(ObjectAnimator.ofFloat(pager, "alpha", 1.0f,0f))
.with(ObjectAnimator.ofFloat(pager, "scaleX",1.0f,0f))
.with(ObjectAnimator.ofFloat(pager, "scaleY",1.0f,0f));
animSet.setDuration(400);
animSet.start();
animSet.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {}
@Override
public void onAnimationRepeat(Animator animation) {}
@Override
public void onAnimationEnd(Animator animation)
{
pager.setVisibility(View.GONE);
}
@Override
public void onAnimationCancel(Animator animation){}
});
} }

注意:

在ViewPager 外面嵌套ScrollView 时,发现ViewPager中的元素显示不出来。

解决办法:
在ScrollView节点指定android:fillviewport="true"