温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-22 23:58:23 来源:网络 阅读:77948 作者:zhf651555765 栏目:开发技术

原本在上一篇博客中要讲解一个Fragment的综合应用,但是中间又想到了滑屏方式,所以就分类在总结了一下【移动开发】Android中Fragment+ViewPager的配合使用,(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 with Fragment, 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 are FragmentPagerAdapter andFragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.


这里大家可以回忆一下如果像上篇中介绍ViewPager的使用,叶片填充数据是Layout,页面少的话还可以,如果页面过多的话,全部加载到手机内存中,可能会耗尽内存,手动销毁又太麻烦。官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例。FragmentPagerAdapterFragmentStatePagerAdapter这两个类都有简单的代码显示如何构建一个完整的用户界面与他们。


适配器类:

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类:

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

<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

<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类:

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);
    }
}


效果图:

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

效果与ViewPager中添加View的效果是一样的!但是它与View的区别在于它有自己的生命周期,可以随时更改自己的状态便于管理。


事实上使用FragmentPagerAdapter 时,Fragment对象会一直存留在内存中,所以当有大量的显示页时,就不适合用FragmentPagerAdapter了,FragmentPagerAdapter 适用于只有少数的page情况,像选项卡

这个时候你可以考虑使用FragmentStatePagerAdapter当使用FragmentStatePagerAdapter 时,如果Fragment不显示,那么Fragment对象会被销毁,(滑过后会保存当前界面,以及下一个界面和上一个界面(如果有),最多保存3个,其他会被销毁掉
但在回调onDestroy()方法之前会回调onSaveInstanceState(Bundle outState)方法来保存Fragment的状态,下次Fragment显示时通过onCreate(Bundle savedInstanceState)把存储的状态值取出来,
FragmentStatePagerAdapter
比较适合页面比较多的情况,像一个页面的ListView










向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI