Android 載入成功、載入失敗、載入中、無資料四個不同介面的切換
阿新 • • 發佈:2019-02-05
2.在介面中使用...因為切換各介面的時候,ToolBar是不變的,所以new一個垂直的LinearLayout上面放ToolBar,下面放我們自定義的FrameLayout用來切換介面。package com.ouyang.qqui.view; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import com.ouyang.qqui.R; public class StateLayout extends FrameLayout{ private View loadingView; private View errorView; private View successView; private View emptyView; public StateLayout(Context context) { this(context,null); } public StateLayout(Context context, AttributeSet attrs) { this(context, attrs,0); } public StateLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView();//初始化View } /** * 新增那4個子View:載入中的,載入成功的,載入沒有資料,載入失敗的 */ private void initView() { //1.載入loadingView loadingView = View.inflate(getContext(), R.layout.progressbar_layout, null); addView(loadingView); //2.新增失敗的View errorView = View.inflate(getContext(), R.layout.network404_layout, null); Button btn_reload = (Button) errorView.findViewById(R.id.click_again); btn_reload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //1.先顯示loadingView showLoadingView(); //2.點選的時候再一次重新載入資料 if(listener!=null){ listener.onReload(); } } }); addView(errorView); //3.新增空白的view emptyView = View.inflate(getContext(), R.layout.empty_view, null); addView(emptyView); //4.載入成功的View在各介面是不同的,所以提供一個方法bindsucessview動態新增 //一開始隱藏所有的View hideAll(); } /** * 新增一個成功的View進來 */ public void bindSuccessView(View view){ successView = view; if(successView!=null){ successView.setVisibility(View.INVISIBLE);//隱藏successView //將它新增進來 addView(successView); } } public void showSuccessView(){ //先隱藏其他的 hideAll(); if(successView!=null){ successView.setVisibility(View.VISIBLE); } } public void showEmptyView(){ //先隱藏其他的 hideAll(); emptyView.setVisibility(View.VISIBLE); } public void showErrorView(){ //先隱藏其他的 hideAll(); errorView.setVisibility(View.VISIBLE); } public void showLoadingView(){ //先隱藏其他的 hideAll(); loadingView.setVisibility(View.VISIBLE); } /** * 隱藏所有的View */ public void hideAll(){ //設定各介面不可見,同時讓他們不重新layout,要用的時候直接show就行了 loadingView.setVisibility(View.INVISIBLE); errorView.setVisibility(View.INVISIBLE); emptyView.setVisibility(View.INVISIBLE); if(successView!=null){ successView.setVisibility(View.INVISIBLE); } } private OnReloadListener listener; public void setOnReloadListener(OnReloadListener listener){ this.listener = listener; } public interface OnReloadListener{ /** * 當重新載入的按鈕被點選的時候呼叫 */ void onReload(); } }
3.Toolbar佈局package com.ouyang.qqui.activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.view.View; import android.widget.LinearLayout; import com.ouyang.qqui.R; import com.ouyang.qqui.adapter.AddFootViewAdapter; import com.ouyang.qqui.adapter.MyRecycleradapter; import com.ouyang.qqui.adapter.ViewHolder; import com.ouyang.qqui.bean.UnReceiveListBean; import com.ouyang.qqui.net.BaseCallback; import com.ouyang.qqui.net.ConnectionUrl; import com.ouyang.qqui.net.HttpUtils; import com.ouyang.qqui.view.StateLayout; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.Bind; import butterknife.ButterKnife; /** * Create by oy 2017/6/15 13:50. */ public class ShowActivity extends AppCompatActivity { private StateLayout stateLayout; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView();//初始化介面 initData();//初始化資料 } private void initView() { //為了增加ToolBar新建一個垂直的線性佈局 LinearLayout lin = new LinearLayout(this); lin.setOrientation(LinearLayout.VERTICAL); //將ToolBar新增在上面 lin.addView(View.inflate(this, R.layout.activity_toolbar,null)); //將我們的各種介面新增在下面 stateLayout = new StateLayout(this); lin.addView(stateLayout); //新增一個載入成功的view stateLayout.bindSuccessView(getsuccessView()); //繫結線性佈局 setContentView(lin); ButterKnife.bind(this); //首先展示正在載入的view stateLayout.showLoadingView(); } public View getsuccessView() { View successView = View.inflate(this, R.layout.activity_rec, null); return successView; } private void initData() { //初始化資料,請求網路,按照你的框架來,這裡只做展示用 String url = "哇哈哈哈"; HttpUtils.getInstance().get(url, new BaseCallback<String>() { @Override public void onFailure(int requstcode, Exception e) { //載入失敗展示錯誤的view stateLayout.showErrorView(); } @Override public void onSuccess(String data) { if (TextUtils.isEmpty(data)){ //如果返回的資料為空,展示空介面 stateLayout.showEmptyView(); }else { //如果資料不為空,展示成功的view stateLayout.showSuccessView(); //繼續噼裡啪啦一大堆你的操作 // ... } } }); } }