Android简单抽取基类问题

Android简单抽取Activity和Fragment基类

  1. Activity:
public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        initView();
        initData();
        bindEvent();

    }
    //绑定布局的方法
    public 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);
    }

}
  1. Fragment:
public abstract class BaseFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(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();
    }
    //绑定视图
    protected abstract int bindLayout();
    //绑定组件
    protected abstract void initView();
    //操作数据
    protected abstract void initData();
    //设置监听
    protected abstract void bindEvent();

    protected  T bindView(int resId){
        return (T) getView().findViewById(resId);
    }
    //绑定某个特定的视图
    protected  T bindView(View view ,int resId){
        return (T) view.findViewById(resId);
    }
}

你可能感兴趣的:(Android学习之路代码笔记)