总结1

依赖:

部分依赖看次博客:https://blog.csdn.net/qq_37454196/article/details/80285521

compile 'com.hjm:BottomTabBar:1.1.1'
    //butterknife
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    implementation rootProject.ext.dependencies["retrofit2"]
    implementation rootProject.ext.dependencies["converter-gson"]
    implementation rootProject.ext.dependencies["rxjava2"]
    implementation rootProject.ext.dependencies["rxjava2:rxandroid"]
    implementation rootProject.ext.dependencies["dagger"]
    annotationProcessor rootProject.ext.dependencies["dagger-compiler"]
    implementation rootProject.ext.dependencies["adapter-rxjava"]
    compile 'com.facebook.fresco:fresco:1.8.1'
    compile 'com.github.bumptech.glide:glide:3.6.1'
    // 支持 GIF 动图,需要添加
    implementation 'com.facebook.fresco:animated-gif:0.12.0'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.sunfusheng:marqueeview:1.3.3'
    implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
    implementation 'com.youth.banner:banner:1.4.10'
    implementation 'com.android.support:design:27+'

    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

第一步先是构建BaseActivity

public interface IBase {
    int getContentLayout();

    void inject();

    void initView(View view);
}
public interface BaseContract {
    //抽取所有Presenter共性,比如绑定,解绑
    interface BasePresenter<T extends BaseView> {
        void attchView(T view);

        void detachView();
    }

    //抽取所有View的共性,比如显示进度条和关闭进度
    interface BaseView {
        void showLoading();

        void dismissLoading();
    }
}
public class BasePresenter<T extends BaseContract.BaseView> implements BaseContract.BasePresenter<T> {
    protected T mView;

    @Override
    public void attchView(T view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        if (mView != null) {
            mView = null;
        }
    }
}

条件已经备好,接下来便是BaseActivity

public abstract class BaseActicity<T extends BaseContract.BasePresenter> extends AppCompatActivity
        implements IBase, BaseContract.BaseView {
    @Inject
    protected T mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentLayout());
        inject();
        //绑定
        if (mPresenter != null) {
            mPresenter.attchView(this);
        }
    }

    @Override
    public void initView(View view) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解绑
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }
}

MianActivity

public class MainActivity extends BaseActicity {

    private BottomTabBar bottom_tab_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        bottom_tab_bar.init(getSupportFragmentManager())
                //   .setImgSize(50,50)
                .setFontSize(12)
                .setTabPadding(4, 6, 10)
                .setChangeColor(Color.RED, Color.DKGRAY)
                .addTabItem("首页", null, OneFragment.class)
                .addTabItem("分类", null, TwoFragment.class)
                .addTabItem("发现", null, ThreeFragment.class)
                .addTabItem("购物车", null, FourFragment.class)
                .addTabItem("我的", null, FiveFragment.class)
                .isShowDivider(false)
                .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
                    @Override
                    public void onTabChange(int position, String name) {

                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void inject() {

    }

    private void initView() {
        bottom_tab_bar = (BottomTabBar) findViewById(R.id.bottom_tab_bar);
    }
}

BaseFragment

public abstract class BaseFragment<T extends BaseContract.BasePresenter> extends Fragment implements IBase,
        BaseContract.BaseView {
    @Inject
    protected T mPresenter;
    private Unbinder bind;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inject();
        if (mPresenter != null) {
            mPresenter.attchView(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (bind != null) {
            bind.unbind();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        View view = inflater.inflate(getContentLayout(), null);
        bind = ButterKnife.bind(getActivity(), view);
        initView(view);
        return view;
    }
    
    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }
}
public class TwoFragment extends BaseFragment {
    
    @Override
    public int getContentLayout() {
        return R.layout.fragment2;
    }

    @Override
    public void inject() {

    }

    @Override
    public void initView(View view) {

    }
}











阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页