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


}
相关推荐
LSL666_2 小时前
5 Repository 层接口
android·运维·elasticsearch·jenkins·repository
alexhilton5 小时前
在Jetpack Compose中创建CRT屏幕效果
android·kotlin·android jetpack
2501_940094027 小时前
emu系列模拟器最新汉化版 安卓版 怀旧游戏模拟器全集附可运行游戏ROM
android·游戏·安卓·模拟器
下位子7 小时前
『OpenGL学习滤镜相机』- Day9: CameraX 基础集成
android·opengl
参宿四南河三9 小时前
Android Compose SideEffect(副作用)实例加倍详解
android·app
火柴就是我10 小时前
mmkv的 mmap 的理解
android
没有了遇见10 小时前
Android之直播宽高比和相机宽高比不支持后动态获取所支持的宽高比
android
shenshizhong11 小时前
揭开 kotlin 中协程的神秘面纱
android·kotlin
vivo高启强11 小时前
如何简单 hack agp 执行过程中的某个类
android
沐怡旸11 小时前
【底层机制】 Android ION内存分配器深度解析
android·面试