AT-FragmentPagerAdapter
关于FragmentPagerAdapter的粗略翻译
英文版api地址:FragmentPagerAdapter(自备梯子)
FragmentPagerAdapter
类概述(Class Overview):
是PagerAdapter的实现类,展示的每一页是Fragment,并且可以长时间持续保存在fragment manager中,以供用户返回到以前的页面。
这种PagerAdapter(即FragmentPagerAdapter)的最佳用法就是用在:少数的、静态的Fragment上,比如一系列的标签。每一页被用户浏览过的的Fragment都会被保存在内存中,尽管此时视图层次(view hieratchy)因为不可见而已经被销毁了。这便导致会有一些内存被占用,因为这些内存中要被用来保存保存fragment的实例状态之类的呀。所以说,你想要使用ViewPager来展示比较多的页面时,请你考虑使用FragmentStatePagerAdapter。
当使用FragmentPagerAdapter时,要注意:和FragmentPagerAdapter搭配使用的ViewPager时,Adapter中必须使用有效的ID集合(我的理解是往Adapter里面传的List<Fragment>中添加的不能是同一个Fragment的引用,尽管每个页面有可能相同,但也请多new几个Fragment然后添加进List中去)
子类实现时,只需要实现getItem(int) 和 getCount() 就可以啦!
这里有一个例子来示范怎样使用FragmentPagerAdapter:
public class FragmentPagerSupport extends FragmentActivity {
static final int NUM_ITEMS = 10;
MyAdapter mAdapter;
ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.goto_first);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0);
}
});
button = (Button)findViewById(R.id.goto_last);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(NUM_ITEMS-1);
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
public static class ArrayListFragment extends ListFragment {
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();
// Supply num input as an argument.
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;
}
/**
* 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 v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
R.layout.fragment_pager的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"> <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:orientation="horizontal"
android:gravity="center" android:measureWithLargestChild="true"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0">
<Button android:id="@+id/goto_first"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/first">
</Button>
<Button android:id="@+id/goto_last"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/last">
</Button>
</LinearLayout>
</LinearLayout>
R.layout.fragment_pager_list的源代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb"> <TextView android:id="@+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/hello_world"/> <!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it -->
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/> <!-- Here is the view to show if the list is emtpy -->
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="No items."/> </FrameLayout> </LinearLayout>
公有构造方法(Public Constructors):
- public FragmentPagerAdapter (FragmentManager fm)
公有方法(Public Methods):
public void destroyItem(ViewGroup container,int position,Object object)
移除对应指定位置(position)的页面。适配器负责从container中移除这些视图,尽管它唯一保证在finishUpdate(ViewGroup)之前执行完!
public void finishUpdate(ViewGroup container)
当展示页面变化成功时调用该方法。此时应该确保所有的页面在适当的时候已经完成了添加、移除工作。
public abstract Fragment getItem(int position)
返回已经和特定位置有联系的Fragment
public long getItemId(int position)
返回指定位置的页面的标示,该标示是唯一的
默认的实现是返回指定位置的序号(即position),如果你的需求实现中,子页面的位置可以变化,你可以尝试通过重写该方法来实现
public Object instantiateItem(ViewGroup container,int position)
创建指定页。适配器负责添加视图到父布局中去。该方法只确保它会在finishUpdate(ViewGroup)调用之前完成
Returns:
Object:返回的这个对象代表了新的一个页面(Page)。它不一定是一个视图View,有可能是一些其他的父布局(Container)。
public boolean isViewFromObject(View view,Object object)
判断一个页面(Page)是否对应产生了一个唯一的键对象,而产生唯一键对象的过程是通过instantiateItem(ViewGroup,int)方法的调用和完成实现的。本方法保证了 PagerAdapter能够运行正常。
public void restoreState(Parcelable state,ClassLoader loader)
恢复和当前适配器相关的任何实例状态,前提是你要在之前就通过saveState()方法保存了实例状态
public Parcelable saveState()
保存任何和当前适配器有关的数据,以供在之后UI状态发生重建时的实例状态恢复
public void setPrimaryItem(ViewGroup container,int position,Object object)
调用该方法是为了告知适配器哪个子页面目前被视为主要的子页面。它对应的是给用户展示的当前页。
public void startUpdate(ViewGroup container)
当展示页有变化并且将要开始更新时会调用该方法。
PS:
关于上面的:Adapter中必须使用有效的ID集合。我的理解如下:
当我在给FragmentAdapter(我自己定义的实现了FragmentPagerAdapter的Adapter类)传入List<Fragment>参数时,
当我给List里面同时添加同一个FragmentContent(自己定义的一个继承Fragment的类)多次时,如下图“

跑起来会报错,如下图:

具体报错内容为:java.lang.IllegalStateException: Can't change tag of fragment FragmentContent{421fbc80 id=0x7f0c006a android:switcher:2131492970:0}: was android:switcher:2131492970:0 now android:switcher:2131492970:1
不难看出,就是因为在引用了同一个Fragment传入才导致Adapter内部出错。具体原因不知。这便验证了上面的观点。
AT-FragmentPagerAdapter的更多相关文章
- ViewPager适配器FragmentStatePagerAdapter 与FragmentPagerAdapter
使用FragmentPagerAdapter存在删除dataSet顺序错乱的问题 改用FragmentStatePagerAdapter
- FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时遇到的坑,给textview赋值时出现的空指针异常
FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时,给textview赋值时出现的空指针异常 public class BaseFr ...
- FragmentPagerAdapter+ViewPager实现Tab切换效果
1.Activity 加载布局文件,获取Viewpager控件 给ViewPager填充适配器. import android.app.ActionBar; import android.app ...
- FragmentPagerAdapter+ViewPager+Fragment
FragmentPagerAdapter中会在滑动到2页时,会预加载第三个页面.如果在这些页面中都有网络请求,那么当你还没有看到第三页时,第三页的数据请求已经发出.这样就会造成,当已进入该页面,可能会 ...
- 转载---ViewPager,PagerAdapter,FragmentPagerAdapter和FragmentStatePagerAdapter的分析对比
转载:http://blog.csdn.net/dreamzml/article/details/9951577 ViewPager ViewPager 如其名所述,是负责翻页的一个 View.准确说 ...
- FragmentStatePageradapter 与 FragmentPageradapter的区别
FragmentPageradapter : 会将fragment储存在内存中 每次加载页面读取内存中的fragment FragmentStatePageradapter: 不会将fragment储 ...
- [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
原文链接:http://blog.csdn.net/zhaokaiqiang1992 FragmentPagerAdapter是android-support-v4支持包里面出现的一个新的适配器,继承 ...
- FragmentPagerAdapter实现刷新
在fragmentpageadapter的instantiateItem方法里,他会先去FragmentManager里面去查找有没有相关的fragment如果有就直接使用如果没有才会触发fragme ...
- Android Tab -- 使用ViewPager、Fragment、FragmentPagerAdapter来实现
原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 效果:滑动切换:点击标签切换. 代码:https://github.com/l ...
- 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?
在一个 Android 应用中,我使用 FragmentPagerAdapter 来 处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我 ...
随机推荐
- JSON--List集合转换成JSON对象
转自:http://www.cnblogs.com/xmaomao/p/3184542.html 1. 简单的手动放置 键值对 到JSONObject,然后在put到JSONArray对象里 List ...
- 明修栈道,暗渡陈仓----之私募一哥徐翔新玩法 z
前言:去年以来,因徐翔和宁电突然举牌资质平平的 000692 惠天热电,引起本人的兴趣,陆陆续续花了比较多的时间和精力去研究和跟踪000692惠天热电,期间也两次亲自去沈阳调研,从一些台前幕后人士那里 ...
- <转>SpringMVC与Struts2 比较总结
原链接:http://blog.csdn.net/chenleixing/article/details/44570681 个人整理: 1.级别不同:SpringMVC :方法 Struts是 类级 ...
- 浅析ado.net获取数据库元数据信息 DeriveParameters
写这个文章源于早先对ADO.Net获取数据库元数据上的认识,去年我在阅读ADO.Net Core Reference的时候曾经注意过DataSet的FillSchema的这个方法.这方面,在我之前的随 ...
- HIbernate学习笔记(七) hibernate中的集合映射和继承映射
九. 集合映射 1. Set 2. List a) @OrderBy 注意:List与Set注解是一样的,就是把Set更改为List就可以了 private List< ...
- 流(stream)
流的概念 Java程序通过流来完成输入/输出.流是生产或消费信息的抽象.流通过Java的输入/输出系统与物理设备链接.尽管与它们链接的物理设备不尽相同,所有流的行为具有同样的方式.这样,相同的输入/输 ...
- nyoj 69 数的长度
数的长度 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 N!阶乘是一个非常大的数,大家都知道计算公式是N!=N*(N-1)······*2*1.现在你的任务是计算出 ...
- [OC Foundation框架 - 12] NSNumber
1.概念 NSArray,NSDictionary只能放OC对象,不能放入基本数据类型 必须使用包装类NSNumber,把基本数据类型包装成OC对象 不支持自动包装解包 void number() ...
- Android 命名规范和编码规范
简明概要 多写注释 一.关于命名规范 对于开发项目来说肯定是要有统一的规范,然而命名规范需要做到哪几点呢? 答: 首先,不能反人类. 再来就是,要望文而知其意. 下面就来说说具体该怎么去规范我们的代 ...
- Ajax调用WebService(一)
Ajax调用WebService(一) http://www.cnblogs.com/leslies2/archive/2011/01/26/1934889.html 分类: Ajax 使用技术 We ...