首页 > 其他分享 >Fragment碎片的使用

Fragment碎片的使用

时间:2023-02-25 22:24:52浏览次数:30  
标签:String title Fragment 碎片 fragment 使用 import include

Fragment碎片的使用类似于Activity,不过碎片的生命周期比Activity要丰富一些

关于Fragment有两种使用方法

第一种使用方法,静态注册

首先需要有一个布局文件,这里以线性布局为例,假设里面放置了一个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);
    }
}

 

 

 

 使用到的布局文件如上所示,代码示例不唯一,只要书写规范符合要求,就拥有无限的创造性。

标签:String,title,Fragment,碎片,fragment,使用,import,include
From: https://www.cnblogs.com/laohei114514/p/17155576.html

相关文章

  • Python | import re模块的使用
    Python对文字处理的能力有很多功能,除了str对象自带的一些方法外,就是正则表达式这个强大的模块了。re模块是Python中处理正则表达式的模块,里面有一些python中常用的正则......
  • Python | import pickle模块的使用
    Python程序运行中得到了一些字符串,列表,字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据。Python中有个序列化过程叫作pickle,它能够实......
  • Python | import argparse模块的使用
    我们在写python脚本的时候,有时候需要在运行的时候传入参数,而不是写死在程序里,这个时候就要用到argparse模块。argparse模块是Python内置的用于命令项选项与参数解析的......
  • 特斯拉恢复使用毫米波雷达以及4D毫米波雷达的发展现状
    一、特斯拉恢复使用毫米波雷达2022年6月7日,特斯拉向FCC提交了有关毫米波雷达的材料。通常,在美国销售的射频相关产品必须向FCC提交第三方测试材料,以证明符合美国频谱控......
  • openEuler管理网络-使用nmcli命令配置ip
    25openEuler管理网络-使用nmcli命令配置ip说明:使用nmcli命令配置的网络配置可以立即生效且系统重启后配置也不会丢失。25.1nmcli介绍nmcli是NetworkManager的一个......
  • 使用有限状态自动机解决剑指 Offer 20. 表示数值的字符串
    剑指Offer20.表示数值的字符串请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。数值(按顺序)可以分成以下几个部分:若干空格一个 小数 或者 整数(......
  • 使用Zookeeper实现分布式锁
    在单体项目中JVM中的锁就可以完成大部分需求,但是在分布式项目中,项目被拆分成各个模块,分别部署在不同的服务器中,比如一个完成一个订单可以需要多个操作,订单模块生成订单信息......
  • k8s operator开发与使用
    k8soperator开发与使用本篇文章用于记录k8soperator的开发,此处以一个简单的demo来记录operator的开发过程及相关要点;本文中使用kubebuilder工具来生成相关代码一、......
  • Docker安装及基本使用
    一、安装Docker(ubuntu)卸载旧版本(可选)sudoapt-getremovedockerdocker-enginedocker.iocontainerdrunc添加Docker的官方GPG密钥curl-fsSLhttps://mirrors.ustc.......
  • Git使用
    GitGit是一个分布式版本控制工具,用来管理多人写作的代码。同时也是一个内容管理系统。Git与SVN的区别:Git是分布式的,svn不是。这是最核心的区别Git把内容按元数据方式......