Android开发实现自定义新闻加载页面功能实例

时间:2022-07-28
本文章向大家介绍Android开发实现自定义新闻加载页面功能实例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文实例讲述了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;
}
}
}
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。