现在看一下它的四种不同状态view
加载中 Loading...
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ProgressBar style="@android:style/Widget.ProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:indeterminateDrawable="@drawable/indeterminate_drawable" /> </FrameLayout>
加载失败
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:id="@+id/page_iv" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:scaleType="centerInside" android:src="@drawable/ic_error_page" /> <Button android:id="@+id/btn_reload" android:layout_width="wrap_content" android:layout_height="34dp" android:layout_below="@id/page_iv" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:background="@drawable/btn_bg" android:ellipsize="end" android:paddingLeft="10dp" android:paddingRight="10dp" android:singleLine="true" android:text="加载失败,点击重试" android:textColor="#ff717171" android:textSize="16sp" /> </RelativeLayout> </FrameLayout>
数据为null
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tree" android:contentDescription="@string/no_data" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="@string/no_data" app:layout_constraintTop_toBottomOf="@id/image" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>
加载成功
这个布局就不用写了,就是你自己要显示的布局
那么具体在代码中如何使用呢,我们看下面这个Demo。
先是一个BaseFragment的基类。
/** * @author Petterp * @date 2019/5/21. */ public abstract class BaseFragment extends SupportFragment implements View.OnClickListener { public ContentPage contentPage; public ProgressDialog pdLoading; protected Activity mActivity; protected Context mContext; private Unbinder mUnBinder; @Override public void onAttach(Context context) { mActivity = (Activity) context; mContext = context; super.onAttach(context); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { /* * 初始化pdLoading */ pdLoading = new ProgressDialog(getActivity()); pdLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER); pdLoading.setMessage("请稍后"); pdLoading.setCanceledOnTouchOutside(false); pdLoading.setCancelable(true); if (contentPage == null) { contentPage = new ContentPage(getActivity()) { @Override public Object loadData() { return requestData(); } @Override public View createSuccessView() { return getSuccessView(); } }; } else { removeSelfFromParent(contentPage); } return contentPage; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mUnBinder = ButterKnife.bind(this, view); } /** * 初始化 Toolbar */ protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title) { ((BaseActivity) getActivity()).initToolBar(toolbar, homeAsUpEnabled, title); } /** * 刷新状态 * */ public void refreshPage(Object o) { contentPage.refreshPage(o); } /** * 返回据的fragment填充的具体View */ protected abstract View getSuccessView(); /** * 返回请求服务器的数据 */ protected abstract Object requestData(); @Override public void onDestroyView() { super.onDestroyView(); mUnBinder.unbind(); } public void removeSelfFromParent(View child) { // 获取父view if (child != null) { ViewParent parent = child.getParent(); if (parent instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) parent; // 将自己移除 viewGroup.removeView(child); } } } }
然后是一个模拟Fragment
/** * 模拟Fragment */ public class FirstFragment extends BaseFragment { @Override protected View getSuccessView() { TextView textView = new TextView(getActivity()); textView.setText("第一页"); return textView; } @Override protected Object requestData() { SystemClock.sleep(1000);/*模拟请求服务器的延时过程*/ return "";/*加载成功*/ } @Override public void onClick(View view) { } /** * 类似于 Activity的 onNewIntent() */ @Override public void onNewBundle(Bundle args) { super.onNewBundle(args); } }
好啦,具体就是这样呢,相应的注释都挺详细的,如果有什么地方不对,也欢迎大家指出。