Android 自定义新闻加载页面

一、概述:

1、效果演示:
这里写图片描述

2、说明:在新闻页面刚加载的时候,一般会出现五种状态
未知状态(STATE_UNKNOW)、空状态(STATE_EMPTY)、加载中(STATE_LOADING)、错误(STATE_ERROT)、成功(STATE_SUCCESS)
因为每个Detail页面都会出现,所以我们可以把他们封装成一个LoadPage的自定义view,可以复用

二、实现:

1、首先的定义三个布局,为什么是三个,因为unkonw与loading的页面可以使用同一个,而success的页面是加载数据的页面,这里不用定义
1)loading页面布局,只有一个进度条

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

2)空页面只有一张图片,显示没有数据

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_empty_page" />

</RelativeLayout>

3)错误页面有一张错误图片与按钮,点击按钮重新加载数据

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <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/page_bt"
            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="@string/load_error"
            android:textColor="#ff717171"
            android:textSize="14dp" />
    </RelativeLayout>

</FrameLayout>

4、初始化控件

/**
     * 初始化加载三种布局
     */
    private void init() {
        mLoadingView = initView(R.layout.loadpage_loading);
        mEmptyView = initView(R.layout.loadpage_empty);
        mErrorView = initView(R.layout.loadpage_error);

        //如果发生错误,点击重新加载
        Button btnError = (Button) mErrorView.findViewById(R.id.page_bt);
        btnError.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });

        showPages();
    }

5、全部代码:

/**
 * @描述 加载页面
 * @项目名称 App_Shop
 * @包名 com.android.shop.view
 * @类名 LoadingPage
 * @author chenlin
 * @date 2014年3月29日 下午8:49:39
 */

public abstract class LoadingPage extends FrameLayout {
    private final static int STATE_UNKNOW = 0;
    private final static int STATE_LOADING = 1;
    private final static int STATE_ERROT = 2;
    private final static int STATE_EMPTY = 3;
    private final static int STATE_SUCCESS = 4;
    // 不能使用静态的,
    private int currentState = STATE_UNKNOW;

    private View mLoadingView; // 加载
    private View mEmptyView; // 空页面
    private View mErrorView; // 网络错误
    private View mSuccessView; // 加载成功后的页面
    private Context mContext;

    /**
     * 定义枚举类型
     */
    public enum LoadResult {
        error(STATE_ERROT), empty(STATE_EMPTY), success(STATE_SUCCESS);
        int value;

        LoadResult(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    public LoadingPage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

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

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

    /**
     * 初始化加载三种布局
     */
    private void init() {
        mLoadingView = initView(R.layout.loadpage_loading);
        mEmptyView = initView(R.layout.loadpage_empty);
        mErrorView = initView(R.layout.loadpage_error);

        //如果发生错误,点击重新加载
        Button btnError = (Button) mErrorView.findViewById(R.id.page_bt);
        btnError.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });

        showPages();
    }

    public View initView(int resId) {
        View view = View.inflate(mContext, resId, null);
        if (view != null) {
            this.addView(view, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            return view;
        }
        return null;
    }

    private void showPages() {
        //加载页面显示与不显示
        mLoadingView.setVisibility(currentState == STATE_UNKNOW || currentState == STATE_LOADING ? View.VISIBLE
                : View.GONE);
        //空页面
        mEmptyView.setVisibility(currentState == STATE_EMPTY ? View.VISIBLE : View.GONE);
        //错误页面显示
        mErrorView.setVisibility(currentState == STATE_ERROT ? View.VISIBLE : View.GONE);
        //如果数据加载成功了,
        if (currentState == STATE_SUCCESS) {
            if (mSuccessView == null) {
                //加载成功页面信息,成功后的页面就是新闻页面信息
                mSuccessView = createSuccessView();
                //添加页面到framelayout里
                addView(mSuccessView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
                mSuccessView.setVisibility(View.VISIBLE);
            }else {
                mSuccessView.setVisibility(View.GONE);
            }
        } 
    }

    public void show() {
        if (currentState == STATE_EMPTY || currentState == STATE_ERROT) {
            currentState = STATE_LOADING;
        }
        // 请求服务器 获取服务器上数据 进行判断
        // 请求服务器 返回一个结果
        ThreadManager.getInstance().createLongPool().execute(new Runnable() {
            @Override
            public void run() {
                //从服务器加载数据,得到返回的状态信息
                final LoadResult result = loadFromServer();
                if (result != null) {
                    Util.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            currentState = result.getValue();
                            //显示
                            showPages();
                        }
                    });
                }
            }
        });

        showPages();
    }

    public abstract View createSuccessView();

    public abstract LoadResult loadFromServer();

}

三、使用:

/**
 * @描述         fragment
 * @项目名称      App_Shop
 * @包名         com.android.shop.fragment
 * @类名         BaseFragment
 * @author      chenlin
 * @date        2014年3月28日 下午10:33:59
 */

public abstract  class  BaseFragment<T> extends Fragment {

    private LoadingPage mLoadingPage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mLoadingPage == null) {
            mLoadingPage = new LoadingPage(getActivity()){
                @Override
                public View createSuccessView() {
                    return BaseFragment.this.createSuccessView();
                }

                @Override
                public LoadResult loadFromServer() {
                    return BaseFragment.this.load();
                }
            };
        }else {
            ViewUtil.removeParent(mLoadingPage);
        }
        return mLoadingPage;
    }



    /***
     *  创建成功的界面
     * @return
     */
    public  abstract View createSuccessView();

    /**
     * 从服务器得到结果吗
     * @return
     */
    protected abstract LoadResult load();

    /**
     * 显示加载页面
     */
    public void show(){
        if (mLoadingPage != null) {
            mLoadingPage.show();
        }
    }

    /**校验数据 */
    public LoadResult checkData(List<T> datas){
        if (datas == null) {
            return LoadResult.error;
        }else {
            if (datas.size() == 0) {
                return LoadResult.empty;
            }else {
                return LoadResult.success;
            }
        }
    }
}

———————————————————————
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万)
有需求者请进站查看,非诚勿扰

https://item.taobao.com/item.htm?spm=686.1000925.0.0.4a155084hc8wek&id=555888526201

01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
(送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门)
——————————————————————–

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值