狀態(tài)封裝布局
1.分析疚漆,我們加載一個(gè)頁(yè)面它的顯示樣式有三種:正在加載酣胀、加載錯(cuò)誤
、顯示內(nèi)容娶聘,那我們?cè)趺慈ヅ袛嗄匚畔猓晕覀冃枰趦?nèi)部寫(xiě)一個(gè)方法來(lái)判斷我們是應(yīng)該顯示那個(gè)頁(yè)面。
2.我們更具分析結(jié)果丸升,我們來(lái)看看我么將要去寫(xiě)的方法:
ProgressLayout(Context context){···}
ProgressLayout(Context context, @Nullable AttributeSet attrs){···}
ProgressLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr){···}(基本的構(gòu)造方法)
init(AttributeSet attrs) (在這個(gè)方法中铆农,我們拿到我們的布局)
onDetachedFromWindow()(該方法用于顯示錯(cuò)誤頁(yè)面是,我們點(diǎn)擊刷新頁(yè)面狡耻,重繪頁(yè)面)
public void showLoading(){···}暴露給外面可以使用的方法墩剖,顯示我們的加載頁(yè)面
public void showContent(){···}暴露給外面可以使用的方法,顯示我們的內(nèi)容頁(yè)面
public void showError (OnClickListener click){···}暴露給外面可以使用的方法夷狰,顯示我們的顯示錯(cuò)誤頁(yè)面岭皂,值得注意的是我們?cè)诳吹剿膮?shù)中有一個(gè)點(diǎn)擊事件,用戶在加載錯(cuò)誤時(shí)沼头,我們可以點(diǎn)擊相對(duì)應(yīng)的控件刷新我們的布局爷绘。
public boolean isContent(){···}該方法用于判斷我們此時(shí)頁(yè)面的狀態(tài)书劝,判斷我們的頁(yè)面是不是我們的內(nèi)容狀態(tài),如果不是我們顯示錯(cuò)誤頁(yè)土至。
showLoadingView(){···}這個(gè)方法就是加載方法的具體實(shí)現(xiàn)购对,用于添加我們的加載頁(yè)面,并在這個(gè)加載頁(yè)面中陶因,我們給我們的控件設(shè)置動(dòng)畫(huà)骡苞。
showErrorView(){···}顯示錯(cuò)誤頁(yè)面的具體實(shí)現(xiàn),在這個(gè)方法中我們把我們的錯(cuò)誤頁(yè)面布局添加進(jìn)來(lái)楷扬,同時(shí)解幽,我們也應(yīng)該把我們的錯(cuò)誤頁(yè)用戶點(diǎn)擊刷新的控件實(shí)例化出來(lái),用于提取它的點(diǎn)擊事件毅否。
hideLoadingView(){···}用于隱藏我們的錯(cuò)誤頁(yè)面
hideLoadingView(){···}用于隱藏我們的加載頁(yè)面
hideContentView(){···}用于隱藏我們的內(nèi)容頁(yè)
setContentVisibility (boolean visible){···}這個(gè)方法用于我們手動(dòng)設(shè)置我們的內(nèi)容頁(yè)不能顯示亚铁。
3.用于判斷我們的狀態(tài)我們可以使用我們的枚舉方法來(lái)寫(xiě)入我么的三種狀態(tài),具體的代碼我們來(lái)如下:
public enum State {
LOADING,CONTENT,ERROR
}
4.具體的代碼如下:
public class ProgressLayout extends LinearLayout{
private static final String LOADING_TAG = "loading_tag";
private static final String ERROR_TAG = "error_tag";
private LayoutParams layoutParams ;
private LayoutInflater layoutInflater;
private LinearLayout loadingView , errorView;
private TextView btn_error;
private List<View> contentViews = new ArrayList<>();
private RotateAnimation rotateAnimation;
private State currentState = State.LOADING;
public enum State {
LOADING,CONTENT,ERROR
}
public ProgressLayout(Context context) {
super(context);
}
public ProgressLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ProgressLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
if(child.getTag() == null ||(!child.getTag().equals(LOADING_TAG))&&!(child.getTag().equals(ERROR_TAG))){
contentViews.add(child);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if(btn_error != null){
btn_error.setOnClickListener(null);
}
}
public void showLoading(){
currentState = State.LOADING;
this.showLoadingView();
this.hideErrorView();
this.setContentVisibility(false);
}
public void showContent(){
currentState = State.CONTENT;
ProgressLayout.this.setContentVisibility(true);
ProgressLayout.this.hideErrorView();
}
public void showError (OnClickListener click){
currentState = State.ERROR;
this.hideLoadingView();
this.showErrorView();
this.btn_error.setOnClickListener(click);
hideContentView();
}
public boolean isContent(){
return currentState == State.CONTENT;
}
private void showLoadingView(){
if(loadingView == null){
loadingView = (LinearLayout) layoutInflater.inflate(R.layout.layout_loading_view,null);
loadingView.setTag(LOADING_TAG);
layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ImageView iv_loading = (ImageView) loadingView.findViewById(R.id.iv_loading);
rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(800);
rotateAnimation.setRepeatMode(Animation.RESTART);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.start();
LinearInterpolator lir = new LinearInterpolator();
rotateAnimation.setInterpolator(lir);
iv_loading.startAnimation(rotateAnimation);
this.addView(loadingView,layoutParams);
}else {
rotateAnimation.start();
loadingView.setVisibility(VISIBLE);
}
}
private void showErrorView(){
if(errorView == null){
errorView = (LinearLayout) layoutInflater.inflate(R.layout.layout_error_view,null);
errorView.setTag(ERROR_TAG);
btn_error = (TextView) errorView.findViewById(R.id.btn_try);
layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.addView(errorView,layoutParams);
}else {
errorView.setVisibility(VISIBLE);
}
}
private void hideLoadingView(){
if(loadingView != null && loadingView.getVisibility() != GONE){
loadingView.setVisibility(GONE);
rotateAnimation.cancel();
}
}
private void hideErrorView(){
if(errorView != null && errorView.getVisibility() != GONE){
errorView.setVisibility(GONE);
}
}
private void hideContentView(){
if(contentViews != null){
for(View contentView : contentViews){
contentView.setVisibility(GONE);
}
}
}
public void setContentVisibility (boolean visible){
for(View contentView :contentViews){
contentView.setVisibility(visible ? View.VISIBLE:View.GONE);
}
}
}