欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)加載狀態(tài)視圖切換效果

 更新時間:2020年07月22日 10:09:02   作者:楊充  
這篇文章主要為大家詳細介紹了Android實現(xiàn)加載狀態(tài)視圖切換效果的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

關(guān)于Android加載狀態(tài)視圖切換,具體內(nèi)容如下

1.關(guān)于Android界面切換狀態(tài)的介紹

怎樣切換界面狀態(tài)?有些界面想定制自定義狀態(tài)?狀態(tài)如何添加點擊事件?下面就為解決這些問題!
內(nèi)容界面
加載數(shù)據(jù)中
加載數(shù)據(jù)錯誤
加載后沒有數(shù)據(jù)
沒有網(wǎng)絡(luò)

2.思路轉(zhuǎn)變,抽取分離類管理幾種狀態(tài)

以前做法:

直接把這些界面include到main界面中,然后動態(tài)去切換界面,后來發(fā)現(xiàn)這樣處理不容易復用到其他項目中,而且在activity中處理這些狀態(tài)的顯示和隱藏比較亂
利用子類繼承父類特性,在父類中寫切換狀態(tài),但有些界面如果沒有繼承父類,又該如何處理

現(xiàn)在做法:

讓View狀態(tài)的切換和Activity徹底分離開,必須把這些狀態(tài)View都封裝到一個管理類中,然后暴露出幾個方法來實現(xiàn)View之間的切換。
在不同的項目中可以需要的View也不一樣,所以考慮把管理類設(shè)計成builder模式來自由的添加需要的狀態(tài)View

3.關(guān)于該狀態(tài)切換工具優(yōu)點分析

可以自由切換內(nèi)容,空數(shù)據(jù),異常錯誤,加載,網(wǎng)絡(luò)錯誤等5種狀態(tài)
父類BaseActivity直接暴露5中狀態(tài),方便子類統(tǒng)一管理狀態(tài)切換

/**
* ================================================
* 作 者:楊充
* 版 本:1.0
* 創(chuàng)建日期:2017/7/6
* 描 述:抽取類
* 修訂歷史:
* ================================================
*/
public abstract class BaseActivity extends AppCompatActivity {

 protected StatusLayoutManager statusLayoutManager;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_base_view);
  initStatusLayout();
  initBaseView();
  initToolBar();
  initView();
 }

 protected abstract void initStatusLayout();

 protected abstract void initView();

 /**
 * 獲取到布局
 */
 private void initBaseView() {
  LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
  ll_main.addView(statusLayoutManager.getRootLayout());
 }

 //正常展示數(shù)據(jù)狀態(tài)
 protected void showContent() {
  statusLayoutManager.showContent();
 }

 //加載數(shù)據(jù)為空時狀態(tài)
 protected void showEmptyData() {
  statusLayoutManager.showEmptyData();
 }

 //加載數(shù)據(jù)錯誤時狀態(tài)
 protected void showError() {
  statusLayoutManager.showError();
 }

 //網(wǎng)絡(luò)錯誤時狀態(tài)
 protected void showNetWorkError() {
  statusLayoutManager.showNetWorkError();
 }

 //正在加載中狀態(tài)
 protected void showLoading() {
  statusLayoutManager.showLoading();
 }
}

當狀態(tài)是加載數(shù)據(jù)失敗時,點擊可以刷新數(shù)據(jù);當狀態(tài)是無網(wǎng)絡(luò)時,點擊可以設(shè)置網(wǎng)絡(luò)

/**
* 點擊重新刷新
*/
private void initErrorDataView() {
 statusLayoutManager.showError();
 LinearLayout ll_error_data = (LinearLayout) findViewById(R.id.ll_error_data);
 ll_error_data.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
   initData();
   adapter.notifyDataSetChanged();
   showContent();
  }
 });
}

/**
* 點擊設(shè)置網(wǎng)絡(luò)
*/
private void initSettingNetwork() {
 statusLayoutManager.showNetWorkError();
 LinearLayout ll_set_network = (LinearLayout) findViewById(R.id.ll_set_network);
 ll_set_network.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
   Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
   startActivity(intent);
  }
 });
}

倘若有些頁面想定制狀態(tài)布局,也可以自由實現(xiàn),很簡單:

/**
* 自定義加載數(shù)據(jù)為空時的狀態(tài)布局
*/
private void initEmptyDataView() {
 statusLayoutManager.showEmptyData();
 //此處是自己定義的狀態(tài)布局
 **statusLayoutManager.showLayoutEmptyData(R.layout.activity_emptydata);**
 LinearLayout ll_empty_data = (LinearLayout) findViewById(R.id.ll_empty_data);
 ll_empty_data.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
   initData();
   adapter.notifyDataSetChanged();
   showContent();
  }
 });
}

4.如何實現(xiàn)的步驟

1).先看看狀態(tài)管理器類【builder建造者模式】

loadingLayoutResId和contentLayoutResId代表等待加載和顯示內(nèi)容的xml文件
幾種異常狀態(tài)要用ViewStub,因為在界面狀態(tài)切換中l(wèi)oading和內(nèi)容View都是一直需要加載顯示的,但是其他的3個只有在沒數(shù)據(jù)或者網(wǎng)絡(luò)異常的情況下才會加載顯示,所以用ViewStub來加載他們可以提高性能。

public class StateLayoutManager {

 final Context context;
 final ViewStub netWorkErrorVs;
 final int netWorkErrorRetryViewId;
 final ViewStub emptyDataVs;
 final int emptyDataRetryViewId;
 final ViewStub errorVs;
 final int errorRetryViewId;
 final int loadingLayoutResId;
 final int contentLayoutResId;
 final int retryViewId;
 final int emptyDataIconImageId;
 final int emptyDataTextTipId;
 final int errorIconImageId;
 final int errorTextTipId;
 final VLayout errorLayout;
 final VLayout emptyDataLayout;

 final RootFrameLayout rootFrameLayout;
 final OnShowHideViewListener onShowHideViewListener;
 final OnRetryListener onRetryListener;

 public StateLayoutManager(Builder builder) {
  this.context = builder.context;
  this.loadingLayoutResId = builder.loadingLayoutResId;
  this.netWorkErrorVs = builder.netWorkErrorVs;
  this.netWorkErrorRetryViewId = builder.netWorkErrorRetryViewId;
  this.emptyDataVs = builder.emptyDataVs;
  this.emptyDataRetryViewId = builder.emptyDataRetryViewId;
  this.errorVs = builder.errorVs;
  this.errorRetryViewId = builder.errorRetryViewId;
  this.contentLayoutResId = builder.contentLayoutResId;
  this.onShowHideViewListener = builder.onShowHideViewListener;
  this.retryViewId = builder.retryViewId;
  this.onRetryListener = builder.onRetryListener;
  this.emptyDataIconImageId = builder.emptyDataIconImageId;
  this.emptyDataTextTipId = builder.emptyDataTextTipId;
  this.errorIconImageId = builder.errorIconImageId;
  this.errorTextTipId = builder.errorTextTipId;
  this.errorLayout = builder.errorLayout;
  this.emptyDataLayout = builder.emptyDataLayout;

  rootFrameLayout = new RootFrameLayout(this.context);
  ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  rootFrameLayout.setLayoutParams(layoutParams);

  rootFrameLayout.setStatusLayoutManager(this);
 }

 /**
 * 顯示loading
 */
 public void showLoading() {
  rootFrameLayout.showLoading();
 }

 /**
 * 顯示內(nèi)容
 */
 public void showContent() {
  rootFrameLayout.showContent();
 }

 /**
 * 顯示空數(shù)據(jù)
 */
 public void showEmptyData(int iconImage, String textTip) {
  rootFrameLayout.showEmptyData(iconImage, textTip);
 }

 /**
 * 顯示空數(shù)據(jù)
 */
 public void showEmptyData() {
  showEmptyData(0, "");
 }

 /**
 * 顯示空數(shù)據(jù)
 */
 public void showLayoutEmptyData(Object... objects) {
  rootFrameLayout.showLayoutEmptyData(objects);
 }

 /**
 * 顯示網(wǎng)絡(luò)異常
 */
 public void showNetWorkError() {
  rootFrameLayout.showNetWorkError();
 }

 /**
 * 顯示異常
 */
 public void showError(int iconImage, String textTip) {
  rootFrameLayout.showError(iconImage, textTip);
 }

 /**
 * 顯示異常
 */
 public void showError() {
  showError(0, "");
 }

 public void showLayoutError(Object... objects) {
  rootFrameLayout.showLayoutError(objects);
 }

 /**
 * 得到root 布局
 */
 public View getRootLayout() {
  return rootFrameLayout;
 }

 public static final class Builder {

  private Context context;
  private int loadingLayoutResId;
  private int contentLayoutResId;
  private ViewStub netWorkErrorVs;
  private int netWorkErrorRetryViewId;
  private ViewStub emptyDataVs;
  private int emptyDataRetryViewId;
  private ViewStub errorVs;
  private int errorRetryViewId;
  private int retryViewId;
  private int emptyDataIconImageId;
  private int emptyDataTextTipId;
  private int errorIconImageId;
  private int errorTextTipId;
  private VLayout errorLayout;
  private VLayout emptyDataLayout;
  private OnShowHideViewListener onShowHideViewListener;
  private OnRetryListener onRetryListener;

  public Builder(Context context) {
   this.context = context;
  }

  /**
  * 自定義加載布局
  */
  public Builder loadingView(@LayoutRes int loadingLayoutResId) {
   this.loadingLayoutResId = loadingLayoutResId;
   return this;
  }

  /**
  * 自定義網(wǎng)絡(luò)錯誤布局
  */
  public Builder netWorkErrorView(@LayoutRes int newWorkErrorId) {
   netWorkErrorVs = new ViewStub(context);
   netWorkErrorVs.setLayoutResource(newWorkErrorId);
   return this;
  }

  /**
  * 自定義加載空數(shù)據(jù)布局
  */
  public Builder emptyDataView(@LayoutRes int noDataViewId) {
   emptyDataVs = new ViewStub(context);
   emptyDataVs.setLayoutResource(noDataViewId);
   return this;
  }

  /**
  * 自定義加載錯誤布局
  */
  public Builder errorView(@LayoutRes int errorViewId) {
   errorVs = new ViewStub(context);
   errorVs.setLayoutResource(errorViewId);
   return this;
  }

  /**
  * 自定義加載內(nèi)容正常布局
  */
  public Builder contentView(@LayoutRes int contentLayoutResId) {
   this.contentLayoutResId = contentLayoutResId;
   return this;
  }

  public Builder errorLayout(VLayout errorLayout) {
   this.errorLayout = errorLayout;
   this.errorVs = errorLayout.getLayoutVs();
   return this;
  }

  public Builder emptyDataLayout(VLayout emptyDataLayout) {
   this.emptyDataLayout = emptyDataLayout;
   this.emptyDataVs = emptyDataLayout.getLayoutVs();
   return this;
  }

  public Builder netWorkErrorRetryViewId(int netWorkErrorRetryViewId) {
   this.netWorkErrorRetryViewId = netWorkErrorRetryViewId;
   return this;
  }

  public Builder emptyDataRetryViewId(int emptyDataRetryViewId) {
   this.emptyDataRetryViewId = emptyDataRetryViewId;
   return this;
  }

  public Builder errorRetryViewId(int errorRetryViewId) {
   this.errorRetryViewId = errorRetryViewId;
   return this;
  }

  public Builder retryViewId(int retryViewId) {
   this.retryViewId = retryViewId;
   return this;
  }

  public Builder emptyDataIconImageId(int emptyDataIconImageId) {
   this.emptyDataIconImageId = emptyDataIconImageId;
   return this;
  }

  public Builder emptyDataTextTipId(int emptyDataTextTipId) {
   this.emptyDataTextTipId = emptyDataTextTipId;
   return this;
  }

  public Builder errorIconImageId(int errorIconImageId) {
   this.errorIconImageId = errorIconImageId;
   return this;
  }

  public Builder errorTextTipId(int errorTextTipId) {
   this.errorTextTipId = errorTextTipId;
   return this;
  }

  public Builder onShowHideViewListener(OnShowHideViewListener onShowHideViewListener) {
   this.onShowHideViewListener = onShowHideViewListener;
   return this;
  }

  public Builder onRetryListener(OnRetryListener onRetryListener) {
   this.onRetryListener = onRetryListener;
   return this;
  }

  public StateLayoutManager build() {
   return new StateLayoutManager(this);
  }
 }

 public static Builder newBuilder(Context context) {
  return new Builder(context);
 }
}

2).大約5種狀態(tài),如何管理這些狀態(tài)?添加到集合中,Android中選用SparseArray比HashMap更省內(nèi)存,在某些條件下性能更好,主要是因為它避免了對key的自動裝箱(int轉(zhuǎn)為Integer類型),它內(nèi)部則是通過兩個數(shù)組來進行數(shù)據(jù)存儲的,一個存儲key,另外一個存儲value,為了優(yōu)化性能,它內(nèi)部對數(shù)據(jù)還采取了壓縮的方式來表示稀疏數(shù)組的數(shù)據(jù),從而節(jié)約內(nèi)存空間

/**存放布局集合 */
private SparseArray<View> layoutSparseArray = new SparseArray();
/**將布局添加到集合 */
……
private void addLayoutResId(@LayoutRes int layoutResId, int id) {
 View resView = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResId, null);
 **layoutSparseArray.put(id, resView);**
 addView(resView);
}

3).當顯示某個布局時,調(diào)用的方法如下

方法里面通過id判斷來執(zhí)行不同的代碼,首先判斷ViewStub是否為空,如果為空就代表沒有添加這個View就返回false,不為空就加載View并且添加到集合當中,然后調(diào)用showHideViewById方法顯示隱藏View,retryLoad方法是給重試按鈕添加事件

/**
* 顯示loading
*/
public void showLoading() {
 if (layoutSparseArray.get(LAYOUT_LOADING_ID) != null)
  **showHideViewById**(LAYOUT_LOADING_ID);
}

/**
* 顯示內(nèi)容
*/
public void showContent() {
 if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null)
  **showHideViewById**(LAYOUT_CONTENT_ID);
}

/**
* 顯示空數(shù)據(jù)
*/
public void showEmptyData(int iconImage, String textTip) {
 if (**inflateLayout**(LAYOUT_EMPTYDATA_ID)) {
  showHideViewById(LAYOUT_EMPTYDATA_ID);
  emptyDataViewAddData(iconImage, textTip);
 }
}

/**
* 顯示網(wǎng)絡(luò)異常
*/
public void showNetWorkError() {
 if (**inflateLayout**(LAYOUT_NETWORK_ERROR_ID))
  showHideViewById(LAYOUT_NETWORK_ERROR_ID);
}

/**
* 顯示異常
*/
public void showError(int iconImage, String textTip) {
 if (**inflateLayout**(LAYOUT_ERROR_ID)) {
  showHideViewById(LAYOUT_ERROR_ID);
  errorViewAddData(iconImage, textTip);
 }
}

//調(diào)用inflateLayout方法,方法返回true然后調(diào)用showHideViewById方法
private boolean inflateLayout(int id) {
 boolean isShow = true;
 if (layoutSparseArray.get(id) != null) return isShow;
 switch (id) {
  case LAYOUT_NETWORK_ERROR_ID:
   if (mStatusLayoutManager.netWorkErrorVs != null) {
    View view = mStatusLayoutManager.netWorkErrorVs.inflate();
    retryLoad(view, mStatusLayoutManager.netWorkErrorRetryViewId);
    layoutSparseArray.put(id, view);
    isShow = true;
   } else {
    isShow = false;
   }
   break;
  case LAYOUT_ERROR_ID:
   if (mStatusLayoutManager.errorVs != null) {
    View view = mStatusLayoutManager.errorVs.inflate();
    if (mStatusLayoutManager.errorLayout != null) mStatusLayoutManager.errorLayout.setView(view);
    retryLoad(view, mStatusLayoutManager.errorRetryViewId);
    layoutSparseArray.put(id, view);
    isShow = true;
   } else {
    isShow = false;
   }
   break;
  case LAYOUT_EMPTYDATA_ID:
   if (mStatusLayoutManager.emptyDataVs != null) {
    View view = mStatusLayoutManager.emptyDataVs.inflate();
    if (mStatusLayoutManager.emptyDataLayout != null) mStatusLayoutManager.emptyDataLayout.setView(view);
    retryLoad(view, mStatusLayoutManager.emptyDataRetryViewId);
    layoutSparseArray.put(id, view);
    isShow = true;
   } else {
    isShow = false;
   }
   break;
 }
 return isShow;
}

4).然后在根據(jù)id隱藏布局

通過id找到需要顯示的View并且顯示它,隱藏其他View,如果顯示隱藏監(jiān)聽事件不為空,就分別調(diào)用它的顯示和隱藏方法

/**
* 根據(jù)ID顯示隱藏布局
* @param id
*/
private void showHideViewById(int id) {
 for (int i = 0; i < layoutSparseArray.size(); i++) {
  int key = layoutSparseArray.keyAt(i);
  View valueView = layoutSparseArray.valueAt(i);
  //顯示該view
  if(key == id) {
   valueView.setVisibility(View.VISIBLE);
   if(mStatusLayoutManager.onShowHideViewListener != null) mStatusLayoutManager.onShowHideViewListener.onShowView(valueView, key);
  } else {
   if(valueView.getVisibility() != View.GONE) {
    valueView.setVisibility(View.GONE);
    if(mStatusLayoutManager.onShowHideViewListener != null) mStatusLayoutManager.onShowHideViewListener.onHideView(valueView, key);
   }
  }
 }
}

5).最后看看重新加載方法

/**
* 重試加載
*/
private void retryLoad(View view, int id) {
 View retryView = view.findViewById(mStatusLayoutManager.retryViewId != 0 ? mStatusLayoutManager.retryViewId : id);
 if (retryView == null || mStatusLayoutManager.onRetryListener == null) return;
 retryView.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
   mStatusLayoutManager.onRetryListener.onRetry();
  }
 });
}

5.使用方法介紹

1).直接在Activity中添加代碼

@Override
protected void initStatusLayout() {
 statusLayoutManager = StateLayoutManager.newBuilder(this)
   .contentView(R.layout.activity_content_data)
   .emptyDataView(R.layout.activity_empty_data)
   .errorView(R.layout.activity_error_data)
   .loadingView(R.layout.activity_loading_data)
   .netWorkErrorView(R.layout.activity_networkerror)
   .onRetryListener(new OnRetryListener() {
    @Override
    public void onRetry() {
     //為重試加載按鈕的監(jiān)聽事件
    }
   })
   .onShowHideViewListener(new OnShowHideViewListener() {
    @Override
    public void onShowView(View view, int id) {
     //為狀態(tài)View顯示監(jiān)聽事件
    }

    @Override
    public void onHideView(View view, int id) {
     //為狀態(tài)View隱藏監(jiān)聽事件
    }
   })
   .build();
}


2).在父類中重寫以下幾個方法,子類直接繼承就行

//正常展示數(shù)據(jù)狀態(tài)
protected void showContent() {
 statusLayoutManager.showContent();
}

//加載數(shù)據(jù)為空時狀態(tài)
protected void showEmptyData() {
 statusLayoutManager.showEmptyData();
}

//加載數(shù)據(jù)錯誤時狀態(tài)
protected void showError() {
 statusLayoutManager.showError();
}

//網(wǎng)絡(luò)錯誤時狀態(tài)
protected void showNetWorkError() {
 statusLayoutManager.showNetWorkError();
}

//正在加載中狀態(tài)
protected void showLoading() {
 statusLayoutManager.showLoading();
}

3).更加詳細的介紹,可以直接參考Demo
https://github.com/yangchong211/YCStateLayout

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論