其他分享
首页 > 其他分享> > Android dataBinding和viewBinding基类的简单封装

Android dataBinding和viewBinding基类的简单封装

作者:互联网

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());
    }

    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();
    }

    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;
    }
}

标签:Nullable,viewBinding,void,binding,public,protected,基类,dataBinding,abstract
来源: https://blog.csdn.net/lw1389074/article/details/121212740