Android FragmentStatePageAdapter的使用Demo
来源:互联网 发布:淘宝买刀犯法吗 编辑:程序博客网 时间:2024/06/07 04:59
上一篇写过FragmentPagerAdapter,这篇来介绍FragmentStatePagerAdapter,那么两者之间有何区别呢:
FragmentPagerAdapter更多的用于少量界面的ViewPager,比如Tab。划过的fragment会保存在内存中,尽管已经划过。而FragmentStatePagerAdapter和ListView有点类似,会保存当前界面,以及下一个界面和上一个界面(如果有),最多保存3个,其他会被销毁掉。
如果想要更详细的了解,可以查看官网API,下面给出按照官网上写出的Demo:
实现效果图:
源代码:
布局文件:
fragment_pager.xml(布局了ViewPager和两个按钮):
- <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();
- }
- }
0 0
- Android FragmentStatePageAdapter的使用Demo
- Android FragmentStatePageAdapter的使用Demo
- android下ExpandableListView的详细使用demo
- Android SoundPool即时音效的使用Demo
- Android HandlerThread 的使用及其Demo
- Android HandlerThread 的使用及其Demo
- Android HandlerThread 的使用及其Demo
- Android HandlerThread 的使用及其Demo
- Android HandlerThread 的使用及其Demo
- Android Studio的使用--导入 API Demo
- 简单的Android RecyclerView 使用小Demo
- android 高德地图Demo 的使用
- Android RecyclerViewStickyHeaders 使用Demo
- Android RecyclerView使用Demo
- Android Studio下使用Android Fragment的简单Demo(1)
- Android学习小Demo(7)SharedPreference的使用
- Android学习小Demo(8)SQLiteDatabase的使用
- Android Fragment的介绍与使用(案例Demo)
- Handle的作用
- sphinx下的max_matches取值对SetLimits的影响
- BASE64、MD5、SHA、HMAC几种加密算法
- 效率大提升 Android 4.4流畅运行背后的原理
- vim 树形目录插件NERDTree安装及简单用法
- Android FragmentStatePageAdapter的使用Demo
- java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inn
- HDU-2003 求绝对值
- 关于windows通过cuteftp访问linux的问题
- RouterOS配置
- “结构体名”和“结构体名是个指针”的区别
- printf()
- ARM 程序计数器(Program Counter,PC)
- HDU A + B Problem II