Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> ViewPager與PagerAdapter,viewpager

ViewPager與PagerAdapter,viewpager

編輯:關於android開發

ViewPager與PagerAdapter,viewpager


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;
    }
    @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){} }); } }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved