首页 技术 正文
技术 2022年11月21日
0 收藏 750 点赞 3,974 浏览 3361 个字

下一篇:《Android ViewPager再探:增加滑动指示条》

ViewPager需要用到适配器PagerAAdapter,以下四个函数需要重写:

instantiateItem(ViewGroup container, int position):创建指定位置的页面视图。适配器增加即将创建的View视图到这里给定的container中。
destroyItem(ViewGroup container, int position, Object object):移除一个给定位置的页面。
getCount():返回当前有效视图的个数。
isViewFromObject(View view, Object object):判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是代表的同一个视图。

主界面的布局只要加上

<android.support.v4.view.ViewPager />

即可:

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"
tools:context=".MainActivity"> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/> </RelativeLayout>

决定ViewPager有3个页面,每个页面都要写布局:

以第一个first_page.xml为例:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="这是第一页"
android:textSize="20sp"/> </RelativeLayout>

MainActivity需要在onCreate里对其初始化,并调用适配器:

 package com.example.hopecapital.myapplication; import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View; import java.util.ArrayList;
import java.util.List; public class MainActivity extends Activity {
private View first,second,third;
private ViewPager viewPager;//对应 <android.support.v4.view.ViewPager/>控件
private List<View> viewList;//View数组 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /*初始化*/
viewPager = (ViewPager)findViewById(R.id.viewpager);
LayoutInflater inflater = getLayoutInflater();
first = inflater.inflate(R.layout.first_page,null);
second = inflater.inflate(R.layout.second_page,null);
third = inflater.inflate(R.layout.third_page,null); viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
viewList.add(first);
viewList.add(second);
viewList.add(third); /*适配器部分*/
NewPagerAdapter pagerAdapter = new NewPagerAdapter(viewList);
viewPager.setAdapter(pagerAdapter);
} }

最后也是最重要的,继承自PagerAdapter的适配器:

NewPagerAdapter.java:

 package com.example.hopecapital.myapplication; import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup; import java.util.List; /**
* Created by LT on 2015/7/27.
*/
public class NewPagerAdapter extends PagerAdapter{
public List<View> viewList; public NewPagerAdapter(List<View> viewList){
this.viewList = viewList;
} /*下面四个函数是一定要重写的*/
@Override
public boolean isViewFromObject(View arg0,Object arg1){
//判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是代表的同一个视图(判断key)
// TODO Auto-generated method stub
return arg0 == arg1;
} @Override
public int getCount() {//返回要滑动的VIew的个数
// TODO Auto-generated method stub
return viewList.size();
} @Override
public void destroyItem(ViewGroup container, int position,
Object object) {//从当前container中删除指定位置(position)的View;
// TODO Auto-generated method stub
container.removeView(viewList.get(position));
} @Override
public Object instantiateItem(ViewGroup container, int position) {
//实例化:将当前视图添加到container中,并返回当前View(传送key)
// TODO Auto-generated method stub
container.addView(viewList.get(position)); return viewList.get(position);
}
}

以上,便完成了简单的ViewPager。

下一篇: 一:Shell基础
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731