一个小例子,可以帮忙记住如何使用ViewPager。
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 2012, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v4.view.PagerTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>
MainActivity.java
package com.example.viewpagerdemos2;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private ViewPager viewPager;
private PagerTitleStrip pagerTitleStrip; // 表示滑动的每一页的标题
private List<View> list; // 表示装载滑动的布局
private List<String> titleList; // 表示滑动的每一页的标题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponent();
// 动态加载布局
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab1, null);
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab2, null);
View view3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab3, null);
// 把动态布局装载到List<View>中
list = new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
// 把动态布局的标题装载到List<String>中
titleList = new ArrayList<String>();
titleList.add("title1");
titleList.add("title2");
titleList.add("title3");
viewPager.setAdapter(new MyAdapter());
}
class MyAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).addView(list.get(position));
return list.get(position);
}
/*
* 从ViewPager的实现过程中,可以发现,当我们在向左或者向右滑动的过程中,实际是在往PagerAdapter容器
* 中添加动态的布局文件,所以这边需要销毁的目标应该是要销毁的动态布局文件
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView(list.get(position));
}
// ViewPager中的包含的List<View>布局的个数
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
/*
* @see
* android.support.v4.view.PagerAdapter#isViewFromObject(android.view
* .View, java.lang.Object) 通过调用:instantiateItem(ViewGroup, int)
* 决定是否一个page视图是否去关联特殊的对象,对于一个正常运行的PagerAdapter 这个方法是需要被执行的。
* 如果这个方法返回true表示,表示视图view是有关联到Object这个对象的
*/
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
}
// 获得每一个pager的标题名称
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titleList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initComponent() {
viewPager = (ViewPager) findViewById(R.id.pager);
pagerTitleStrip = (PagerTitleStrip) findViewById(R.id.tabs);
}
}