当前位置: 首页 > news >正文

Android 自定义BaseFragment

直接上代码:

BaseFragment代码:

package com.example.custom.fragment;import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;/*** 基本Fragment* */
public abstract class BaseFragment extends Fragment {/*** 设置数据* */protected abstract void setData(Bundle savedInstanceState);/*** 绑定布局* */protected abstract int setContentLayout();/*** 初始化组件* */protected abstract void setControls(View view);@Overridepublic void onAttach(@NonNull Context context) {super.onAttach(context);}@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setData(savedInstanceState);}@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View fragmentView  = inflater.inflate(setContentLayout(),container,false);return fragmentView;}@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);// 控件setControls(view);}@Overridepublic void onStart() {super.onStart();}@Overridepublic void onResume() {super.onResume();}@Overridepublic void onPause() {super.onPause();}@Overridepublic void onStop() {super.onStop();}@Overridepublic void onDestroy() {super.onDestroy();}@Overridepublic void onDetach() {super.onDetach();}
}

HomeFragment代码:

package com.example.custom.fragment;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import com.example.custom.R;public class HomeFragment extends BaseFragment{private TextView mainTv;@Overrideprotected void setData(Bundle savedInstanceState) {}@Overrideprotected int setContentLayout() {return R.layout.fragment_home;}@Overrideprotected void setControls(View view) {mainTv = view.findViewById(R.id.mainTV);mainTv.setText("Home页面");}}

HomeFragment布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/mainTV"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="home"/></LinearLayout>

使用:

(1) main_layout.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.fragment.app.FragmentContainerViewandroid:id="@+id/fragment_container_view"android:layout_width="match_parent"android:layout_height="match_parent"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:orientation="horizontal"><Buttonandroid:id="@+id/homeBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="home"/><Buttonandroid:id="@+id/shoppingBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="shopping"/><Buttonandroid:id="@+id/myBtn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="my"/></LinearLayout></RelativeLayout>

(2) MainActivity.java代码:

package com.example.custom.main;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.example.custom.R;
import com.example.custom.activity.BaseActivity;
import com.example.custom.fragment.HomeFragment;
import com.example.custom.fragment.MyFragment;
import com.example.custom.fragment.ShoppingFragment;public class MainActivity extends BaseActivity {private Button home,shop,my;// 获取Fragment管理对象(此方法只能在继承AppCompatActivity中使用)private FragmentManager fragmentManager = getSupportFragmentManager();private FragmentTransaction transaction;// fragmentprivate HomeFragment homeFragment = new HomeFragment();private ShoppingFragment shoppingFragment = new ShoppingFragment();private MyFragment myFragment = new MyFragment();@Overridepublic void setData(Bundle savedInstanceState) {// 竖屏setScreenPortrait(true);}@Overridepublic int setContentLayout() {return R.layout.main_layout;}@Overridepublic void setControls() {home = findViewById(R.id.homeBtn);home.setOnClickListener(homeClick);shop = findViewById(R.id.shoppingBtn);shop.setOnClickListener(shopClick);my = findViewById(R.id.myBtn);my.setOnClickListener(myClick);// 默认home// 必须写下面的三条语句transaction = fragmentManager.beginTransaction();transaction.replace(R.id.fragment_container_view,homeFragment);transaction.commit();}View.OnClickListener homeClick = new View.OnClickListener() {@Overridepublic void onClick(View view) {// 必须写下面的三条语句transaction = fragmentManager.beginTransaction();transaction.replace(R.id.fragment_container_view,homeFragment);transaction.commit();}};View.OnClickListener shopClick = new View.OnClickListener() {@Overridepublic void onClick(View view) {// 必须写下面的三条语句transaction = fragmentManager.beginTransaction();transaction.replace(R.id.fragment_container_view,shoppingFragment);transaction.commit();}};View.OnClickListener myClick = new View.OnClickListener() {@Overridepublic void onClick(View view) {// 必须写下面的三条语句transaction = fragmentManager.beginTransaction();transaction.replace(R.id.fragment_container_view,myFragment);transaction.commit();}};}

相关文章:

  • Panalog 日志审计系统 sessiptbl.php 前台RCE漏洞复现
  • 蓝桥杯(Web大学组)2022国赛真题:水果消消乐
  • Python学习之路-爬虫进阶:爬虫框架雏形
  • 构建智慧交通平台:架构设计与实现
  • Python爬虫——解析库安装(1)
  • 【操作系统】Ubuntu Swap内存扩容
  • 【30秒看懂大数据】数据标准
  • AlmaLinux更换鼠标样式为Windows样式
  • WordPress函数wptexturize的介绍及用法示例,字符串替换为HTML实体
  • 随机过程及应用学习笔记(四) 马尔可夫过程
  • LLVM实战之LLVM bitcode转换成目标平台汇编码
  • 【30秒看懂大数据】数据中台
  • 不到1s生成mesh! 高效文生3D框架AToM
  • Java学习网络编程
  • Apache 神禹(shenyu)源码阅读(三)——被网关路由的后端服务 Client 向 Admin 注册的数据传输(Client端)
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • Angular 2 DI - IoC DI - 1
  • ES6系统学习----从Apollo Client看解构赋值
  • 基于Android乐音识别(2)
  • 精彩代码 vue.js
  • 三分钟教你同步 Visual Studio Code 设置
  • 一道面试题引发的“血案”
  • 你对linux中grep命令知道多少?
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 数据库巡检项
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (14)Hive调优——合并小文件
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (Python) SOAP Web Service (HTTP POST)
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (七)c52学习之旅-中断
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (顺序)容器的好伴侣 --- 容器适配器
  • (转)原始图像数据和PDF中的图像数据
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • @ConfigurationProperties注解对数据的自动封装
  • @JsonSerialize注解的使用
  • @Validated和@Valid校验参数区别
  • [ 数据结构 - C++]红黑树RBTree
  • [acwing周赛复盘] 第 69 场周赛20220917
  • [Android]RecyclerView添加HeaderView出现宽度问题
  • [C++基础]-入门知识
  • [Everyday Mathematics]20150130
  • [FROM COM张]如何解决Nios II SBTE中出现的undefined reference to `xxx'警告
  • [Gamma]阶段测试报告
  • [IDF]啥?
  • [Json.net]快速入门
  • [leetcode] Balanced Binary Tree