Android ViewPager简单的使用方法


public class MainActivity extends Activity {
private ViewPager viewPager; //声明一个ViewPager组件
private PagerTabStrip tabTitle; //声明ViewPager标题
private View tab01,tab02,tab03,tab04; //声明ViewPager中的几个标签页
private List<View> listView; //声明一个View类型List用于装载 ViewPager中的几个标签页
private List<String> pagerTitle;//声明一个Stirng类型的List用于装载ViewPager标题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建活动时调用
initViewPager();
}

private void initViewPager() {
//获取ViewPager中的几个标签页
tab01 = LayoutInflater.from(this).inflate(R.layout.tab01, null);
tab02 = LayoutInflater.from(this).inflate(R.layout.tab02, null);
tab03 = LayoutInflater.from(this).inflate(R.layout.tab03, null);
tab04 = LayoutInflater.from(this).inflate(R.layout.tab04, null);
//实例化listView同时装载 ViewPager中的几个标签页
listView = new ArrayList<View>();
listView.add(tab01);
listView.add(tab02);
listView.add(tab03);
listView.add(tab04);
//获取ViewPager标题
tabTitle = (PagerTabStrip) this.findViewById(R.id.tabstrip);
//实例化pagerTitle同时装载ViewPager标题
pagerTitle = new ArrayList<String>();
pagerTitle.add("选项卡01");
pagerTitle.add("选项卡02");
pagerTitle.add("选项卡03");
pagerTitle.add("选项卡04");
//获取ViewPager组件
viewPager = (ViewPager) this.findViewById(R.id.MyViewPager);
//为viewpager设置适配器
viewPager.setAdapter(new PagerAdapter() {


@Override
public CharSequence getPageTitle(int position) {

return pagerTitle.get(position);
}

@Override
public void destroyItem(ViewGroup container, int position,
Object object) {

container.removeView(listView.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

container.addView(listView.get(position));
return listView.get(position);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}

@Override
public int getCount() {
return listView.size();
}
});
}

//activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.shi.viewpager.MainActivity" >

<android.support.v4.view.ViewPager
android:id="@+id/MyViewPager"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabstrip"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:gravity="center" />
</android.support.v4.view.ViewPager>
</RelativeLayout>


//tab01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="01"
/>
</LinearLayout>

//tab02.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="02"
/>
</LinearLayout>

//tab03.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="03"
/>
</LinearLayout>

//tab04.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="04"
/>
</LinearLayout>

//图不弄上来!哈哈!谁会告诉一下谢谢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值