ViewPager的使用
android.support.v4.view.ViewPager 和 android.support.v4.view.PagerTabStrip的使用 0、android.support.v4.view.PagerTabStrip默认效果 1.这里ViewPager和 PagerTabStrip都要把包名写全了,不然会ClassNotFount 2.API中说:在布局xml把PagerTabStrip当做ViewPager的一个子标签来用,不能拿出来,不然还是会报错 3.在PagerTabStrip标签中可以用属性android:layout_gravity=TOP|BOTTOM来指定title的位置(貌似不能用) 4.如果要显示出PagerTabStrip某一页的title,需要在ViewPager的adapter中实现getPageTitle(int) |
1、布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_tabstrip"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</RelativeLayout>
|
2、代码: package cn.zengfansheng.viewpager;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ViewPagerActivity extends Activity { private ViewPager viewPager;
private PagerTabStrip pagerTabStrip;
private MyPagerAdapter myPagerAdapter;
private List<TextView> views;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
init();
}
/**
* 初始化
*/
public void init() {
// 要展示的组件及内容
views = new ArrayList<TextView>();
for (int i = 0; i < 4; i++) {
TextView tView = new TextView(this);
tView.setText("viewpager测试:" + i);
views.add(tView);
}
// pageradapter初始化
viewPager = (ViewPager) this.findViewById(R.id.view_pager);
// 设置adapter
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
}
/**
* 选项卡标题效果的处理
*/
public void tabStrip(){
// 初始化
pagerTabStrip = (PagerTabStrip) this.findViewById(R.id.pager_tabstrip);
pagerTabStrip.setDrawFullUnderline(false);// 隐藏水平分割线
pagerTabStrip.setTabIndicatorColor(Color.RED);// 设置下划线的颜色
}
// *********************PagerAdapter**************************//
private final static String[] TITLES = new String[] { "福彩", "体彩", "好彩" ,"牛彩"};
private class MyPagerAdapter extends PagerAdapter {
@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
System.out.println("startUpdate");
}
// 显示的个数
@Override
public int getCount() {
System.out.println("getCount()");
return views.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
System.out.println("isViewFromObject()");
return view == object;
}
// 创建的时候调用,会调用多次
@Override
public Object instantiateItem(ViewGroup container, int position) {
System.out.println("instantiateItem()");
TextView tv = views.get(position);
container.addView(tv);
return tv;
}
// 销毁的时候调用,会被调用多次
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);//FIXME:这个一定要注释掉
System.out.println("destroyItem");
// container.removeViewAt(position);
//用上面的会报异常:The specified child already has a parent. You must call removeView() on the child's parent first.
container.removeView(views.get(position));
}
@Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
System.out.println("finishUpdate");
}
// 获取标题
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
}
} |
效果: |
|