先看看Activity的基类抽取
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
/**
* @Auther: 殒命星
* @Date: 2019/1/19 10:33:${}
* @Description:
*/
public abstract class BaseActivity extends FragmentActivity {//继承这个地方可以继承Activity也可以继承FragmentActivity
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayout());
initView();
initData();
bindEvent();
}
protected abstract int bindLayout();
protected abstract void initView();
protected abstract void initData();
protected abstract void bindEvent();
protected <T extends View> T bindView(int resId){
return (T) findViewById(resId);
}
}
Fragment的基类抽取
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @Auther: 殒命星
* @Date: 2019/1/19 11:36:${}
* @Description:
*/
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(bindLayout(), container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
initData();
bindEvent();
}
//绑定xml
protected abstract int bindLayout();
//获取控件
protected abstract void initView();
//操作数据
protected abstract void initData();
//监听
protected abstract void bindEvent();
//找控件
protected <T extends View> T bindView(int resId){
return (T) getView().findViewById(resId);
}
}