android 搭建框架简书,Android 基础框架搭建

从业4年搭建的框架,欢迎吐槽,提出意见修改

建议项目采用Retrofit2.0+RxJava2.0做网络请求

BaseActivity

package com.listen.feng.base;

import android.app.Activity;

import android.content.ComponentName;

import android.os.Bundle;

import android.support.annotation.IdRes;

import android.support.annotation.LayoutRes;

import android.support.v4.media.MediaBrowserCompat;

import android.support.v4.media.MediaMetadataCompat;

import android.support.v4.media.session.MediaControllerCompat;

import android.support.v4.media.session.MediaSessionCompat;

import android.support.v4.media.session.PlaybackStateCompat;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.text.TextUtils;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

import com.listen.feng.R;

import com.listen.feng.common.LoadDialog;

import com.listen.feng.play.MusicService;

import com.listen.feng.util.UtilIntent;

import com.orhanobut.logger.Logger;

import io.reactivex.disposables.CompositeDisposable;

/**

* Created by liu on 2017/9/25.

*/

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{

protected BaseActivity mActivity;

/**

* 全局的加载框对象,已经完成初始化.

*/

public LoadDialog mProgressDialog;

private TextView mTvTitle;

private ImageView mIvLeft;

private TextView mTvRight;

private ImageView mIvRight;

protected FrameLayout actionBarLayout;

protected FrameLayout errorLayout;

protected FrameLayout contentLayout;

private RelativeLayout rlRoot;

private TextView tvTitle;

private ImageButton ivBackBtn;

private Button btnRefresh;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mActivity = this;

getIntentData();

}

/**

* 设置标题

* @return

*/

protected abstract String setTitle();

/**

* 初始化View

*/

protected abstract void initView();

/**

* 初始化数据

*/

protected abstract void initData();

/**

* 获取Intent参数

*/

protected abstract void getIntentData();

/**

* 是否显示标题栏

* @return

*/

protected abstract boolean showActionBar();

protected void reload(){

};

public CompositeDisposable mDisposable = new CompositeDisposable();

@Override

public void setContentView(@LayoutRes int layoutResID) {

super.setContentView(R.layout.activity_base);

contentLayout = findViewById(R.id.frame_layout);

actionBarLayout = findViewById(R.id.actionbar);

errorLayout = findViewById(R.id.frame_error_layout);

LayoutInflater.from(this).inflate(layoutResID, contentLayout);

if(showActionBar()){

actionBarLayout.setVisibility(View.VISIBLE);

LayoutInflater.from(this).inflate(R.layout.base_actionbar, actionBarLayout);

tvTitle = actionBarLayout.findViewById(R.id.tv_title);

ivBackBtn = actionBarLayout.findViewById(R.id.iv_back_btn);

ivBackBtn.setOnClickListener(this);

if(!TextUtils.isEmpty(setTitle())) {

tvTitle.setText(setTitle());

}

}else{

actionBarLayout.setVisibility(View.GONE);

}

initView();

initData();

}

public RelativeLayout getRlRoot() {

return rlRoot;

}

/**

* 设置中间标题

* @param str

*/

protected void setCenterTitle(String str){

tvTitle.setText(str);

}

/**

* 显示右面的图标

* @param id

*/

protected void showRightImage(int id){

if(mTvRight != null){

mTvRight.setVisibility(View.GONE);

}

if(mIvRight != null){

mIvRight.setVisibility(View.VISIBLE);

mIvRight.setImageResource(id);

mIvRight.setOnClickListener(this);

}

}

/**

* 显示右面的文字

* @param str

*/

protected void showRightText(String str){

if(mIvRight != null){

mIvRight.setVisibility(View.GONE);

}

if(mTvRight != null){

mTvRight.setVisibility(View.VISIBLE);

mTvRight.setText(str);

mTvRight.setOnClickListener(this);

}

}

protected void isShowRightImage(boolean b){

if(mIvRight != null){

mIvRight.setVisibility(b ? View.VISIBLE : View.GONE);

}

}

protected void isShowRightTextView(boolean b){

if(mTvRight != null){

mTvRight.setVisibility(b ? View.VISIBLE : View.GONE);

}

}

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.iv_back_btn:

finishAnim(this);

break;

case R.id.btn_refresh:

reload();

break;

}

}

/**

* 显示

* @param resId

*/

protected void setCustomActionBar(@LayoutRes int resId){

actionBarLayout.setVisibility(View.VISIBLE);

actionBarLayout.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, actionBarLayout);

}

/**

* 显示有数据界面

*/

protected void showDataLayout(){

contentLayout.setVisibility(View.VISIBLE);

errorLayout.setVisibility(View.GONE);

}

protected void showEmptyErrorLayout(){

contentLayout.setVisibility(View.GONE);

errorLayout.setVisibility(View.VISIBLE);

errorLayout.removeAllViews();

LayoutInflater.from(mActivity).inflate(R.layout.error_empty_base_layout, errorLayout);

}

protected void showEmptyErrorLayout(@LayoutRes int resId){

contentLayout.setVisibility(View.GONE);

errorLayout.setVisibility(View.VISIBLE);

errorLayout.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, errorLayout);

}

protected void showErrorLayout(){

contentLayout.setVisibility(View.GONE);

errorLayout.setVisibility(View.VISIBLE);

errorLayout.removeAllViews();

LayoutInflater.from(mActivity).inflate(R.layout.error_network_base_layout, errorLayout);

btnRefresh = errorLayout.findViewById(R.id.btn_refresh);

btnRefresh.setOnClickListener(this);

}

protected void showErrorLayout(@LayoutRes int resId){

contentLayout.setVisibility(View.GONE);

errorLayout.setVisibility(View.VISIBLE);

errorLayout.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, errorLayout);

}

protected void showErrorLayout(@LayoutRes int resId, View.OnClickListener listener, @IdRes int... id){

contentLayout.setVisibility(View.GONE);

errorLayout.setVisibility(View.VISIBLE);

errorLayout.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, errorLayout);

for (int i = 0; i < id.length; i++){

findViewById(id[i]).setOnClickListener(listener);

}

}

@Override

protected void onStart() {

super.onStart();

}

@Override

protected void onDestroy() {

super.onDestroy();

mDisposable.dispose();

}

/**

* 显示加载框

*/

public void showProgressDialog() {

if (mProgressDialog == null) {

mProgressDialog = new LoadDialog(this);

}

//如果加载框不显示, 那么就显示加载框

if (!mProgressDialog.isShowing()) {

mProgressDialog.show();

}

}

/**

* 移除加载框.

*/

public void removeProgressDialog() {

if (mProgressDialog != null) {

if (mProgressDialog.isShowing()) {

mProgressDialog.dismiss();

}

}

}

@Override

public void onBackPressed() {

finishAnim(this);

}

/**

* 描述:Toast提示文本.

*

* @param text 文本

*/

public void showToast(String text) {

Toast.makeText(this, text, Toast.LENGTH_SHORT).show();

}

}

activity_base

android:id="@+id/rl_root"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/actionbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

>

android:id="@+id/frame_error_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/actionbar"

android:visibility="gone">

android:id="@+id/frame_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/actionbar">

BaseFragment

package com.listen.feng.base;

import android.os.Bundle;

import android.support.annotation.IdRes;

import android.support.annotation.LayoutRes;

import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.FrameLayout;

import com.listen.feng.R;

import io.reactivex.disposables.CompositeDisposable;

/**

* Created by liu on 2017/9/26.

*/

public abstract class BaseFragment extends Fragment implements View.OnClickListener{

private View baseView;

protected FrameLayout frameContext;

protected FrameLayout frameError;

protected FrameLayout frameActionBar;

protected BaseActivity mActivity;

protected View rootView;

protected CompositeDisposable mDisposable = new CompositeDisposable();

/**

* 全局的加载框对象,已经完成初始化.

*/

protected abstract void initView();

protected abstract void initData();

protected abstract int getLayoutId();

protected abstract boolean showActionBar();

protected abstract void getIntentData();

protected abstract int setActionBar();

protected void reload(){

};

@Override

public void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mActivity = (BaseActivity) getActivity();

}

@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

if(baseView == null){

baseView = inflater.inflate(R.layout.fragment_base, container,false);

frameContext = baseView.findViewById(R.id.frame_context);

frameError = baseView.findViewById(R.id.frame_error);

frameActionBar = baseView.findViewById(R.id.actionbar);

rootView = inflater.inflate(getLayoutId(), frameContext);

if(showActionBar()){

frameActionBar.setVisibility(View.VISIBLE);

inflater.inflate(setActionBar(), frameActionBar);

}else{

frameActionBar.setVisibility(View.GONE);

}

getIntentData();

initView();

}

return baseView;

}

@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

initData();

}

@Override

public void onDestroy() {

super.onDestroy();

mDisposable.dispose();

}

/**

* 显示自定义标题栏

* @param resId

*/

protected void setCustomActionBar(@LayoutRes int resId){

frameActionBar.setVisibility(View.VISIBLE);

frameActionBar.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, frameActionBar);

}

protected void showDataLayout(){

frameContext.setVisibility(View.VISIBLE);

frameError.setVisibility(View.GONE);

}

protected void showEmptyErrorLayout(){

frameContext.setVisibility(View.GONE);

frameError.setVisibility(View.VISIBLE);

frameError.removeAllViews();

LayoutInflater.from(mActivity).inflate(R.layout.error_empty_base_layout, frameError);

}

protected void showErrorLayout(){

frameContext.setVisibility(View.GONE);

frameError.setVisibility(View.VISIBLE);

frameError.removeAllViews();

LayoutInflater.from(mActivity).inflate(R.layout.error_network_base_layout, frameError);

frameError.findViewById(R.id.btn_refresh).setOnClickListener(this);

}

protected void showErrorLayout(@LayoutRes int resId){

frameContext.setVisibility(View.GONE);

frameError.setVisibility(View.VISIBLE);

frameError.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, frameError);

}

protected void showErrorLayout(@LayoutRes int resId, View.OnClickListener listener, @IdRes int... id){

frameContext.setVisibility(View.GONE);

frameError.setVisibility(View.VISIBLE);

frameError.removeAllViews();

LayoutInflater.from(mActivity).inflate(resId, frameError);

for (int i = 0; i < id.length; i++){

frameError.findViewById(id[i]).setOnClickListener(listener);

}

}

public void showToast(String text) {

mActivity.showToast(text);

}

public void showProgressDialog(){

mActivity.showProgressDialog();

}

public void removeProgressDialog(){

mActivity.removeProgressDialog();

}

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.btn_refresh:

reload();

break;

}

}

}

fragment_base

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/actionbar"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/frame_error"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/actionbar"

android:visibility="gone">

android:id="@+id/frame_context"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/actionbar">

BaseObserver

package com.listen.feng.base;

import android.text.TextUtils;

import com.listen.feng.bean.BaseResult;

import java.lang.ref.WeakReference;

import io.reactivex.observers.DisposableObserver;

/**

* Created by liu on 2017/10/31.

*/

public class BaseObserver extends DisposableObserver {

private WeakReference mActivityWeakReference;

private WeakReference mFragmentWeakReference;

//默认显示加载loading

private boolean isShow = true;

//是否显示错误页面

private boolean isShowError = true;

public BaseObserver(BaseActivity activity) {

mActivityWeakReference = new WeakReference(activity);

}

public BaseObserver(BaseActivity activity, boolean b) {

mActivityWeakReference = new WeakReference(activity);

isShow = b;

}

public BaseObserver(BaseActivity activity, boolean b, boolean isShowError) {

mActivityWeakReference = new WeakReference(activity);

isShow = b;

this.isShowError = isShowError;

}

public BaseObserver(BaseFragment fragment) {

mFragmentWeakReference = new WeakReference(fragment);

}

public BaseObserver(BaseFragment fragment, boolean b) {

mFragmentWeakReference = new WeakReference(fragment);

isShow = b;

}

public BaseObserver(BaseFragment fragment, boolean b, boolean isShowError) {

mFragmentWeakReference = new WeakReference(fragment);

isShow = b;

this.isShowError = isShowError;

}

@Override

protected void onStart() {

super.onStart();

if (mActivityWeakReference != null && mActivityWeakReference.get() != null) {

if (isShow) {

mActivityWeakReference.get().showProgressDialog();

}

}

if (mFragmentWeakReference != null && mFragmentWeakReference.get() != null) {

if (isShow) {

mFragmentWeakReference.get().showProgressDialog();

}

}

}

@Override

public void onNext(BaseResult value) {

if (value.getStatus() == 0) {

if (mActivityWeakReference != null && mActivityWeakReference.get() != null) {

mActivityWeakReference.get().showDataLayout();

}

if (mFragmentWeakReference != null && mFragmentWeakReference.get() != null) {

mFragmentWeakReference.get().showDataLayout();

}

success(value);

} else {

if (!TextUtils.isEmpty(value.getMessage())) {

if (mActivityWeakReference != null && mActivityWeakReference.get() != null) {

mActivityWeakReference.get().showToast(value.getMessage());

}

if (mFragmentWeakReference != null && mFragmentWeakReference.get() != null) {

mFragmentWeakReference.get().showToast(value.getMessage());

}

}

error(value);

}

}

public void success(BaseResult data) {

}

/**

* 错误

*

* @param

*/

public void error(BaseResult data) {

if (isShowError) {

if (mActivityWeakReference != null && mActivityWeakReference.get() != null) {

mActivityWeakReference.get().showErrorLayout();

}

if (mFragmentWeakReference != null && mFragmentWeakReference.get() != null) {

mFragmentWeakReference.get().showErrorLayout();

}

}

}

@Override

public void onError(Throwable e) {

if (mActivityWeakReference != null && mActivityWeakReference.get() != null) {

mActivityWeakReference.get().removeProgressDialog();

}

if (mFragmentWeakReference != null && mFragmentWeakReference.get() != null) {

mFragmentWeakReference.get().removeProgressDialog();

}

error(null);

}

@Override

public void onComplete() {

if (mActivityWeakReference != null && mActivityWeakReference.get() != null) {

mActivityWeakReference.get().removeProgressDialog();

}

if (mFragmentWeakReference != null && mFragmentWeakReference.get() != null) {

mFragmentWeakReference.get().removeProgressDialog();

}

}

}

例子:

BaseObserver observer = new BaseObserver>>(this){

@Override

public void success(BaseResult data) {

super.success(data);

Log.d("aaa", "aaaa");

CategoryAdapter adapter = new CategoryAdapter(getChildFragmentManager(), mActivity);

adapter.setData((List) data.getData());

mViewPager.setAdapter(adapter);

mTabLayout.setupWithViewPager(mViewPager);

}

@Override

public void error(BaseResult data) {

super.error(data);

}

};

mDisposable.add(observer);

UtilRetrofit.getInstance().create(Api.class).category().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);

你可能感兴趣的:(android,搭建框架简书)