ViewPager的简单例子
这个例子是按照官网上的例子写的,有点抄袭的嫌疑,但是自己单独写一下会加深自己的印象。
首先是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的简单例子的更多相关文章
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- mysql定时任务简单例子
mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9 如果要每30秒执行以下语句: [sql] update userinfo set endtime = now() WHE ...
- java socket编程开发简单例子 与 nio非阻塞通道
基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select
以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...
- jsonp的简单例子
jsonp的简单例子 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
随机推荐
- codeforces Gym 100500H H. ICPC Quest 水题
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- zookeeper作为soa服务器集群的协调调度服务器
zookeeper作为soa服务器集群的协调调度服务器,当然自身也支持集群. ZooKeeper搭建系列集 ZooKeeper系列之一:ZooKeeper简介 ZooKeeper系列之二:ZooKee ...
- [Angular 2] Inject Service with "Providers"
In this lesson, we’re going to take a look at how add a class to the providers property of a compone ...
- [WebGL] Setting Up WebGL
In this lesson we cover setting up WebGL for use, including creating a canvas, getting the WebGL ren ...
- memset用法详解(转)
问题描述: int * cow = new int[n+1]; memset(cow,0,(n+1)*4); 将N+1个元素置成0,为什么不行 memset是对一个字节的int4个字节,因此*4 但是 ...
- C#多线程的介绍(园子里比较全的一篇)
一.多线程的概念 Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一 ...
- cocos2d-x使用tinyxml2存储解析xml
我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库. 导入头文件:#include "support/tinyxml2/tinyxml2.h" using nam ...
- Branch and Bound:分支限界算法
http://blog.sciencenet.cn/blog-509534-728984.html 分支定界 (branch and bound) 算法是一种在问题的解空间树上搜索问题的解的方法.但与 ...
- jenkis编译报错:需要class,interface或enum
现象: 1.jenkis编译报错:需要class,interface或enum 2.使用ant进行编译ok. 解决方法: 1. Jenkis重新编译一个以前成功的svn版本,直至编译成功. 2.Jen ...
- 用CCRenderTexture和BlendFunc制作游戏教学时使用的黑色覆盖层
游戏快要完成了,准备做教学. 我们的教学是在整个界面上盖一层灰色图片,然后把提示点击的按钮部分亮出来,也就是在一块黑色图片上,按需求扣空一小部分.如图,把武器部分扣空,那么在其它地方又会扣空其它部分, ...