这是一些以 ViewPager 方式执行的代码(这正是您的操作方式)
package com.test.viewpager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class WalkthroughActivity extends Activity {
private static final int MAX_VIEWS = 5;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.walkthrough_activity);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new WalkthroughPagerAdapter());
mViewPager.setOnPageChangeListener(new WalkthroughPageChangeListener());
}
class WalkthroughPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return MAX_VIEWS;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (View) object;
}
@Override
public Object instantiateItem(View container, int position) {
Log.e("walkthrough", "instantiateItem(" + position + ");");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View imageViewContainer = inflater.inflate(R.layout.walkthrough_single_view, null);
ImageView imageView = (ImageView) imageViewContainer.findViewById(R.id.image_view);
switch(position) {
case 0:
imageView.setImageResource(R.drawable.image1);
break;
case 1:
imageView.setImageResource(R.drawable.image2);
break;
case 2:
imageView.setImageResource(R.drawable.image3);
break;
case 3:
imageView.setImageResource(R.drawable.image4);
break;
case 4:
imageView.setImageResource(R.drawable.image5);
break;
}
((ViewPager) container).addView(imageViewContainer, 0);
return imageViewContainer;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
}
}
class WalkthroughPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
// Here is where you should show change the view of page indicator
switch(position) {
case MAX_VIEWS - 1:
break;
default:
}
}
}
}
这是我的walkthrough_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/screen_navigation_button" />
<!-- This TextView will not swipe when you swipe in the ViewPager -->
<TextView
android:id="@id/screen_navigation_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:padding="10dip"
android:text="Hello, Walkthrough!"
android:textSize="18sp" />
</RelativeLayout>
就这么walkthrough_single_view.xml
简单——
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
希望有帮助:)