ViewPager

整理保存: 
简单的实现:
/**
 * 滑动
 * @author Administrator
 *
 */
private class MyPagerAdapter extends PagerAdapter{

@Override
public int getCount() {
Log.i(TAG, "getCount");
return _listview.size();
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
Log.i(TAG, "isViewFromObject");
return arg0==arg1;
}
@Override
public Object instantiateItem(View container, int position) {
Log.i(TAG, "instantiateItem");
((ViewPager)container).addView(_listview.get(position));
return _listview.get(position);
}
@Override
public Parcelable saveState() {
Log.i(TAG, "saveState");
// return super.saveState();
return null;
}
@Override
public void startUpdate(View container) {
Log.i(TAG, "startUpdate");
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
Log.i(TAG, "restoreState");
// super.restoreState(state, loader);
}
@Override
public void finishUpdate(View container) {
Log.i(TAG, "finishUpdate");
}

@Override
public void destroyItem(View container, int position, Object object) {
Log.i(TAG, "destroyItem");
((ViewPager)container).removeView(_listview.get(position));
}
}



//主方法
public class MainUiActivity extends Activity{
private String TAG="MainUiActivity";
private LayoutInflater _inflater;//加载器
private ViewPager viewpager;//控件
private View _firstView,_secondView,_thirdView;
private List<View> _listview;
private MyPagerAdapter _myPagerAdapter;
       
void init(){
_myPagerAdapter=new MyPagerAdapter();
viewpager=(ViewPager) findViewById(R.id.viewpager_layout);
viewpager.setAdapter(_myPagerAdapter);
_listview=new ArrayList<View>();
_inflater=getLayoutInflater();
_firstView=_inflater.inflate(R.layout.viewpager_one,null);
_secondView=_inflater.inflate(R.layout.viewpager_two,null);
_thirdView=_inflater.inflate(R.layout.viewpager_three, null);
}
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_ui);
init();
_listview.add(_firstView);
_listview.add(_secondView);
_listview.add(_thirdView);
//初始化当前显示的View
viewpager.setCurrentItem(0);
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
//activity从1到2滑动,2被加载后掉用此方法
Log.i(TAG, "onPageSelected"+position);
View v=_listview.get(position);
}
@Override
public void onPageScrolled(int position, float arg1, int arg2) {
//从1到2滑动,在1滑动前调用
Log.i(TAG, "onPageScrolled"+position);
}
@Override
public void onPageScrollStateChanged(int position) {
//状态有三个0空闲,1是增在滑行中,2目标加载完毕
Log.i(TAG, "onPageScrollStateChanged"+position);
                /**
                 * 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;

}
});
}
 
配置文件:
  <android.support.v4.view.ViewPager android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/viewpager_layout"/>
上一篇:【方法2】删除Map中Value重复的记录,并且只保留Key最小的那条记录


下一篇:Android Api Guid 之App Components 笔记