1.fragment的出现本来是为了解决android适配的,但是如果使用Fragment开发app可以使软件更流畅
但是fragment的管理比较麻烦,个人建议使用Fragmentation框架对Fragment进行统一管理
1.所有的Fragment都要集成SurpportFragment
/** * Author:DoctorWei * Time:2018/11/3 11:16 * Description:基类Fragment * email:[email protected] */ public abstract class BaseFragment extends SupportFragment { private BaseActivity mActivity; @Override public void onAttach(Context context) { super.onAttach(context); mActivity= (BaseActivity) context; } @Override public void onDetach() { super.onDetach(); mActivity=null; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(getLayoutId(),container,false); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); init(view); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initData(savedInstanceState); } protected abstract int getLayoutId(); protected abstract void init(View view); protected abstract void initData(Bundle savedInstanceState);
2.所有的Activity都要继承SurpportActivity
/** * Author:DoctorWei * Time:2018/11/2 18:33 * Description:定义一个Activity的基类 用于加载 * email:[email protected] */ public abstract class BaseActivity extends SupportActivity { @Override public void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutId()); initView(); initData(savedInstanceState); } protected abstract void initData(Bundle savedInstanceState); protected abstract void initView(); protected abstract int getLayoutId(); //写一个全局的跳转 public void openActivityWithIntent(@NonNull Class activity,@NonNull Bundle bundle){ Intent intent=new Intent(this,activity); if (bundle!=null) { intent.putExtras(bundle); } startActivity(intent); } public Bundle getIntentBundle(){ Bundle bundle = getIntent().getExtras(); return bundle; } }
3.在Acitivity调用Fragment
public class MainActivity extends BaseActivity implements View.OnClickListener { private ImageView mAppIvTabMain; /** * 首页 */ private TextView mAppTvTabMain; private RelativeLayout mAppRlTabMain; private ImageView mAppIvTabBeauty; /** * 美女 */ private TextView mAppTvTabBeauty; private RelativeLayout mAppRlTabBeauty; private ImageView mAppIvTabFunction; /** * 功能 */ private TextView mAppTvTabFunction; private RelativeLayout mAppRlTabFunction; private ImageView mAppIvTabVideo; /** * 消息 */ private TextView mAppTvTabVideo; private RelativeLayout mAppRlTabVideo; private ImageView mAppIvTabSetting; /** * 我的 */ private TextView mAppTvTabSetting; private RelativeLayout mAppRlTabSetting; private LinearLayout mLlBottom; //存储imageView和TextView的集合 ImageView[] imageViews = new ImageView[5]; TextView[] textViews = new TextView[5]; @Override protected void initData(Bundle savedInstanceState) { if (savedInstanceState == null) { loadRootFragment(R.id.fl_container, HomeFragment.newInstance()); //activity初始加载HomeFragment } } @Override protected void initView() { mAppIvTabMain = (ImageView) findViewById(R.id.app_iv_tab_main); mAppTvTabMain = (TextView) findViewById(R.id.app_tv_tab_main); imageViews[0] = mAppIvTabMain; textViews[0] = mAppTvTabMain; mAppRlTabMain = (RelativeLayout) findViewById(R.id.app_rl_tab_main); mAppRlTabMain.setOnClickListener(this); mAppIvTabBeauty = (ImageView) findViewById(R.id.app_iv_tab_beauty); mAppTvTabBeauty = (TextView) findViewById(R.id.app_tv_tab_beauty); imageViews[1] = mAppIvTabBeauty; textViews[1] = mAppTvTabBeauty; mAppRlTabBeauty = (RelativeLayout) findViewById(R.id.app_rl_tab_beauty); mAppRlTabBeauty.setOnClickListener(this); mAppIvTabFunction = (ImageView) findViewById(R.id.app_iv_tab_function); mAppTvTabFunction = (TextView) findViewById(R.id.app_tv_tab_function); imageViews[2] = mAppIvTabFunction; textViews[2] = mAppTvTabFunction; mAppRlTabFunction = (RelativeLayout) findViewById(R.id.app_rl_tab_function); mAppRlTabFunction.setOnClickListener(this); mAppIvTabVideo = (ImageView) findViewById(R.id.app_iv_tab_video); mAppTvTabVideo = (TextView) findViewById(R.id.app_tv_tab_video); imageViews[3] = mAppIvTabVideo; textViews[3] = mAppTvTabVideo; mAppRlTabVideo = (RelativeLayout) findViewById(R.id.app_rl_tab_video); mAppRlTabVideo.setOnClickListener(this); mAppIvTabSetting = (ImageView) findViewById(R.id.app_iv_tab_setting); mAppTvTabSetting = (TextView) findViewById(R.id.app_tv_tab_setting); imageViews[4] = mAppIvTabSetting; textViews[4] = mAppTvTabSetting; mAppRlTabSetting = (RelativeLayout) findViewById(R.id.app_rl_tab_setting); mAppRlTabSetting.setOnClickListener(this); mLlBottom = (LinearLayout) findViewById(R.id.ll_bottom); } @Override protected int getLayoutId() { return R.layout.activity_main; } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.app_rl_tab_main: HomeFragment homeFragment=findFragment(HomeFragment.class); Bundle newBundle=new Bundle(); newBundle.putString(Extra.FRAGMENT_FROM,"主页来自:"+getTopFragment().getClass()); start(homeFragment, SupportFragment.SINGLETASK); break; case R.id.app_rl_tab_beauty: BeautyFragment beautyFragment=findFragment(BeautyFragment.class); if (beautyFragment==null){ popTo(HomeFragment.class, false, new Runnable() { @Override public void run() { start(BeautyFragment.newInstance()); } }); }else{ start(beautyFragment,SupportFragment.SINGLETASK); } break; case R.id.app_rl_tab_function: FunctionFragment functionFragment=findFragment(FunctionFragment.class); if (functionFragment==null){ popTo(FunctionFragment.class, false, new Runnable() { @Override public void run() { start(FunctionFragment.newInstance()); } }); }else{ start(functionFragment,SupportFragment.SINGLETASK); } break; case R.id.app_rl_tab_video://消息 MessageFragment messageFragment=findFragment(MessageFragment.class); if (messageFragment==null){ popTo(MessageFragment.class, false, new Runnable() { @Override public void run() { start(MessageFragment.newInstance()); } }); }else{ start(messageFragment,SupportFragment.SINGLETASK); } break; case R.id.app_rl_tab_setting: SettingFragment settingFragment=findFragment(SettingFragment.class); if (settingFragment==null){ popTo(SettingFragment.class, false, new Runnable() { @Override public void run() { start(SettingFragment.newInstance()); } }); }else{ start(settingFragment,SettingFragment.SINGLETASK); } break; } } }