本文介紹Android中實作左右滑動的指引效果。
本文的目的是要實作左右滑動的指引效果。那麼什麼是指引效果呢?現在的應用為了有更好的使用者體驗,一般會在應用開始顯示一些指引幫助頁面,使使用者能更好的了解應用的功能,甚至是一些新聞閱讀器會把一些頭條新聞以指引效果的形式顯示。說個最基本的,就是我們的手機主螢幕就是這種效果。
下面我們就開始實作我們的左右滑動指引效果。為了大家更好的了解,我們先看下實作效果,如下圖所示:
圖所示:
首先我們先看下項目結構:
核心實作代碼如下所示:
package com.test.guide;
import java.util.ArrayList;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
/**
* Android實作左右滑動指引效果
* @Description: Android實作左右滑動指引效果
* @File: MyGuideViewActivity.java
* @Package com.test.guide
* @Author Hanyonglu
* @Date 2012-4-6 下午11:15:18
* @Version V1.0
*/
public class MyGuideViewActivity extends Activity {
private ViewPager viewPager;
private ArrayList<View> pageViews;
private ImageView imageView;
private ImageView[] imageViews;
// 包裹滑動圖檔LinearLayout
private ViewGroup main;
// 包裹小圓點的LinearLayout
private ViewGroup group;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設定無标題視窗
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList<View>();
pageViews.add(inflater.inflate(R.layout.item05, null));
pageViews.add(inflater.inflate(R.layout.item06, null));
pageViews.add(inflater.inflate(R.layout.item01, null));
pageViews.add(inflater.inflate(R.layout.item02, null));
pageViews.add(inflater.inflate(R.layout.item03, null));
pageViews.add(inflater.inflate(R.layout.item04, null));
imageViews = new ImageView[pageViews.size()];
main = (ViewGroup)inflater.inflate(R.layout.main, null);
group = (ViewGroup)main.findViewById(R.id.viewGroup);
viewPager = (ViewPager)main.findViewById(R.id.guidePages);
for (int i = 0; i < pageViews.size(); i++) {
imageView = new ImageView(MyGuideViewActivity.this);
imageView.setLayoutParams(new LayoutParams(20,20));
imageView.setPadding(20, 0, 20, 0);
imageViews[i] = imageView;
if (i == 0) {
//預設選中第一張圖檔
imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
group.addView(imageViews[i]);
}
setContentView(main);
viewPager.setAdapter(new GuidePageAdapter());
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
}
// 指引頁面資料擴充卡
class GuidePageAdapter extends PagerAdapter {
@Override
public int getCount() {
return pageViews.size();
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(pageViews.get(arg1));
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(pageViews.get(arg1));
return pageViews.get(arg1);
public void restoreState(Parcelable arg0, ClassLoader arg1) {
public Parcelable saveState() {
return null;
public void startUpdate(View arg0) {
public void finishUpdate(View arg0) {
// 指引頁面更改事件監聽器
class GuidePageChangeListener implements OnPageChangeListener {
public void onPageScrollStateChanged(int arg0) {
public void onPageScrolled(int arg0, float arg1, int arg2) {
public void onPageSelected(int arg0) {
for (int i = 0; i < imageViews.length; i++) {
imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused);
if (arg0 != i) {
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
}
}
}
item.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
android:id="@+id/linearLayout02"
android:orientation="vertical"
android:background="@drawable/divider_horizontal_line">
</LinearLayout>
android:id="@+id/linearLayout1"
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/feature_guide_0" >
</ImageView>
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:background="@drawable/bg" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30px"
android:layout_marginBottom="30px"
android:layout_marginLeft="30px"
android:layout_marginRight="30px"
android:background="@drawable/divider_horizontal_line" >
</LinearLayout>
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingLeft="30px"
android:paddingRight="30px"
android:text="@string/text1" />
</LinearLayout>