通过我们前面的学习,我们知道ViewPager是可以做出近乎完美的滑动体验,回顾整个Android,我们发现Gallery具备同样的特点,于是我们大胆地猜想,Gallery是否和ViewPager之间有些关联呢?两者在功能上的类似和重叠,总会让我们对此充满好奇,我们开始思考:如果不借助Gallery,我们能否做出像Gallery一样的效果呢?答案是肯定的。那么,我们今天就来一起学习自己动手打造一个Gallery吧。
首先讲述下原理,和前面一样,当我们继承PagerAdapter这个类时,一般会传入一个View类型的数组,这样在ViewPager绑定了这个适配器类时,就能将这个View数组里的View对象显示出来。在这里,View可以是一个布局,也可以是某个控件,甚至是任何继承自View的控件。所以,我们同样可以传入一个ImageView作为这个View对象啊。好,明白了原理,我们就来开始今天的学习吧!
首先来看布局代码,ViewPager是必不可少的一个控件。
- <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"
- android:background="#000000"
- tools:context=".MainActivity" >
- <android.support.v4.view.ViewPager
- android:id="@+id/ViewPager"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
- </LinearLayout>
接下来是那个百用不厌的ViewPagerAdapter类,继承自PagerAdapter:
- package com.Android.ViewPager4Gallery;
- import java.util.ArrayList;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.view.View;
- public class ViewPagerAdapter extends PagerAdapter {
- private ArrayList<View> Views;//存放View的ArrayList
- /*
- * ViewAdapter构造函数
- * @author:Robin
- */
- public ViewPagerAdapter(ArrayList<View> Views)
- {
- this.Views=Views;
- }
- /*
- * 返回View的个数
- */
- @Override
- public int getCount()
- {
- if(Views!=null)
- {
- return Views.size();
- }
- return 0;
- }
- /*
- * 销毁View
- */
- @Override
- public void destroyItem(View container, int position, Object object)
- {
- ((ViewPager)container).removeView(Views.get(position));
- }
- /*
- * 初始化
- */
- @Override
- public Object instantiateItem(View container, int position)
- {
- ((ViewPager)container).addView(Views.get(position),0);
- return Views.get(position);
- }
- /*
- * 判断View是否来自Object
- */
- @Override
- public boolean isViewFromObject(View view, Object object)
- {
- return (view==object);
- }
- }
然后就是核心的代码部分了,代码已经做好了注释,写得很明白,不再赘述了
- package com.Android.ViewPager4Gallery;
- import java.util.ArrayList;
- import android.os.Bundle;
- import android.app.Activity;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.Menu;
- import android.view.View;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnPageChangeListener {
- //图像资源
- private int[] Resources=new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,
- R.drawable.image5,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image10,
- R.drawable.image11,R.drawable.image13,R.drawable.image16,R.drawable.image17,R.drawable.image18,
- R.drawable.image19,R.drawable.image21,R.drawable.image22,R.drawable.image23,R.drawable.image24,
- R.drawable.image25,R.drawable.image27,R.drawable.image28,R.drawable.image29};
- //适配器
- private ViewPagerAdapter mAdapter;
- //View数组
- private ArrayList<View> Views;
- //ViewPager
- private ViewPager mViewPager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- //初始化Views
- Views=new ArrayList<View>(Resources.length);
- for(int i=0;i<Resources.length;i++)
- {
- ImageView image=new ImageView(this);
- image.setImageResource(Resources[i]);
- image.setScaleType(ImageView.ScaleType.FIT_XY);
- image.setAdjustViewBounds(true);
- Views.add(image);
- }
- //初始化ViewPager
- mViewPager=(ViewPager)findViewById(R.id.ViewPager);
- //初始化适配器
- mAdapter=new ViewPagerAdapter(Views);
- //绑定
- mViewPager.setAdapter(mAdapter);
- //事件注册
- mViewPager.setOnPageChangeListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onPageScrollStateChanged(int arg0)
- {
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2)
- {
- //Toast.makeText(this, "你滚动了ViewPager", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onPageSelected(int position)
- {
- Toast.makeText(this, "这是第"+position+"张图片", Toast.LENGTH_SHORT).show();
- }
- }
最终运行效果如下: