一个简单的页面加载管理类(包含加载中,加载失败,数据为空,加载成功)(下)

简介: 在最近公布的比赛框架中,发现了页面加载管理类,觉得挺有用的,所以做个简单的笔记。

现在看一下它的四种不同状态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);
    }
}

好啦,具体就是这样呢,相应的注释都挺详细的,如果有什么地方不对,也欢迎大家指出。

目录
相关文章
|
存储 缓存 网络协议
从输入URL到页面加载的全过程?隐藏元素有哪些方式,有什么区别?什么是内存泄漏?
1. 在浏览器地址栏输入url并回车 2. 浏览器查看是否有缓存,比较缓存是否过期,无缓存或过期则重新发起请求 3. DNS解析域名对应的IP 4. 根据IP建立TCP链接,即三次握手 5. 发送http请求 6. 服务器响应并返回结果 7. 关闭TCP链接,即四次挥手 8. 渲染页面,构建DOM树
|
JavaScript 前端开发
react 实现图片正在加载中 加载完成 加载失败三个阶段的
最近博客写道项目列表中,发现这里比较多图片,一开加载会比较慢,然后就想要用一个loading的图片来占位。与此同时,如果图片加载失败那么显示错误的图片,不显示一个原有的错误,那样比较难看。
react 实现图片正在加载中 加载完成 加载失败三个阶段的
一个简单的页面加载管理类(包含加载中,加载失败,数据为空,加载成功)(上)
在最近公布的比赛框架中,发现了页面加载管理类,觉得挺有用的,所以做个简单的笔记。
94 0
一个简单的页面加载管理类(包含加载中,加载失败,数据为空,加载成功)(上)
|
XML JavaScript 前端开发
HTML页面基本结构和加载过程
HTML页面基本结构和加载过程
244 0
HTML页面基本结构和加载过程
|
Web App开发 JavaScript iOS开发
求助,主页面通过iframe访问ngnix中的子页面,子页面中利用调用js方法判断localstonge中是否有值,ios不可用,关闭阻止跨网站追踪,safari可用
紧急求助,主页面通过iframe访问ngnix中的子页面,子页面中利用调用js方法判断localstonge中是否有值,ios不可用,关闭阻止跨网站追踪,safari可用
|
JavaScript Java
如何查找出本地运行的UI5应用加载的库文件具体是从哪个位置加载的
如何查找出本地运行的UI5应用加载的库文件具体是从哪个位置加载的
100 0
如何查找出本地运行的UI5应用加载的库文件具体是从哪个位置加载的
|
XML Android开发 数据格式
【PageLayout】非常简单的一键切换加载-空数据-错误页,支持自定义
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/82594706 项目中我们经常会用到的加载数据,加载完数据后显示内容,如果没有数据显示一个空白页,这是如果网络错误了显示一个网络错误页,自定义一个PageLayout。
1150 0
|
测试技术 编译器 C++