上一篇写过FragmentPagerAdapter,这篇来介绍FragmentStatePagerAdapter,那么两者之间有何差别呢:

FragmentPagerAdapter很多其它的用于少量界面的ViewPager,比方Tab。划过的fragment会保存在内存中,虽然已经划过。而FragmentStatePagerAdapter和ListView有点类似,会保存当前界面,以及下一个界面和上一个界面(假设有),最多保存3个,其它会被销毁掉。

假设想要更具体的了解,能够查看官网API,以下给出依照官网上写出的Demo:

实现效果图:

源码:

布局文件:

fragment_pager.xml(布局了ViewPager和两个button):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" > <android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:measureWithLargestChild="true"
android:orientation="horizontal" > <Button
android:id="@+id/goto_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调到首页" >
</Button> <Button
android:id="@+id/goto_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调到尾页" >
</Button>
</LinearLayout> </LinearLayout>

fragment_pager_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb"
android:orientation="vertical" > <!-- 该Textview用来显示Fragment的页数 --> <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" /> <!-- 不为空用来显示ListView,假设为空,则显示TextView(数据为空) --> <FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" > <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" /> <TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="数据为空"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout> </LinearLayout>

代码文件:

MainActivity:

package com.fragmentdemo13_fragmentstatepageradapter;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 这里我们调用的是support.v4的包,所以MainActivity继承的是FragmentActivity,而不是Activity。
*/
public class MainActivity extends FragmentActivity {
public static final int NUM_ITEMS = 10;
private MyAdapter mAdapter;
private ViewPager mPager;
private Button button_first, button_last; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
/**
* 相同,因为调用的是support.v4的包,这里是getSupportFragmentManager(),而不是getFragmentManager();
*/
mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
/**
* 点击返回首页
*/
button_first = (Button) findViewById(R.id.goto_first);
button_first.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
/**
* 点击返回尾页
*/
button_last = (Button) findViewById(R.id.goto_last);
button_last.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(NUM_ITEMS - 1);
}
}); } }

MyAdapter.java:

package com.fragmentdemo13_fragmentstatepageradapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter; /**
* 这里继承的是FragmentStatePagerAdapter, 依据官方API的介绍,当项目中遇到使用大量的列表页面时,使用该适配器是个更好的选择。
* (This version of the pager is more useful when there are a large number of
* pages, working more like a list view.)
*/
public class MyAdapter extends FragmentStatePagerAdapter { public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* 仅仅须要实现以下两个方法就可以。
*/
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
} @Override
public int getCount() {
return MainActivity.NUM_ITEMS;
} }

ArrayListFragment.java:

package com.fragmentdemo13_fragmentstatepageradapter;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; public class ArrayListFragment extends ListFragment {
private int mNum;
public ArrayList<String> list = new ArrayList<String>(); /**
* 创建一个计算Fragment页面的实例,将怒num作为一个參数。
* (Create a new instance of
* CountingFragment, providing "num" as an argument.)
*/
public static ArrayListFragment newInstance(int num) { ArrayListFragment f = new ArrayListFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args); return f;
} /**
* 当调用该方法时,检索此实例的数量的參数。
* (When creating, retrieve this instance's number from
* its arguments.)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
} /**
* Fragment的UI仅仅显示它所在的页码。
* (The Fragment's UI is just a simple text view
* showing its instance number.)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pager_list, container,
false);
TextView tv = (TextView) view.findViewById(R.id.text);
tv.setText("Fragment #" + mNum);
return view;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, getData()));
} /**
* 在每个Fragment列表中展示的数据。
*/
private ArrayList<String> getData() {
for (int i = 0; i < 20; i++) {
list.add("nihao" + i);
}
return list;
} @Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "您点击了"+position, 0).show();
} }

源码下载:

点击下载源代码

Android FragmentStatePageAdapter的使用Demo的更多相关文章

  1. 解决Android微信支付官方demo运行失败

    Android微信支付官方demo运行失败,在此简单记录一下解决步骤 1.httpclient错误 官方给的demo是eclipse的,打开之后提示httpclient的错误,我知道在as下解决htt ...

  2. Android JNI学习(五)——Demo演示

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  3. Android之ViewPager循环Demo

    ViewPager是谷歌官方提供的兼容低版本安卓设备的软件包,里面包含了只有在安卓3.0以上可以使用的api.Viewpager现在也算是标配了,如果一个App没有用到ViewPager感觉还是比较罕 ...

  4. Android studio百度地图demo出现230错误,key校验失败

    转自daoxiaomianzi原文 Android studio 百度地图demo出现230错误,key校验失败 使用AndroidStudio导入Baidu地图的as版的demo,引入后,发现没有k ...

  5. Android第一代壳demo编写

    Android第一代壳Demo编写 前言 这篇文章是对姜维大佬的这篇文章[Android中的Apk的加固(加壳)原理解析和实现]的补充.建议先看一编姜维大佬的这篇文章再看. 姜维大佬写那篇文章的时间距 ...

  6. Android -- 自定义View小Demo,动态画圆(一)

    1,转载:(http://blog.csdn.NET/lmj623565791/article/details/24500107),现在如下图的效果: 由上面的效果图可以看到其实是一个在一个圆上换不同 ...

  7. 转:android surface简单使用Demo

    转: http://blog.csdn.net/listening_music/article/details/6860786 通过之前介绍的如何自定义View, 我们知道使用它可以做一些简单的动画效 ...

  8. 转:android 录制视频的Demo

    转:http://blog.csdn.net/peijiangping1989/article/details/7049991 在这里给出自己的一个测试DEMO,里面注释很详细.简单的视频录制功能. ...

  9. android Popupwindow 的一个demo源码

    一直想用一下PopupWindow,就是苦于没有demo,自己去研究有太懒,刚好最近研究推送,下载了一个腾讯信鸽的demo,里面用到了一个PopupWindow,效果还不错,弄下来记录一下: 1.核心 ...

随机推荐

  1. HDU 3068 最长回文 Manacher算法

    Manacher算法是个解决Palindrome问题的O(n)算法,能够说是个超级算法了,秒杀其它一切Palindrome解决方式,包含复杂的后缀数组. 网上非常多解释,最好的解析文章当然是Leetc ...

  2. JavaScript-4.6鼠标事件监听,获取鼠标坐标window.event---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html" ...

  3. Entity FramWork - 在VS里面直接创建表,并同步到数据库

    前面具体添加什么直接看: 1.Entity - 使用EF框架进行增删改查 - 模型先行 2.Entity - 使用EF框架进行增删改查 - 数据库先行 然后: 然后右键,可以添加[实体],也就是表.之 ...

  4. Codeforces Round #216 (Div. 2) D. Valera and Fools

    题目链接:http://codeforces.com/contest/369/problem/D 注意题意:所有fools都向编号最小的fool开枪:但每个fool都不会笨到想自己开枪,所以编号最小的 ...

  5. 一次完整的HTTP请求所经历的7个步骤(前三步是浏览器工作,后四步是服务器工作)

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...

  6. 请问,如何在windows系统下面同时使用中文和英文的cmd?_百度知道

    请问,如何在windows系统下面同时使用中文和英文的cmd?_百度知道 在批处理开始加一行chcp 437就是英文的cmdchcp 936就是中文的cmd

  7. jQuery事件大全

    jQuery事件大全 attribute:  $(" p" ).addclass(css中定义的样式类型) 给某个元素添加样式 $(" img" ).attr( ...

  8. 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.h

    继续看yusugomori的代码,看逻辑回归.在DBN(Deep Blief Network)中,下面几层是RBM,最上层就是LR了.关于回归.二类回归.以及逻辑回归,资料就是前面转的几篇.套路就是设 ...

  9. MFC 对话框中动态创建N级菜单以及响应事件

    创建一个基于对话框的工程,工程名为CreateMenu 为该对话框增加一个文件菜单项和测试菜单项,如下图所示   测试菜单项至少要有一个子菜单项 在对话框属性中关联该菜单 在resource.h中增加 ...

  10. 数字雨Shopex 4.8.5 SQL Injection Exp

    # -*- coding:utf-8 -* #Author:MXi4oyu #Email:798033502@qq.com #Shopex 4.8.5 SQL Injection Exp #转载请说明 ...