直接上代码:
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布局代码:
使用:
(1) main_layout.xml代码
(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();}};}