基类封装Activity和Fargment,封装RetrofitUtils工具类

本文介绍了如何在Android开发中进行组件化封装,包括使用ButterKnife的依赖,Retrofit的依赖,以及如何创建BasePresenter、BaseActivity、BaseFragment的基础类。此外,还详细讲解了如何封装RetrofitUtils工具类,以简化网络请求操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

butterknife需要的依赖

implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
implementation 'com.github.tongchexinfeitao:QRCodeLibrary:1.0'

defaultConfig里
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

app的build.gradle最上面
apply plugin: 'com.jakewharton.butterknife'

根目录repositories里
mavenCentral()
maven { url 'https://jitpack.io' }

dependencies里
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.1'

retrofit需要的依赖

//retrofit核心库
implementation 'com.squareup.retrofit2:retrofit:2.6.2'//retrofit辅助,解析gson的库
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.2'

//okhttp日志拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'

抽基类封装BasePresenter

public abstract class BasePresenter<M extends BaseModel,V extends BaseView>{

    public M model;
    public WeakReference<V> weakReference;


    public BasePresenter(){
        model=initModel();
    }

    public void attch(V v){
        weakReference=new WeakReference<>(v);
    }

    public void deatch(){
        if (weakReference != null) {
            weakReference.clear();
            weakReference=null;
        }
    }

    protected abstract M initModel();

    public V getView(){
        return weakReference.get();
    }

}

基类封装BaseActivity

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView{

    public P presenter;
    public Unbinder unbinder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(LayoutId());

        unbinder= ButterKnife.bind(this);
        presenter=initPresenter();
        if (presenter != null) {
            presenter.attch(this);
        }

        initView();

        initData();
    }

    protected abstract P initPresenter();

    protected abstract void initData();

    protected abstract void initView();

    protected abstract int LayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (presenter != null) {
            presenter.deatch();
        }

        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}

基类封装BaseFragment

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {

    public P presenter;
    public Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View inflate = View.inflate(getActivity(), LayoutId(), null);

        unbinder= ButterKnife.bind(this,inflate);

        presenter=initPresenter();
        if (presenter != null) {
            presenter.attch(this);
        }

        initView(inflate);

        return inflate;
    }

    protected abstract void initView(View inflate);

    protected abstract P initPresenter();

    protected abstract int LayoutId();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    protected abstract void initData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.deatch();
        }

        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}

封装RetrofitUtils工具类

public class RetrofitUtils {

    private static RetrofitUtils retrofitUtils;
    private final Retrofit retrofit;

    private RetrofitUtils(){

        OkHttpClient okHttpClient=new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    public static RetrofitUtils getInstance() {

        if (retrofitUtils==null){
            synchronized (RetrofitUtils.class){
                if (retrofitUtils==null){
                    retrofitUtils=new RetrofitUtils();
                }
            }
        }

        return retrofitUtils;
    }

    public<T> T createService(Class<T> tClass){
        T t=retrofit.create(tClass);

        return t;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值