自定义带进度的ProgressRelativeLayout,实现加载网络数据时有进度条样式
来源:互联网 发布:扫题出答案软件下载 编辑:程序博客网 时间:2024/06/01 09:11
第一步:自定义一个ProgressRelativeLayout类,如下
package com.example.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.text.TextUtils;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.ProgressBar;import android.widget.RelativeLayout;import android.widget.TextView;import com.example.R;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class ProgressRelativeLayout extends RelativeLayout { private static final String TAG_PROGRESS = "ProgressLayout.TAG_PROGRESS"; private static final String TAG_ERROR = "ProgressLayout.TAG_ERROR"; public static enum State { CONTENT, PROGRESS, ERROR } private View mProgressView; private TextView mErrorTextView; private List<View> mContentViews = new ArrayList<View>(); private State mState = State.CONTENT; public ProgressRelativeLayout(Context context) { super(context); } public ProgressRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public ProgressRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } private void init(AttributeSet attrs) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressRelativeLayout); int backgroundColor = a.getColor(R.styleable.ProgressRelativeLayout_progressBackground, Color.TRANSPARENT); boolean progress = a.getBoolean(R.styleable.ProgressRelativeLayout_progress, false); a.recycle(); LayoutParams layoutParams; // if progressBackground color == Color.TRANSPARENT just add progress bar if (backgroundColor == Color.TRANSPARENT) { mProgressView = new ProgressBar(getContext()); layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(CENTER_IN_PARENT); } else { // else wrap progress bar in LinearLayout and set background color to LinearLayout LinearLayout linearLayout = new LinearLayout(getContext()); linearLayout.setGravity(Gravity.CENTER); linearLayout.setBackgroundColor(backgroundColor); layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); ProgressBar progressBar = new ProgressBar(getContext()); linearLayout.addView(progressBar); mProgressView = linearLayout; } mProgressView.setTag(TAG_PROGRESS); addView(mProgressView, layoutParams); // add error text view mErrorTextView = new TextView(getContext()); mErrorTextView.setTag(TAG_ERROR); layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(CENTER_IN_PARENT); addView(mErrorTextView, layoutParams); mProgressView.setVisibility(progress ? VISIBLE : GONE); } @Override public void addView(View child, int index, ViewGroup.LayoutParams params) { super.addView(child, index, params); if (child.getTag() == null || (!child.getTag().equals(TAG_PROGRESS) && !child.getTag().equals(TAG_ERROR))) { mContentViews.add(child); } } public void showProgress() { switchState(State.PROGRESS, null, Collections.<Integer>emptyList()); } public void showProgress(List<Integer> skipIds) { switchState(State.PROGRESS, null, skipIds); } public void showErrorText() { switchState(State.ERROR, null, Collections.<Integer>emptyList()); } public void showErrorText(List<Integer> skipIds) { switchState(State.ERROR, null, skipIds); } public void showErrorText(String error) { switchState(State.ERROR, error, Collections.<Integer>emptyList()); } public void showErrorText(String error, List<Integer> skipIds) { switchState(State.ERROR, error, skipIds); } public void showContent() { switchState(State.CONTENT, null, Collections.<Integer>emptyList()); } public void showContent(List<Integer> skipIds) { switchState(State.CONTENT, null, skipIds); } public void switchState(State state) { switchState(state, null, Collections.<Integer>emptyList()); } public void switchState(State state, String errorText) { switchState(state, errorText, Collections.<Integer>emptyList()); } public void switchState(State state, List<Integer> skipIds) { switchState(state, null, skipIds); } public void switchState(State state, String errorText, List<Integer> skipIds) { mState = state; switch (state) { case CONTENT: mErrorTextView.setVisibility(View.GONE); mProgressView.setVisibility(View.GONE); setContentVisibility(true, skipIds); break; case PROGRESS: mErrorTextView.setVisibility(View.GONE); mProgressView.setVisibility(View.VISIBLE); setContentVisibility(false, skipIds); break; case ERROR: if (TextUtils.isEmpty(errorText)) { mErrorTextView.setText(R.string.unknown_error); } else { mErrorTextView.setText(errorText); } mErrorTextView.setVisibility(View.VISIBLE); mProgressView.setVisibility(View.GONE); setContentVisibility(false, skipIds); break; } } public State getState() { return mState; } public boolean isProgress() { return mState == State.PROGRESS; } public boolean isContent() { return mState == State.CONTENT; } public boolean isError() { return mState == State.ERROR; } private void setContentVisibility(boolean visible, List<Integer> skipIds) { for (View v : mContentViews) { if (!skipIds.contains(v.getId())) { v.setVisibility(visible ? View.VISIBLE : View.GONE); } } }}
第二步:在布局文件中需要网络请求的地方引用
<?xml version="1.0" encoding="utf-8"?><com.example.view.ProgressRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progress_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <span style="white-space:pre"></span>... </LinearLayout></com.example.view.ProgressRelativeLayout>
第三步:在初始化布局时显示进度并开始加载网络数据,根据加载的状态取消进度,并显示相应信息
显示进度
_progressLinearLayout = (ProgressRelativeLayout)findViewById(R.id.progress_layout); _progressLinearLayout.showProgress();
在加载网络数据失败的回调中,显示错误信息
<span style="white-space:pre"></span> @Override public void onFailure(Response resp, Throwable e) { String msg = getString(R.string.film_collect_get_error); _progressLinearLayout.showErrorText(msg);//在此方法的最后调用 }
在加载网络数据成功的回调中,显示数据
<span style="white-space:pre"></span> @Override public void onSuccess(Response resp) { //在此方法的最后调用 _progressLinearLayout.showContent(); }
0 0
- 自定义带进度的ProgressRelativeLayout,实现加载网络数据时有进度条样式
- 自定义View(带进度的圆形进度条)
- 带波形进度的环形进度条实现
- 自定义View,自定义属性(带进度的圆形进度条)
- 异步加载网络数据,自定义进度条显示
- 异步加载网络数据,自定义进度条显示
- 异步加载网络数据,自定义进度条显示
- 异步加载网络数据,自定义进度条显示
- Android 自定义带进度显示的半圆形进度条ArcTextProgressBar
- Android 自定义View -带进度的圆形进度条
- Android带圆形数字进度的自定义进度条
- 自定义一个带进度值的圆形进度条
- android 自定义带进度值的圆形进度条
- 自定义圆形带颜色的加载进度条
- 自定义ProgressBar(包括自定义图片,带进度的圆形进度条、长方形进度条)
- 2种方式实现带进度的圆形进度条
- Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar)
- Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar)
- Java NIO——6 基于非阻塞编程UDP NIO的例子
- 处理从数据库中返回的json格式日期
- 构造方法与析构方法、对象类型在内存中的分配
- 【nyoj 1261 不是匹配 】
- Python web多sitemap创建更新解决方案
- 自定义带进度的ProgressRelativeLayout,实现加载网络数据时有进度条样式
- 集合框架_04_List集合之LinkList
- centos7服务管理组件systemctl的服务存贮目录和常用列表命令
- 坦克小战~~~~之敌人AI
- c语言中的蛇形矩阵代码分享
- 【计算机组成原理】简介
- Velocity教程
- &&与||到底返回什么~,&&or&
- dfs+避免重复-幸运的袋子(网易题)