首页 文章 精选 留言 我的

精选列表

搜索[传媒云原生 移动开发平台],共10000篇文章
优秀的个人博客,低调大师

【移动开发】Android中Fragment+ViewPager的配合使用

原本在上一篇博客中要讲解一个Fragment的综合应用,但是中间又想到了滑屏方式,所以就分类在总结了一下,(http://smallwoniu.blog.51cto.com/3911954/1308959)今天我将继续总结,关于Fragment+ViewPager的使用! 官方文档:http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPager is most often used in conjunction withFragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These areFragmentPagerAdapterandFragmentStatePagerAdapter; each of these classes have simplecode showing how to build a full user interface with them. 这里大家可以回忆一下如果像上篇中介绍ViewPager的使用,叶片填充数据是Layout,页面少的话还可以,如果页面过多的话,全部加载到手机内存中,可能会耗尽内存,手动销毁又太麻烦。官方推荐ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例。FragmentPagerAdapter和FragmentStatePagerAdapter这两个类都有简单的代码显示如何构建一个完整的用户界面与他们。 适配器类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.zhf.android_viewpager_fragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; /** * 自定义fragment适配器 * @author ZHF * */ public class MyFragmentPageAdapter extends FragmentPagerAdapter { public MyFragmentPageAdapter(FragmentManager fm) { super (fm); } @Override public int getCount() { return 3 ; } @Override public Fragment getItem( int position) { switch (position) { case 0 : return MyFragment.newInstance(position); case 1 : return MyFragment.newInstance(position); case 2 : return MyFragment.newInstance(position); default : return null ; } } } MyFragment类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.zhf.android_viewpager_fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * 用于创建Fragment对象,作为ViewPager的叶片 * @author ZHF * */ public class MyFragment extends Fragment { int mNum; //页号 public static MyFragment newInstance( int num) { MyFragment fragment = new MyFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt( "num" , num); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); //这里我只是简单的用num区别标签,其实具体应用中可以使用真实的fragment对象来作为叶片 mNum = getArguments() != null ? getArguments().getInt( "num" ) : 1 ; } /**为Fragment加载布局时调用**/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_pager_list, null ); TextView tv = (TextView) view.findViewById(R.id.text); tv.setText( "fragment+" + mNum); return view; } } 布局文件: activity_main.xml 1 2 3 4 5 6 7 8 9 10 <RelativeLayout 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.support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "fill_parent" android:layout_height= "fill_parent" /> </RelativeLayout> fragment_pager_list.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 <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 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" /> </LinearLayout> MainActivity类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.zhf.android_viewpager_fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity { private ViewPager mViewPager; private MyFragmentPageAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.viewpager); //这里因为是3.0一下版本,所以需继承FragmentActivity,通过getSupportFragmentManager()获取FragmentManager //3.0及其以上版本,只需继承Activity,通过getFragmentManager获取事物 FragmentManager fm = getSupportFragmentManager(); //初始化自定义适配器 mAdapter = new MyFragmentPageAdapter(fm); //绑定自定义适配器 mViewPager.setAdapter(mAdapter); } } 效果图: 效果与ViewPager中添加View的效果是一样的!但是它与View的区别在于它有自己的生命周期,可以随时更改自己的状态便于管理。 事实上使用FragmentPagerAdapter时,Fragment对象会一直存留在内存中,所以当有大量的显示页时,就不适合用FragmentPagerAdapter了,FragmentPagerAdapter适用于只有少数的page情况,像选项卡。 这个时候你可以考虑使用FragmentStatePagerAdapter,当使用FragmentStatePagerAdapter时,如果Fragment不显示,那么Fragment对象会被销毁,(滑过后会保存当前界面,以及下一个界面和上一个界面(如果有),最多保存3个,其他会被销毁掉)但在回调onDestroy()方法之前会回调onSaveInstanceState(Bundle outState)方法来保存Fragment的状态,下次Fragment显示时通过onCreate(Bundle savedInstanceState)把存储的状态值取出来,FragmentStatePagerAdapter比较适合页面比较多的情况,像一个页面的ListView。 本文转自zhf651555765 51CTO博客,原文链接:http://blog.51cto.com/smallwoniu/1322746,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册