LoadingPage的抽取

news/2024/7/7 21:48:45

抽取的动机

任何一个联网的view界面展示都有四种情况,

①正在加载

②加载失败

③加载成功,但是没有数据

④加载成功,同时返回数据

public abstract class LoadingPager extends FrameLayout {
//1.定义4种不同的显示状态
private static final int STATE_LOADING = 1;
private static final int STATE_ERROR = 2;
private static final int STATE_EMPTY = 3;
private static final int STATE_SUCCESS = 4;

private int state_current = STATE_LOADING;//默认情况下,当前状态为正在加载

//2.提供4种不同的界面
private View view_loading;
private View view_error;
private View view_empty;
private View view_success;
private LayoutParams params;


public LoadingPager(Context context) {
    this(context, null);
}

public LoadingPager(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public LoadingPager(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    init();
}

//初始化方法
private void init() {
    //实例化view
    //1.提供布局显示的参数
    params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    if (view_loading == null) {
        //2.加载布局
        view_loading = UIUtils.getView(R.layout.page_loading);
        //3.添加到当前的frameLayout中
        addView(view_loading, params);
    }

    if (view_empty == null) {
        //2.加载布局
        view_empty = UIUtils.getView(R.layout.page_empty);
        //3.添加到当前的frameLayout中
        addView(view_empty, params);
    }

    if (view_error == null) {
        //2.加载布局
        view_error = UIUtils.getView(R.layout.page_error);
        //3.添加到当前的frameLayout中
        addView(view_error, params);
    }

    showSafePage();
}

//保证如下的操作在主线程中执行的:更新界面
private void showSafePage() {
    UIUtils.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            //保证run()中的操作在主线程中执行
            showPage();
        }
    });
}

private void showPage() {
    //根据当前state_current的值,决定显示哪个view
    view_loading.setVisibility(state_current == STATE_LOADING ? View.VISIBLE : View.INVISIBLE);
    view_error.setVisibility(state_current == STATE_ERROR ? View.VISIBLE : View.INVISIBLE);
    view_empty.setVisibility(state_current == STATE_EMPTY ? View.VISIBLE : View.INVISIBLE);

    if (view_success == null) {
        view_success = UIUtils.getView(layoutId());
        addView(view_success, params);
    }

    view_success.setVisibility(state_current == STATE_SUCCESS ? View.VISIBLE : View.INVISIBLE);
}

public abstract int layoutId();

private ResultState resultState;

//在show()中实现联网加载数据
public void show() {

    String url = url();
    if (TextUtils.isEmpty(url)) {
        resultState = ResultState.SUCCESS;
        resultState.setContent("");
        loadImage();
        return;
    }

    UIUtils.getHandler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //可以换成OKHttp,retrofit等其他框架
            AsyncHttpClient client = new AsyncHttpClient();
            client.get(url(), params(), new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String content) {
                    if (TextUtils.isEmpty(content)) {// "" or null
                        //state_current = STATE_EMPTY;
                        resultState = ResultState.EMPTY;
                        resultState.setContent("");
                    } else {
                        //state_current = STATE_SUCCESS;
                        resultState = ResultState.SUCCESS;
                        resultState.setContent(content);
                    }

                    //showSafePage();
                    loadImage();
                }

                @Override
                public void onFailure(Throwable error, String content) {
                    //state_current = STATE_ERROR;
                    resultState = ResultState.ERROR;
                    resultState.setContent("");

                    // showSafePage();
                    loadImage();

                }
            });
        }
    }, 2000);


}

private void loadImage() {
    switch (resultState) {
        case ERROR:
            state_current = STATE_ERROR;
            break;
        case EMPTY:
            state_current = STATE_EMPTY;
            break;
        case SUCCESS:
            state_current = STATE_SUCCESS;
            break;
    }
    //根据修改以后的state_current,更新视图的显示。
    showSafePage();

    if (state_current == STATE_SUCCESS) {
        onSuccss(resultState, view_success);
    }
}

protected abstract void onSuccss(ResultState resultState, View view_success);

//提供联网的请求参数
protected abstract RequestParams params();

//提供联网的请求地址
public abstract String url();

//提供枚举类,封装联网以后的状态值和数据
public enum ResultState {

    ERROR(2), EMPTY(3), SUCCESS(4);

    int state;

    ResultState(int state) {
        this.state = state;
    }

    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

}

BaseFragment中简单使用

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  /*  View view= UIUtils.getView(getLayoutId());
    unbinder = ButterKnife.bind(this, view);
    initTitle();
    initData();*/
    loadingPager = new LoadingPager(container.getContext()) {
        @Override
        public int layoutId() {
            return getLayoutId();
        }


        @Override
        protected void onSuccss(ResultState resultState, View view_sucess) {
            unbinder = ButterKnife.bind(BaseFragment.this, view_sucess);
            initData(resultState.getContent());
            initTitle();

        }

        @Override
        protected RequestParams params() {
            return getParams();
        }

        @Override
        public String url() {
            return geturl();
        }
    };
    return loadingPager;
}

http://www.niftyadmin.cn/n/3649075.html

相关文章

[C++]打包传输结构体或大内存块的四种办法(完全版)

打包传输结构体或大内存块作者 郑昀内容BSTR的解法SAFEARRAY的解法boost::serialization的解法IStream流的解法本文假定您熟悉 SAFEARRAY、C、BOOST 和 MSMQ。摘要:本文阐述了结构体/大内存块分布式传输时常用的四种打包方法,并演示了您如何利用这四种方…

如何在Ubuntu 18.04服务器上使用MySQL配置Galera群集

介绍 (Introduction) Clustering adds high availability to your database by distributing changes to different servers. In the event that one of the instances fails, others are quickly available to continue serving. 群集通过将更改分发到不同的服务器来增加数据…

Android Launcher开发(一)LiveFolder(实时文件夹) 完全解析

实时文件夹概述: 实时文件夹是在SDK1.5中引入的,支持开发人员在设备的默认打开屏幕(我们将其称为设备的主页)上公开 ContentProvider,如联系人信息、笔记和媒体。将ContentProvider(比如Android的 contactsContentProvider)在主页…

网页开端第一次培训笔记

HTML HTML基本语法由 标签、整体结构、DOCTYPE组成 HTML的常用标签有 标题和水平线、段落和换行、列表、div和span、格式化标签 图片、超链接标签、表格、表单、字符实体等 HTML的全称为超文本标记语言,是一种标记语言。它包括一系列标签.通过这些标…

eclipse theia_如何在Ubuntu 18.04上设置Eclipse Theia Cloud IDE平台

eclipse theia介绍 (Introduction) With developer tools moving to the cloud, adoption of cloud IDE (Integrated Development Environment) platforms is growing. Cloud IDEs are accessible from every type of modern device through web browsers, and they offer nume…

Activity还是getApplicationContext、Fragment

Activity还是getApplicationContext 显示dialog必须使用Activity 使用adapter中初始化convertView最好使用Activity 显示地图时必须使用ApplicationContext 其他绝大的情况下两者都可以 选择ApplicationContext肯定没有问题 选择Activity可能会导致Activity对象不能被回收&…

网页开端第二次培训笔记

form标签性质及相关属性 input元素相关内容及框内属性、按钮属性 label标签、button按钮、select 常用属性:value设置值,selected 设置选中状态

BaseAdapter的封装(ListView)

首先第一步&#xff1a;我们看下我们一般情况下我们的BaseAdapter的使用 public class ProductAdapter extends BaseAdapter {private List<Product> productList;public ProductAdapter(List<Product> productList) {this.productList productList; }Override …