Android中利用ViewPager實現視圖切換

來源:互聯網
上載者:User

在為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/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.