这个例子是按照官网上的例子写的,有点抄袭的嫌疑,但是自己单独写一下会加深自己的印象。

首先是MainAcitivity.xml:

<LinearLayout 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:orientation="vertical"
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=".MainActivity"> <android.support.v4.view.ViewPager
android:translationZ="10dp"
android:elevation="10dp"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"> <Button
android:id="@+id/go_to_first_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="@string/first"
android:textAllCaps="false" /> <Button
android:id="@+id/go_to_last_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:text="@string/last"
android:textAllCaps="false" />
</LinearLayout> </LinearLayout>

布局很简单,就是一个ViewPager加上一个LinearLayout

然后ViewPager中的item的布局,这里的ViewPager使用的adapter是FragmentStatePagerAdatper

<?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:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"> <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No items."
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
</LinearLayout>

然后是MainAcitivity的代码:

package com.example.crazykids.customsizeviewpager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
static final int NUM_ITEMS = 10; private ViewPager mPager;
private MyAdapter mAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setAdapter(mAdapter);
mPager.setPageMargin(10);
mPager.setPageMarginDrawable(android.R.color.background_dark); Button button = (Button) findViewById(R.id.go_to_first_item);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button) findViewById(R.id.go_to_last_item);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(NUM_ITEMS - 1);
}
});
} public static class MyAdapter extends FragmentStatePagerAdapter { @Override
public float getPageWidth(int position) {
return 0.2f;
} @Override
public CharSequence getPageTitle(int position) {
return position + "";
} public MyAdapter(FragmentManager fm) {
super(fm);
} @Override
public Fragment getItem(int position) {
return MyListFragment.getInstance(position);
} @Override
public int getCount() {
return NUM_ITEMS;
}
} public static class MyListFragment extends ListFragment {
private int mNum; public static MyListFragment getInstance(int position) {
MyListFragment fragment = new MyListFragment(); Bundle args = new Bundle();
args.putInt("position", position);
fragment.setArguments(args); return fragment;
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() == null ? 1 : getArguments().getInt("position");
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page_item, container, false);
TextView text = (TextView) view.findViewById(R.id.text);
text.setText("Fragment # " + mNum);
return view;
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setListAdapter(
new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
MyArrays.arrays));
} @Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}

这里的fragment继承自ListFragment,在布局文件中ListView的id要设置成android:id/list,然后还有一个id为android:id/empty的textview,当list的adapter的数据为空时,

fragment为显示这个textview。

然后在FragmentStatePagerAdapter中有个getPageSize()的方法,这个方法是制定viewPager中的每个item占整个屏幕的比例,适当的设置这个值可以让viewpager中一次显示多个页面。

下面是效果图:

ViewPager的简单例子的更多相关文章

  1. Hibernate4.2.4入门(一)——环境搭建和简单例子

    一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...

  2. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答

    一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...

  3. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  4. ko 简单例子

    Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...

  5. mysql定时任务简单例子

    mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9     如果要每30秒执行以下语句:   [sql] update userinfo set endtime = now() WHE ...

  6. java socket编程开发简单例子 与 nio非阻塞通道

    基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...

  7. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  8. [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select

    以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...

  9. jsonp的简单例子

    jsonp的简单例子 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

随机推荐

  1. Codeforces Gym 100513D D. Data Center 前缀和 排序

    D. Data Center Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560/proble ...

  2. delphi 01设置 字体属性

    设置/获取 字体属性 名称 大小 粗体 斜体 下划线 删除线 颜色1 颜色2   uses MSHTML;   //设置 //------------------------------------- ...

  3. IT人才什么最重要

    做人最重要 做人最主要的一条就是尊重别人 不尊重别人,一般都是产生于自己认为自己别别人强,认为别人总是犯一些非常二的错误,于是就不自觉的通过言语.表情.反应等一些细节流露出来 这种人时间长了就会没人愿 ...

  4. 利用ajax获取到的网页源码不能执行js代码

    今天觉得我的博客中加载腾讯微博的速度很慢,所以就想改写为js,本来以为直接新建一个页面,把获取函数移到新的页面中,原来的页面只要使用xmlhttp去GET一下,然后把div的innerhtml属性等于 ...

  5. springMVC中的Controller里面定义全局变量

    转自:http://notebookdong.iteye.com/blog/1869852 使用SpringMVC的时候,如果想要在Controller中定义一个全局变量,并且实现在不同用户访问程序的 ...

  6. pt-online-schema-change原理解析 博客相关需要阅读

    xiaoboluo768  http://www.lai18.com/user/481193.html   都说pt-toolkit工具集中的pt-online-schema-change可以在线不锁 ...

  7. 1081. Rational Sum (20)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1081 the code ...

  8. 不安装oracle客户端也可以使用pl/sql developer

    通常情况下,用PL/SQL Developer连接Oracle是需要安装Oracle客户端软件的,这也就意味着你的硬盘将被占用大约1G-2G的空间,对于Windows操作系统来说,你还会多出一些开机自 ...

  9. 实例源码--Android底部功能分类Tab使用实例

    下载源码 技术要点:  1.底部功能布局实例 2.TAB使用详细实例 3.详细的源码注释 ...... 详细介绍: 1. 底部功能布局实例 底部功能布局是开发过程中常用到布局,本实例用TAB的方式实现 ...

  10. TortoiseGit安装教程

    TortoiseGit 是Windows下的可视化Git界面. 下载Git 网站地址: http://code.google.com/p/tortoisegit/ 安装前必须装上msysgit才能在W ...