我正在尝试向我的应用添加一个页面浏览器,只是显示一些图片来解释应用程序的工作方式,但我遇到了很多问题...... 它正确显示的第一页但是当我拖到下一页时,我刚刚看到的所有页面都停留在后台,这会引起奇怪的可视化。
这是我的代码......
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.view.View;
import android.widget.ImageView;
public class ViewPagerActivity extends Activity {
private static int NUM_VIEWS = 5;
private MyPagerAdapter adapter;
private ViewPager pager;
private int[] pics = { R.drawable.bookmarks, R.drawable.bookmarks_click, R.drawable.help,
R.drawable.help_click, R.drawable.logo_wil };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.mypager);
pager.setAdapter(adapter);
}
private class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return NUM_VIEWS;
}
/**
* 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) {
ImageView view = new ImageView(ViewPagerActivity.this);
view.setImageResource(pics[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((ImageView) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) 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) {
}
}
}