reference
BaseActivity
public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
/**
* 获取布局文件ID
*
* @return 返回布局ID
*/
protected abstract int getContentViewId();
/**
* 初始化控件
*/
protected void initView() {
}
/**
* 初始化数据
*/
protected void initData() {
}
/**
* 初始化设备
*/
protected void initDevice() {
}
/**
* 初始化监听器
*/
protected void addListeners() {
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
//绑定ButterKnife
ButterKnife.bind(this);
init();
}
/**
* 初始化
*/
private void init(){
initView();
initData();
initDevice();
addListeners();
}
@Override
public Context getContext() {
return this;
}
}
BaseFragment
public abstract class BaseFragment extends Fragment implements IBaseView {
private Unbinder unbinder;
/**
* 获取布局文件ID
* @return 返回布局ID
*/
protected abstract int getContentViewId();
/**
* 初始化控件
*/
protected void initView() {
}
/**
* 初始化数据
*/
protected void initData() {
}
/**
* 初始化设备
*/
protected void initDevice() {
}
/**
* 初始化监听器
*/
protected void addListeners() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(getContentViewId(), container, false);
unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
/**
* 初始化
*/
private void init(){
initView();
initData();
initDevice();
addListeners();
}
@Override
public Context getContext() {
return getActivity();
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
IBaseView
public interface IBaseView {
Context getContext();
}