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

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


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setData(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View fragmentView  = inflater.inflate(setContentLayout(),container,false);
        return fragmentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 控件
        setControls(view);
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public 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;

    @Override
    protected void setData(Bundle savedInstanceState) {

    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected 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">


    <TextView
        android: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.FragmentContainerView
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/homeBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="home"/>
        <Button
            android:id="@+id/shoppingBtn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="shopping"/>
        <Button
            android: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;
    // fragment
    private HomeFragment homeFragment = new HomeFragment();
    private ShoppingFragment shoppingFragment = new ShoppingFragment();
    private MyFragment myFragment = new MyFragment();


    @Override
    public void setData(Bundle savedInstanceState) {
        // 竖屏
        setScreenPortrait(true);
    }

    @Override
    public int setContentLayout() {
        return R.layout.main_layout;
    }

    @Override
    public 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() {
        @Override
        public void onClick(View view) {
            // 必须写下面的三条语句
            transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment_container_view,homeFragment);
            transaction.commit();
        }
    };

    View.OnClickListener shopClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 必须写下面的三条语句
            transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment_container_view,shoppingFragment);
            transaction.commit();
        }
    };

    View.OnClickListener myClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 必须写下面的三条语句
            transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment_container_view,myFragment);
            transaction.commit();
        }
    };


}


http://www.kler.cn/news/234783.html

相关文章:

  • Linux和Windows文件共享实现方式
  • 2024美赛C题保姆级分析完整思路代码数据教学
  • FL Studio版本升级-FL Studio怎么升级-FL Studio升级方案
  • 多进程服务器和多线程服务器
  • Vue - 快速入门(一)
  • 腾讯云4核8G12M轻量应用服务器性能够用吗?支持多少人?
  • sheng的学习笔记-部署-目录
  • 嵌入式电子产品开发感悟!
  • 深入学习Pandas:数据连接、合并、加入、添加、重构函数的全面指南【第72篇—python:数据连接】
  • C++局部变量与全局变量
  • ChatGPT高效提问—prompt实践
  • 修改SpringBoot中默认依赖版本
  • 快速手动完成 VS 编写脚本自动化:如何选取最高效的工作方式?
  • MySQL(基础)
  • 集群及LVS简介、LVSNAT模式原理、LVSNAT模式配置、LVSDR模式原理、LVSDR模式配置、LVS错误排查
  • HiveSQL——条件判断语句嵌套windows子句的应用
  • UR10+gazebo+moveit吸盘抓取搬运demo
  • 数据分析基础之《pandas(8)—综合案例》
  • 解决Spring Boot Configuration Annotation Processor not configured的问题
  • KY139 毕业bg
  • 协议-TCP协议-基础概念04-可能发生丢包的位置-linux配置项梳理(TCP连接的建立和断开、收发包过程)
  • 更换商品图片日期JSON格式报错 - 序列化与反序列化日期格式设置
  • springcloud分布式架构网上商城源码和论文
  • Linux笔记之Docker进行镜像备份与迁移
  • openresty (nginx)快速开始
  • 【数学建模】【2024年】【第40届】【MCM/ICM】【F题 减少非法野生动物贸易】【解题思路】
  • 分享76个时间日期JS特效,总有一款适合您
  • Redis进阶(二):事务
  • 巴尔加瓦算法图解:算法运用(上)
  • Java安全 CC链1分析(Lazymap类)