本篇文章给大家分享的是有关Android中怎么自定义新闻加载页面,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
首先的定义三个布局,为什么是三个,因为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 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>
初始化控件
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 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();}
三、使用:
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; } public abstract View createSuccessView(); 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中怎么自定义新闻加载页面,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。