第一种使用方法,静态注册
首先需要有一个布局文件,这里以线性布局为例,假设里面放置了一个ImageView和一个TextView
然后写一个类,继承自Fragment,然后在OnCreateView方法中通过inflater.inflate()获取到布局文件
再逐个给布局文件的控件设置属性,最后返回view
然后在Activity中对应的xml文件引用<Fragment>标签 注意给name属性设置值为包名
第二种使用方法,动态注册
这里以ViewPager为例
首先写一个类继承自Fragment,写一个静态的返回实例方法(不是单例模式
在这个方法里面获取到外界传来的参数,以便给我们的Fragment所对应的布局文件设置格式。
这里用到了Bundle包裹,先把外界传递的参数给包裹,再通过包裹把参数给到onCreateView方法中,再通过inflate方法获取到布局对象
再通过对象找到具体控件,再给控件设置值,最后返回view。
这里使用了FragmentPagerAdapter适配器(已经过时了,最新的好像用的是ViewPager2,还没学到那里)
getItem方法返回一个Fragment对象(通过静态方法
最后再活动页面给ViewPager对象设置适配器,最后运行即可
package com.example.clockapp.Fragment;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class MyFirstAdapter extends FragmentPagerAdapter {
private String[]title;
private String[]include;
public MyFirstAdapter(@NonNull FragmentManager fm, String[]title,String[]include) {
super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.include = include;
this.title = title;
@NonNull
@Override
public Fragment getItem(int position) {
return MyFirstFragment.getInstance(position,title[position],include[position]);
@Override
public int getCount() {
return title.length;
package com.example.clockapp.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.clockapp.R;
public class MyFirstFragment extends Fragment {
private View mView;
private Context mContext;
private String title;
private String include;
public static MyFirstFragment getInstance(int position,String title,String include){
MyFirstFragment fragment = new MyFirstFragment();
Bundle bundle = new Bundle();
bundle.putString("title",title);
bundle.putString("include",include);
fragment.setArguments(bundle);
return fragment;
@Nullable
//注意了,这里的onCreateView方法是动态创建一个碎片视图时加载的方法,第二个参数container是指的碎片所在的容器
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getActivity();//Fragment的方法
if(getArguments()!=null){
title = getArguments().getString("title","nothing");
include = getArguments().getString("include","nothing");
mView = inflater.inflate(R.layout.fragment_item,container,false);
TextView tv_title = mView.findViewById(R.id.tv_title);
TextView tv_include = mView.findViewById(R.id.tv_include);
tv_title.setText(title);
tv_include.setText(include);
return mView;
package com.example.clockapp.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.annotation.SuppressLint;
import android.os.Bundle;
import com.example.clockapp.R;
public class ViewPagerByFragmentActivity extends AppCompatActivity {
private ViewPager vp_fragment;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_by_fragment);
vp_fragment = findViewById(R.id.vp_fragment);
String[] title = {"1","2","3"};
String[] include = {"1111","2222","3333"};
MyFirstAdapter adapter = new MyFirstAdapter(getSupportFragmentManager(),title,include);
vp_fragment.setAdapter(adapter);
vp_fragment.setCurrentItem(0);