public abstract class BaseFragment<MD extends BaseViewModel> extends Fragment implements View.OnClickListener {
protected Context context;
protected MD viewModel;
protected Application app;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
this.context = context;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (initViewModel() != null){
viewModel = (MD) new ViewModelProvider(getActivity()).get(initViewModel().getClass());
getLifecycle().addObserver(viewModel);
}
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
initData();
initListener();
}
@Override
public void onDestroy() {
if (initViewModel() != null){
getLifecycle().removeObserver(viewModel);
}
super.onDestroy();
}
protected void initOnClick(View... views){
for (View view: views) {
view.setOnClickListener(this);
}
}
protected abstract MD initViewModel();
protected abstract void initView();
protected abstract void initData();
protected abstract void initListener();
}
dataBinding:
public abstract class BaseDataBindingFragment<DB extends ViewDataBinding, MD extends BaseViewModel> extends BaseFragment<MD> {
protected DB binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
binding.setLifecycleOwner(this);
return binding.getRoot();
}
@Override
public void onDestroyView() {
if (binding != null){
binding.unbind();
binding = null;
}
super.onDestroyView();
}
protected abstract int getLayoutId();
}
使用:
public class DataFragment extends BaseDataBindingFragment<FragmentDataBinding, BaseViewModel> {
@Override
protected int getLayoutId() {
return R.layout.fragment_data;
}
@Override
protected BaseViewModel initViewModel() {
return null;
}
@Override
protected void initView() {
initOnClick(binding.dTxt);
}
@Override
protected void initData() {
}
@Override
protected void initListener() {
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.dTxt:
Toast.makeText(context, "d-123", Toast.LENGTH_SHORT).show();
break;
}
}
}
viewBinding:
public abstract class BaseViewBindingFragment<VB extends ViewBinding, MD extends BaseViewModel> extends BaseFragment<MD> {
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() {
binding = null;
super.onDestroyView();
}
}
使用:
public class BlankFragment extends BaseViewBindingFragment<FragmentBlankBinding, BaseViewModel> {
@Override
protected FragmentBlankBinding getViewBinding(@Nullable LayoutInflater inflater, @Nullable ViewGroup container) {
return FragmentBlankBinding.inflate(inflater, container, false);
}
@Override
protected BaseViewModel initViewModel() {
return null;
}
@Override
protected void initView() {
initOnClick(binding.bTxt);
}
@Override
protected void initData() {
}
@Override
protected void initListener() {
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bTxt:
Toast.makeText(context, "b-123", Toast.LENGTH_SHORT).show();
break;
}
}
}