public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
protected Context context;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBindingView();
context = this;
initData();
initListener();
}
protected void initBindingView() {}
protected void initOnClick(View... views){
for (View id: views) {
id.setOnClickListener(this);
}
}
protected abstract void initData();
protected abstract void initListener();
protected void startActivity(Class<?> cls) {
startActivity(cls, null);
}
protected void startActivity(Class<?> cls, Bundle bundle) {
Intent intent = new Intent(context, cls);
if (bundle != null) {
intent.putExtras(bundle);
}
startActivity(intent);
}
}
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
protected Context context;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initData();
initListener();
}
protected void initOnClick(View... views){
for (View id: views) {
id.setOnClickListener(this);
}
}
protected abstract void initData();
protected abstract void initListener();
}
dataBinding:
public abstract class BaseDataBindingActivity<DB extends ViewDataBinding> extends BaseActivity {
protected DB binding;
@Override
protected void initBindingView() {
super.initBindingView();
binding = DataBindingUtil.setContentView(this, getLayoutId());
}
@Override
protected void onDestroy() {
super.onDestroy();
if (binding != null){
binding.unbind();
binding = null;
}
}
protected abstract int getLayoutId();
}
public abstract class BaseDataBindingFragment<DB extends ViewDataBinding> extends BaseFragment {
protected DB binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
return binding.getRoot();
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (binding != null){
binding.unbind();
binding = null;
}
}
protected abstract int getLayoutId();
}
viewBinding:
public abstract class BaseViewBindingActivity<VB extends ViewBinding> extends BaseActivity {
protected VB binding;
@Override
protected void initBindingView() {
super.initBindingView();
binding = getViewBinding(getLayoutInflater());
setContentView(binding.getRoot());
}
protected abstract VB getViewBinding(LayoutInflater layoutInflater);
}
public abstract class BaseViewBindingFragment<VB extends ViewBinding> extends BaseFragment {
protected VB binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = getViewBinding(inflater, container);
return binding.getRoot();
}
protected abstract VB getViewBinding(@Nullable LayoutInflater inflater, @Nullable ViewGroup container);
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
|