Activity

整体规划

1
2
3
4
5
BaseActivity:把onPostCreate逻辑拆分为三个方法,方便管理。
BaseCommonActivity:不同项目可以复用的逻辑,例如:启动界面等
BaseLogicActivity:本项目的通用逻辑,例如:背景颜色,全局迷你播放控制等。
BaseTitleActivity:标题相关。

BaseActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.example.testandroid.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
* 所有Activity父类
*/
public class BaseActivity extends AppCompatActivity {
/**
* 找控件
*/
protected void initViews(){

}

/**
* 设置数据
*/
protected void initDatum() {

}

/**
* 设置监听器
*/
protected void initListeners() {

}

/**
* 在onCreate方法后面调用
* @param savedInstanceState
*/
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
initViews();
initDatum();
initListeners();
}
}

最主要的三个动作就是:

initView: 初始化控件

initDatum: 初始化数据

initListeners:界面中的控件监听器设置

为什么放在 postCreate

因为onCreate里面:

1
2
3
4
5
6
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//显示当前Activity要显示那个布局
setContentView(R.layout.activity_main);
}

必须要有【设置当前Activity要显示那个布局】的哪一行代码。

然后我们的 initView 要想拿到控件,就只能在这行代码后面 findViewById 找到控件。

而 onPostCreate 就是在 OnCreate 完成之后才调用的,所以就刚好放在这里。

BaseCommonActivity

1
2
3
4
5
6
7
/**
* 通用界面逻辑
* 所有项目APP都可以用
*/
public class BaseCommonActivity extends BaseActivity{

}

BaseLogicActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 本项目APP的通用逻辑
*/
public class BaseLogicActivity extends BaseCommonActivity{
/**
* 获取界面方法
*
* @return 返回当前界面
*/
protected BaseLogicActivity getHostActivity() {
return this;
}
}

这里的getHostActivity是为了让 Activity 和 fragment 都能通过这个方法拿到自己界面的 activity 实例。

不然 activity 界面用 this,fragment 又不能用很不协调。

BaseTitleActivity

1
2
3
4
5
6
/**
* 标题相关
*/
public class BaseTitleActivity extends BaseLogicActivity{

}

Fragment

基本道理一直。

这里我们用 BaseDialogFragment举例

结构逻辑:

DialogFragment -> BasedialogFragment -> TermServiceDialogFragment

系统类 我们定制的通用控制器 开发中用到的具体Fragment

BasedialogFragment

Fragment 和 Activity 不同的点在:

Activity onCreate方法中 setContentView(R.layout.activity_main);这钟设置当前 Activity 的 xml 显示方法没有了。

Fragment 里面要我们自己封装。

如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.example.testandroid.fragment;

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.DialogFragment;

/**
* 所有 DialogFragment 对话框父类
*/
public abstract class BaseDialogFragment extends DialogFragment {

/**
* 找控件
*/
protected void initViews() {}

/**
* 设置数据
*/
protected void initDatum() {}

/**
* 设置监听器
*/
protected void initListeners() {}


/**
* 把当前 Fragment 显示的控件返回
* @param inflater The LayoutInflater object that can be used to inflate
* any views in the fragment,
* @param container If non-null, this is the parent view that the fragment's
* UI should be attached to. The fragment should not add the view itself,
* but this can be used to generate the LayoutParams of the view.
* @param savedInstanceState If non-null, this fragment is being re-constructed
* from a previous saved state as given here.
*
* @return
*/
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//获取view
View view = getLayoutView(inflater, container, savedInstanceState);

//返回view
return view;
}

/**
* 获取View
*
* @param inflater inflater
* @param container 容器
* @param savedInstanceState 保存的实例状态
* @return 生成的fragment实体
*/
protected abstract View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

/**
* View创建了
*
* @param view view
* @param savedInstanceState 保存的实例状态
*/
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initViews();
initDatum();
initListeners();
}

/**
* 给fragment增加findViewById
*
* @param id 想找的控件 id
* @param <T> 控件类型
* @return 返回控件实体
*/
public <T extends View> T findViewById(@IdRes int id) {
return getView().findViewById(id);
}


/**
* 获取界面方法
* @return
*/
protected BaseCommonActivity getHostActivity(){
return (BaseCommonActivity) getActivity();
}
}

解释:

  • onViewCreated 就是 Fragment 中的 onpostCreate
  • onCreateView 即是 Fragment 中的 onCreate 这个方法会返回 Fragment 会显示的View
  • getLayoutView :将一个 xml 文件 to 一个View 。不过是 abstract 抽象的,谁继承,谁就要实现。

这样继承了这个 BaseDialogFragment 的Fragment

都要重写 getLayoutView 这个方法来指定当前 Fragment 对应的xml 是什么

就像这样:

1
2
3
4
5
6
7
8
9
10
11
/**
* 返回当前 DialogFragment 显示的布局
* @param inflater inflater
* @param container 容器
* @param savedInstanceState 保存的实例状态
* @return
*/
@Override
protected View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dialog_term_service,container,false);
}