public class MainActivity extends Activity { public static final String TAG = MainActivity.class.getSimpleName(); ViewPager pager = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //学习ViewPage //ViewPage可以实现滑动 使用PageAdatper pager = (ViewPager) this.findViewById(R.id.pager); pager.setAdapter(new MyPagerAdatper(this)); pager.setCurrentItem(Integer.MAX_VALUE /2); } private class MyPagerAdatper extends PagerAdapter { private List<Drawable> lists = null; private Context mContext; public MyPagerAdatper(Context context) { super(); mContext = context; lists = new ArrayList<Drawable>(); lists.add(mContext.getResources().getDrawable(R.drawable.ic_launcher)); lists.add(mContext.getResources().getDrawable(R.drawable.ic_launcher)); lists.add(mContext.getResources().getDrawable(R.drawable.ic_launcher)); lists.add(mContext.getResources().getDrawable(R.drawable.ic_launcher)); } //得到adapter的个数 @Override public int getCount() { Log.i(TAG, "getCount"); return Integer.MAX_VALUE; } //是否把对象作为view @Override public boolean isViewFromObject(View view, Object obj) { Log.i(TAG, "isViewFromObject"); return view == (View) obj; } @Override public void startUpdate(ViewGroup container) { // TODO Auto-generated method stub Log.i(TAG, "startUpdate"); super.startUpdate(container); } //初始化一个item @Override public Object instantiateItem(ViewGroup container, int position) { Log.i(TAG, "instantiateItem"); View view = View.inflate(mContext, R.layout.img, null); ImageView iv = (ImageView) view.findViewById(R.id.img); int size = lists.size(); int location = Integer.MAX_VALUE % size; iv.setImageDrawable(lists.get(location)); ((ViewPager) container).addView(view); return view; } //pageAdatper与BaseAdapter有很大区别,现在我理解为ViewPager释放有adapter处理 @Override public void destroyItem(View container, int position, Object object) { // TODO Auto-generated method stub Log.i(TAG, "destroyItem"); ((ViewPager) container).removeView((View) object); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub Log.i(TAG, "destroyItem"); super.destroyItem(container, position, object); } @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub Log.i(TAG, "setPrimaryItem"); super.setPrimaryItem(container, position, object); } @Override public void finishUpdate(ViewGroup container) { // TODO Auto-generated method stub Log.i(TAG, "finishUpdate"); super.finishUpdate(container); } @Override public void startUpdate(View container) { // TODO Auto-generated method stub Log.i(TAG, "startUpdate"); super.startUpdate(container); } @Override public Object instantiateItem(View container, int position) { // TODO Auto-generated method stub Log.i(TAG, "instantiateItem position:" + position); return super.instantiateItem(container, position); } @Override public void setPrimaryItem(View container, int position, Object object) { // TODO Auto-generated method stub Log.i(TAG, "setPrimaryItem position:" + position); super.setPrimaryItem(container, position, object); } @Override public void finishUpdate(View container) { // TODO Auto-generated method stub Log.i(TAG, "finishUpdate"); super.finishUpdate(container); } @Override public Parcelable saveState() { // TODO Auto-generated method stub Log.i(TAG, "saveState"); return super.saveState(); } @Override public void restoreState(Parcelable state, ClassLoader loader) { // TODO Auto-generated method stub Log.i(TAG, "restoreState"); super.restoreState(state, loader); } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub Log.i(TAG, "getItemPosition"); return super.getItemPosition(object); } @Override public void notifyDataSetChanged() { // TODO Auto-generated method stub Log.i(TAG, "notifyDataSetChanged"); super.notifyDataSetChanged(); } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub Log.i(TAG, "getPageTitle position:" + position); return super.getPageTitle(position); } //这个是一个很有用的方法默认返回1.0f,权重比例 @Override public float getPageWidth(int position) { // TODO Auto-generated method stub Log.i(TAG, "getPageWidth position:" + position); return super.getPageWidth(position); } } }
ViewPage
最新推荐文章于 2024-06-01 18:02:03 发布