fragment的两种填充方式

Fragment系列文章:
1、Fragment全解析系列(一):那些年踩过的坑
2、Fragment全解析系列(二):正确的使用姿势
3、Fragment之我的解决方案:Fragmentation
4、Android Fragment 真正的完全解析(上)
两种方式:
一是布局里有个FrameLayout标签

//替换掉布局中的FrameLayout
        FirstFragment firstFragment = new FirstFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, firstFragment);
        transaction.commit();

二是使用viewpager,将fragment通过适配器塞进去

package com.example.viewpager_mvp.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by HASEE on 2017/6/22 11:39
 */

public class MyFragmentAdapter extends FragmentPagerAdapter {

    private List fragments;
    private String[] titles;


    public MyFragmentAdapter(FragmentManager fm, List fragments, String[] titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

你可能感兴趣的:(fragment的两种填充方式)