Android dataBinding和viewBinding简单封装Fragment

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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值