Android ViewPager使用详解(转)
![此博文包含图片](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
![此博文包含视频](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api。而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等。那如何使用它呢,与LisstView类似,我们也需要一个适配器,他就是PagerAdapter。看一下api的图片,
- <</span>android.support.v4.view.ViewPager
这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载xml布局文件,他是不会显示内容的。 -
android:id="@+id/viewpager" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_gravity="center" >
- LayoutInflater
lf = getLayoutInflater().from(this); -
view1 = lf.inflate(R.layout.layout1, null); -
view2 = lf.inflate(R.layout.layout2, null); -
view3 = lf.inflate(R.layout.layout3, null); -
-
viewList = new ArrayList();// 将要分页显示的View装入数组中 -
viewList.add(view1); -
viewList.add(view2); -
viewList.add(view3);
- PagerAdapter
pagerAdapter = new PagerAdapter() { -
-
@Override -
public boolean isViewFromObject(View arg0, Object arg1) { -
-
return arg0 == arg1; -
} -
-
@Override -
public int getCount() { -
-
return viewList.size(); -
} -
-
@Override -
public void destroyItem(ViewGroup container, int position, -
Object object) { -
container.removeView(viewList.get(position)); -
-
} -
-
@Override -
public int getItemPosition(Object object) { -
-
return super.getItemPosition(object); -
} -
-
@Override -
public CharSequence getPageTitle(int position) { -
-
return titleList.get(position); -
} -
-
@Override -
public Object instantiateItem(ViewGroup container, int position) { -
container.addView(viewList.get(position)); -
weibo_button=(Button) findViewById(R.id.button1); -
weibo_button.setOnClickListener(new OnClickListener() { -
-
public void onClick(View v) { -
intent=new Intent(ViewPagerDemo.this,WeiBoActivity.class); -
startActivity(intent); -
} -
}); -
return viewList.get(position); -
} -
-
}; -
viewPager.setAdapter(pagerAdapter);
- public
class MyViewPagerAdapter extends PagerAdapter{ -
private List mListViews; -
-
public MyViewPagerAdapter(List mListViews) { -
this.mListViews = mListViews;//构造方法,参数是我们的页卡,这样比较方便。 -
} -
-
@Override -
public void destroyItem(ViewGroup container, int position, Object object) { -
container.removeView(mListViews.get(position));//删除页卡 -
} -
-
-
@Override -
public Object instantiateItem(ViewGroup container, int position) { //这个方法用来实例化页卡 -
container.addView(mListViews.get(position), 0);//添加页卡 -
return mListViews.get(position); -
} -
-
@Override -
public int getCount() { -
return mListViews.size();//返回页卡的数量 -
} -
-
@Override -
public boolean isViewFromObject(View arg0, Object arg1) { -
return arg0==arg1;//官方提示这样写 -
} -
}
instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)
- package
com.example.viewpagerdemo; -
- import
java.util.ArrayList; - import
java.util.List; - import
android.os.Bundle; - import
android.app.Activity; - import
android.content.Context; - import
android.content.Intent; - import
android.support.v4.view.PagerAdapter; - import
android.support.v4.view.PagerTabStrip; - import
android.support.v4.view.PagerTitleStrip; - import
android.support.v4.view.ViewPager; - import
android.util.AttributeSet; - import
android.view.LayoutInflater; - import
android.view.Menu; - import
android.view.View; - import
android.view.View.OnClickListener; - import
android.view.ViewGroup; - import
android.widget.Button; -
- public
class ViewPagerDemo extends Activity { -
-
private View view1, view2, view3;//需要滑动的页卡 -
private ViewPager viewPager;//viewpager -
private PagerTitleStrip pagerTitleStrip;//viewpager的标题 -
private PagerTabStrip pagerTabStrip;//一个viewpager的指示器,效果就是一个横的粗的下划线 -
private List viewList;//把需要滑动的页卡添加到这个list中 -
private List titleList;//viewpager的标题 -
private Button weibo_button;//button对象,一会用来进入第二个Viewpager的示例 -
private Intent intent; -
@Override -
public void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.activity_view_pager_demo); -
initView(); -
} -
-
-
view1 = findViewById(R.layout.layout1); -
view2 = findViewById(R.layout.layout2); -
view3 = findViewById(R.layout.layout3); -
-
LayoutInflater lf = getLayoutInflater().from(this); -
view1 = lf.inflate(R.layout.layout1, null); -
view2 = lf.inflate(R.layout.layout2, null); -
view3 = lf.inflate(R.layout.layout3, null); -
-
viewList = new ArrayList();// 将要分页显示的View装入数组中 -
viewList.add(view1); -
viewList.add(view2); -
viewList.add(view3); -
-
titleList = new ArrayList();// 每个页面的Title数据 -
titleList.add("wp"); -
titleList.add("jy"); -
titleList.add("jh"); -
-
PagerAdapter pagerAdapter = new PagerAdapter() { -
-
@Override -
public boolean isViewFromObject(View arg0, Object arg1) { -
-
return arg0 == arg1; -
} -
-
@Override -
public int getCount() { -
-
return viewList.size(); -
} -
-
@Override -
public void destroyItem(ViewGroup container, int position, -
Object object) { -
container.removeView(viewList.get(position)); -
-
} -
-
@Override -
public int getItemPosition(Object object) { -
-
return super.getItemPosition(object); -
} -
-
@Override -
public CharSequence getPageTitle(int position) { -
-
return titleList.get(position);//直接用适配器来完成标题的显示,所以从上面可以看到,我们没有使用PagerTitleStrip。当然你可以使用。 -
-
} -
-
@Override -
public Object instantiateItem(ViewGroup container, int position) { -
container.addView(viewList.get(position)); -
weibo_button=(Button) findViewById(R.id.button1);//这个需要注意,我们是在重写adapter里面实例化button组件的,如果你在onCreate()方法里这样做会报错的。 -
weibo_button.setOnClickListener(new OnClickListener() { -
-
public void onClick(View v) { -
intent=new Intent(ViewPagerDemo.this,WeiBoActivity.class); -
startActivity(intent); -
} -
}); -
return viewList.get(position); -
} -
-
}; -
viewPager.setAdapter(pagerAdapter); -
} -
-
@Override -
public boolean onCreateOptionsMenu(Menu menu) { -
getMenuInflater().inflate(R.menu.activity_view_pager_demo, menu); -
return true; -
} -
- }