Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android中通過ViewPager實現左右滑屏

android中通過ViewPager實現左右滑屏

編輯:Android開發實例

一、簡介 ViewPager類提供了多界面切換的效果(類似home中workspace的切換)。該效果有如下特征: [1] 當前顯示一組界面中的其中一個界面。 [2] 當用戶通過左右滑動界面時,當前的屏幕顯示當前界面和下一個界面的一部分。 [3]滑動結束後,界面自動跳轉到當前選擇的界面中

官方的描述:

 請參考:http://developer.android.com/sdk/compatibility-library.html#Notes

下面是一個效果圖 圖1:   圖2:   二、ViewPager的下載與安裝    ViewPager來源於google 的補充組件android-support-v13.jar. 首先啟動Android SDK Manager的,然後選中Extras的“Android support package”進行下載更新。 在下載更新後,在eclipse中工程上點擊右鍵,選擇android tools -> add compatibility library即可完成安裝。 另外也可以手工通過工程屬性中的"Java Build Path"->"Libraries"->"Add External JARS..."把android-support-v13.jar導到Android工程中, jar包所在位置是\android-sdk\extras\android\compatibility\v4\android-support-v13.jar 二、使用ViewPager控件 2.1 在布局文件中引用ViewPager控件      <android.support.v4.view.ViewPager          android:id="@+id/viewPager1"             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:layout_alignLeft="@+id/textView1"             android:layout_above="@id/adLayout"             android:layout_below="@id/topText"             android:layout_centerVertical="true"  />   2.2 設置ViewPager控件的適配器    ViewPager的適配器必須繼承於PagerAdapter,並實現以下四個方法 //獲取當前窗體界面數 public int getCount()  //初始化position位置的界面 public Object instantiateItem(View collection, int position) //銷毀position位置的界面 public void destroyItem(View collection, int position, Object view) // 判斷是否由對象生成界面 public boolean isViewFromObject(View arg0, Object arg1) 2.3  初始化ViewPager控件  初始化ViewPager控件的適配器 viewPager1 = (ViewPager) findViewById(R.id.viewPager1); viewPager1.setAdapter(new ImgPagerAdapter(this,lists)); 配置適配器的頁面變化事件 viewPager1        .setOnPageChangeListener(new OnPageChangeListener() {             //頁面選擇            @Override   / /當一個頁面即將被加載時,調用此方法            public void onPageSelected(int position) {               topText.setText(String.valueOf(position+1)+"/"+String.valueOf(lists.length));            }            @Override            // 狀態有三個0空閒,1是增在滑行中,2目標加載完畢            public void onPageScrollStateChanged(int state) {            }              @Override            / /當在一個頁面滾動時,調用此方法            public void onPageScrolled(int position,                   float positionOffset, int positionOffsetPixels) {            }        });    實例1 viewpager_layout.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" android:orientation="vertical"> <!-- 此處需要給出全路徑 --> <android.support.v4.view.ViewPager     android:id="@+id/viewpagerLayout" android:layout_height="fill_parent" android:layout_width="fill_parent"/> </LinearLayout> layout1.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" android:orientation="vertical">     <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="fill_parent" android:text="第一頁"></TextView>     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1">         <requestFocus></requestFocus>     </EditText> </LinearLayout> layout2.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" android:orientation="vertical">     <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="fill_parent" android:text="第二頁"></TextView>     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1">         <requestFocus></requestFocus>     </EditText> </LinearLayout> layout3.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent" android:orientation="vertical">     <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_width="fill_parent" android:text="第三頁"></TextView>     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1">         <requestFocus></requestFocus>     </EditText> </LinearLayout> TestViewPager.java文件 package com.teleca.robin; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import com.teleca.robin.R;
public class TestViewPager extends Activity { final static String tag = "robin"; private ViewPager myViewPager; private MyPagerAdapter myAdapter;
private LayoutInflater mInflater; private List<View> mListViews; private View layout1 = null; private View layout2 = null; private View layout3 = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewpager_layout); myAdapter = new MyPagerAdapter(); myViewPager = (ViewPager) findViewById(R.id.viewpagerLayout); myViewPager.setAdapter(myAdapter); mListViews = new ArrayList<View>(); mInflater = getLayoutInflater(); layout1 = mInflater.inflate(R.layout.layout1, null); layout2 = mInflater.inflate(R.layout.layout2, null); layout3 = mInflater.inflate(R.layout.layout3, null); mListViews.add(layout1); mListViews.add(layout2); mListViews.add(layout3); // 初始化當前顯示的view myViewPager.setCurrentItem(1); // 初始化第二個view的信息 EditText v2EditText = (EditText) layout2.findViewById(R.id.editText1); v2EditText.setText("動態設置第二個view的值"); myViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { Log.d(tag, "onPageSelected - " + arg0); // activity從1到2滑動,2被加載後掉用此方法 View v = mListViews.get(arg0); EditText editText = (EditText) v.findViewById(R.id.editText1); editText.setText("動態設置#" + arg0 + "edittext控件的值"); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { Log.d(tag, "onPageScrolled - " + arg0); // 從1到2滑動,在1滑動前調用 } @Override public void onPageScrollStateChanged(int arg0) { Log.d(tag, "onPageScrollStateChanged - " + arg0); // 狀態有三個0空閒,1是增在滑行中,2目標加載完畢 /** * Indicates that the pager is in an idle, settled state. The * current page * is fully in view and no animation is in progress. */ // public static final int SCROLL_STATE_IDLE = 0; /** * Indicates that the pager is currently being dragged by the * user. */ // public static final int SCROLL_STATE_DRAGGING = 1; /** * Indicates that the pager is in the process of settling to a * final position. */ // public static final int SCROLL_STATE_SETTLING = 2; }
});
} private class MyPagerAdapter extends PagerAdapter { @Override public void destroyItem(View arg0, int arg1, Object arg2) { Log.d(tag, "destroyItem"); ((ViewPager) arg0).removeView(mListViews.get(arg1)); } @Override public void finishUpdate(View arg0) { Log.d(tag, "finishUpdate"); } @Override public int getCount() { Log.d(tag, "getCount"); return mListViews.size(); } @Override public Object instantiateItem(View arg0, int arg1) { Log.d(tag, "instantiateItem"); ((ViewPager) arg0).addView(mListViews.get(arg1), 0); return mListViews.get(arg1); } @Override public boolean isViewFromObject(View arg0, Object arg1) { Log.d(tag, "isViewFromObject"); return arg0 == (arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { Log.d(tag, "restoreState"); } @Override public Parcelable saveState() { Log.d(tag, "saveState"); return null; } @Override public void startUpdate(View arg0) { Log.d(tag, "startUpdate");
}
}
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved