97 lines
2.5 KiB
Java
97 lines
2.5 KiB
Java
package com.xscm.moduleutil.base;
|
|
|
|
import android.os.Bundle;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.databinding.DataBindingUtil;
|
|
import androidx.databinding.ViewDataBinding;
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import com.xscm.moduleutil.activity.BaseAppCompatActivity;
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
public abstract class BaseFragment<VDB extends ViewDataBinding> extends Fragment {
|
|
protected VDB mBinding;
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
EventBus.getDefault().unregister(this);
|
|
super.onDestroy();
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
mBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
|
|
mBinding.setLifecycleOwner(this);
|
|
return mBinding.getRoot();
|
|
}
|
|
|
|
@Override
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
if (getArguments() != null) {
|
|
initArgs(getArguments());
|
|
}
|
|
initView();
|
|
initData();
|
|
initListener();
|
|
EventBus.getDefault().register(this);
|
|
}
|
|
|
|
|
|
@Subscribe (threadMode = ThreadMode.MAIN)
|
|
public void onEvent(Object event) {
|
|
|
|
}
|
|
public void initArgs(Bundle arguments) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onDestroyView() {
|
|
if (mBinding != null) {
|
|
mBinding.unbind();
|
|
}
|
|
if (EventBus.getDefault().isRegistered(this)) {
|
|
EventBus.getDefault().unregister(this);
|
|
}
|
|
super.onDestroyView();
|
|
}
|
|
|
|
protected void initListener() {
|
|
|
|
}
|
|
|
|
|
|
protected abstract void initData();
|
|
|
|
protected abstract void initView();
|
|
|
|
protected abstract int getLayoutId();
|
|
|
|
|
|
public void showLoading(String content) {
|
|
if (!isAdded() || getActivity() == null) {
|
|
return;
|
|
}
|
|
if (getActivity() instanceof BaseAppCompatActivity) {
|
|
((BaseAppCompatActivity) getActivity()).showLoading(content);
|
|
}
|
|
}
|
|
|
|
public void disLoading() {
|
|
if (getActivity() instanceof BaseAppCompatActivity) {
|
|
((BaseAppCompatActivity) getActivity()).disLoading();
|
|
}
|
|
}
|
|
|
|
}
|