在为ViewFlipper视图切换增加动画和Android中实现视图随手势移动中实现了视图随手势切换,现在Android中Compatibility Package提供了ViewPager可以更简便的实现视图切换,实现的效果如下:

效果和ViewGroup一样,但是实现过程更简单.新版的Android Market和Google+都是用了ViewPager.

说一下实现过程:

工程目录如下:

MyPagerActivity的onCreate方法如下:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initPageContent();
awesomeAdapter = new MyPagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
}

其中main.xml布局文件引入了ViewPager:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#a4c639">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/awesomepager"/>
</LinearLayout>

MyPagerAdapter继承了PagerAdapter:

private class MyPagerAdapter extends PagerAdapter{

@Override
public int getCount() {
return imageS.length;
}

/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container The containing View in which the page will be shown.
* @param position The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
@Override
public Object instantiateItem(View collection, int position) {

View view = getLayoutInflater().inflate(R.layout.page,null);
ImageView imageView =(ImageView) view.findViewById(R.id.imageId);
imageView.setImageDrawable(imageS[position]);
((ViewPager) collection).addView(view,0);
return view;
}

/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}

/**
* Called when the a change in the shown pages has been completed. At this
* point you must ensure that all of the pages have actually been added or
* removed from the container as appropriate.
* @param container The containing View which is displaying this adapter’s
* page views.
*/
@Override
public void finishUpdate(View arg0) {}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(View arg0) {}
}

其中红色代码部分负责加载Layout和想layout中填充View.这样就实现了视图的随手势切换.

源代码见:http://bigcateasymorse.googlecode.com/svn/trunk/android-viewpager0.1/

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
  3. python list.sort()根据多个关键字排序的方法实现
  4. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  5. Android(安卓)Activity界面切换添加动画特效
  6. android EditText设置不可写
  7. Android(安卓)拨号器的简单实现
  8. android实现字体闪烁动画的方法
  9. Android中不同应用间实现SharedPreferences数据共享

随机推荐

  1. android ViewFlipper的使用
  2. 实现一个Android输入法
  3. Android构建面试知识
  4. Android(安卓)中自定义控件和属性(attr.x
  5. Android(安卓)UI目录
  6. 博文视点大讲堂37期 ——It's Android Ti
  7. Android上的单元测试shell
  8. 【译】Google官方推出的Android架构组件
  9. Android双机(网络和USB)调试及其完美ROOT
  10. 如何成为一名Android架构师,乃至高级架构