ViewPager总结
ViewPager的包android.support.v4.view.ViewPager
ViewPager的主要部分是处理PagerAdapter和OnPagerChangeListener
使用ViewPager主要步骤有:
1、在布局文件中使用ViewPager控件
2、初始化ViewPager
3、初始化PagerAdapter
需要实现四个方法
isViewFromObject 判断选择哪个界面
getCount 界面数量
instantiateItem 显示当前的界面
destroyItem 销毁以前的界面
4、设置OnPagerChangeListener
主要在onPageSelected()方法中实现相关的
package com.viewpager;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.View;public class MainActivity extends Activity {ViewPager viewPager ;List<View> views = new ArrayList<View>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewPager = (ViewPager) findViewById(R.id.viewpager);init();}private void init(){views.add(getLayoutInflater().inflate(R.layout.first, null));views.add(getLayoutInflater().inflate(R.layout.seconde, null));views.add(getLayoutInflater().inflate(R.layout.third, null));viewPager.setAdapter(adapter);viewPager.setOnPageChangeListener(changeListener);}PagerAdapter adapter = new PagerAdapter() {@Overridepublic boolean isViewFromObject(View view, Object object) {return view == object;}@Overridepublic int getCount() {return views.size();}public Object instantiateItem(View container, int position) {((ViewPager)container).addView(views.get(position));return views.get(position);};public void destroyItem(View container, int position, Object object) {((ViewPager)container).removeView(views.get(position));};};OnPageChangeListener changeListener = new OnPageChangeListener() {@Overridepublic void onPageSelected(int position) {}@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageScrollStateChanged(int state) {}};}