一款超酷的Android自定义加载控件

在设计应用的时候,我们应该热爱极简主义,简单就是好的,对于很多用户来说,复杂的东西并不受欢迎。
我要实现的是根据不同的情况去显示不同的加载效果,随用随调,效果是借鉴于某一项目的效果,我认为有必要提取出来改善封装一下,供以后使用。情况大致分为:加载中、无网络、无数据、加载失败等,这些仅仅就需要一个View 就可以搞定啦!

预览下效果图:

我们怎么实现这种效果呢
view_loading.xml的布局如下:

<?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"> <LinearLayout android:id="@+id/lin_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" android:orientation="vertical"> <ImageView android:id="@+id/img_loading" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/loading_animation" /> <TextView android:id="@+id/tv_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_gravity="center_horizontal" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:id="@+id/lin_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" android:visibility="visible" > <ImageView android:id="@+id/iv_load" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/tv_load" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_gravity="center_horizontal" android:textSize="14sp" /> </LinearLayout> </FrameLayout>

从布局来看,我分了两个部分,一个是加载中,另外一个是带有ImagView、文字和按钮的布局,有人看到这,就会说,哇靠,这不是很简单吗?根据不同的情况去设置Visibility的值就好了啊,没错,原理就是这样。

XHLoadingView.java的代码如下:

package com.woyou.loadingdemo.widget; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.woyou.loadingdemo.LoadingState; import com.woyou.loadingdemo.R; /** * Created by Xiho on 11:21. */ public class XHLoadingView extends FrameLayout { private Context mContext; // 加载中的布局 private LinearLayout mLinearLoad; //其他加载的布局 private LinearLayout mLinearLoading; private TextView mTvLoading; private TextView mTvLoad; private ImageView mIvLoading; private ImageView mIvLoad; private Button mBtnLoad; private LoadingState mState; private AnimationDrawable animation; public XHLoadingView(Context context) { super(context); mContext = context; } public XHLoadingView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public XHLoadingView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; } public XHLoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); mContext = context; } public void build(){ LayoutInflater.from(mContext).inflate(R.layout.view_loading, this, true); mLinearLoading = (LinearLayout) findViewById(R.id.lin_loading); mLinearLoad = (LinearLayout) findViewById(R.id.lin_load); mIvLoading = (ImageView) findViewById(R.id.img_loading); mIvLoad = (ImageView) findViewById(R.id.iv_load); mTvLoading = (TextView) findViewById(R.id.tv_loading); mTvLoad = (TextView) findViewById(R.id.tv_load); mBtnLoad = (Button) findViewById(R.id.btn_load); mBtnLoad.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setState(LoadingState.STATE_LOADING); mOnRetryListener.onRetry(); } }); } @Override public void setVisibility(int visibility) { super.setVisibility(visibility); if(View.GONE==visibility && mState==LoadingState.STATE_LOADING && animation!=null&&animation.isRunning()){ animation.stop(); } } /** * 加载中提示文字 */ private String mLoadingText; private int mLoadingIcon; public XHLoadingView withLoadingIcon(int resId){ mLoadingIcon = resId; return this; } /** * 加载数据为空提示文字 */ private String mLoadEmptyText; private int mLoadEmptyIcon; public XHLoadingView withEmptyIcon(int resId){ mLoadEmptyIcon = resId; return this; } /** * 无网络提示 */ private String mLoadNoNetworkText; private int mNoNetworkIcon; public XHLoadingView withNoNetIcon(int resId){ mNoNetworkIcon = resId; return this; } private OnRetryListener mOnRetryListener; /** * 定义重试的的接口 */ public interface OnRetryListener { void onRetry(); } public XHLoadingView withOnRetryListener(OnRetryListener mOnRetryListener){ this.mOnRetryListener = mOnRetryListener; return this; } /** * 设置加载的状态 * @param state */ public void setState(LoadingState state){ if(mState==state){ return; }else if(state==LoadingState.STATE_LOADING){ mLinearLoading.setVisibility(VISIBLE); mLinearLoad.setVisibility(GONE); }else if(state!=LoadingState.STATE_LOADING){ mLinearLoading.setVisibility(GONE); mLinearLoad.setVisibility(VISIBLE); if(animation!=null && mState==LoadingState.STATE_LOADING) animation.stop(); } changeState(state); } public boolean btnEmptyEnable = true; public boolean btnErrorEnable = true; public boolean btnNoNetworkEnable = true; public XHLoadingView withBtnNoNetEnnable(boolean ennable) { btnNoNetworkEnable = ennable; return this; } public XHLoadingView withBtnErrorEnnable(boolean ennable) { btnErrorEnable = ennable; return this; } public XHLoadingView withBtnEmptyEnnable(boolean ennable) { btnEmptyEnable = ennable; return this; } /** * 改变状态 * @param state */ private void changeState(LoadingState state) { switch (state) { //加载中 case STATE_LOADING: mState = LoadingState.STATE_LOADING; mIvLoading.setImageResource(mLoadingIcon); mTvLoading.setText(mLoadingText); if (animation == null) { animation = (AnimationDrawable) mIvLoading.getDrawable(); } if (animation != null) animation.start(); break; //数据为空 case STATE_EMPTY: mState = LoadingState.STATE_EMPTY; mIvLoad.setImageResource(mLoadEmptyIcon); mTvLoad.setText(mLoadEmptyText); if (btnEmptyEnable) { mBtnLoad.setVisibility(VISIBLE); mBtnLoad.setText(btn_empty_text); } else { mBtnLoad.setVisibility(GONE); } break; //加载失败 case STATE_ERROR: mState = LoadingState.STATE_ERROR; mIvLoad.setImageResource(mErrorIco); mTvLoad.setText(mLoadErrorText); if (btnErrorEnable) { mBtnLoad.setVisibility(VISIBLE); mBtnLoad.setText(btn_error_text); } else { mBtnLoad.setVisibility(GONE); } break; //无网络 case STATE_NO_NET: mState = LoadingState.STATE_NO_NET; mIvLoad.setImageResource(mNoNetworkIcon); mTvLoad.setText(mLoadNoNetworkText); if (btnNoNetworkEnable) { mBtnLoad.setVisibility(VISIBLE); mBtnLoad.setText(btn_nonet_text); } else { mBtnLoad.setVisibility(GONE); } break; } } /** * 后台或者本地出现错误提示 */ private String mLoadErrorText; private int mErrorIco; public XHLoadingView withErrorIco(int resId) { mErrorIco = resId; return this; } /** * 加载空数据 * @param resId * @return */ public XHLoadingView withLoadEmptyText(int resId) { mLoadEmptyText = getResources().getString(resId); return this; } public XHLoadingView withLoadEmptyText(String mLoadEmptyText) { this.mLoadEmptyText = mLoadEmptyText; return this; } /** * 无网络时候加载文字 * @param resId * @return */ public XHLoadingView withLoadNoNetworkText(int resId) { mLoadNoNetworkText = getResources().getString(resId); return this; } public String btn_empty_text = "重试"; public String btn_error_text = "重试"; public String btn_nonet_text = "重试"; /** * 数据为空的Button的文字提示 * @param text * @return */ public XHLoadingView withBtnEmptyText(String text) { this.btn_empty_text = text; return this; } /** * 加载错误的Button的文字提示 * @param text * @return */ public XHLoadingView withBtnErrorText(String text) { this.btn_error_text = text; return this; } /** * 加载错误的文字提示 * @param resId * @return */ public XHLoadingView withLoadErrorText(int resId) { this.mLoadErrorText = getResources().getString(resId); return this; } public XHLoadingView withLoadErrorText(String mLoadedErrorText) { this.mLoadErrorText = mLoadedErrorText; return this; } /** * 加载无网络的Button的文字提示 * @param text * @return */ public XHLoadingView withBtnNoNetText(String text) { this.btn_nonet_text = text; return this; } /** * 加载没有网路的文字提示 * @param mLoadedNoNetText * @return */ public XHLoadingView withLoadNoNetworkText(String mLoadedNoNetText) { this.mLoadNoNetworkText = mLoadedNoNetText; return this; } public XHLoadingView withLoadingText(int resId) { this.mLoadingText = getResources().getString(resId); return this; } public XHLoadingView withLoadingText(String mLoadingText) { this.mLoadingText = mLoadingText; return this; } }

针对不同的情况作了不同的处理,然后我们在需要的Activity调用。

private XHLoadingView mLoadingView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); mLoadingView = (XHLoadingView) findViewById(R.id.lv_loading); mLoadingView.withLoadEmptyText("≥﹏≤ , 啥也木有 !").withEmptyIcon(R.drawable.disk_file_no_data).withBtnEmptyEnnable(false) .withErrorIco(R.drawable.ic_chat_empty).withLoadErrorText("(῀( ˙᷄ỏ˙᷅ )῀)ᵒᵐᵍᵎᵎᵎ,我家程序猿跑路了 !").withBtnErrorText("臭狗屎!!!") .withLoadNoNetworkText("你挡着信号啦o( ̄ヘ ̄o)☞ᗒᗒ 你走").withNoNetIcon(R.drawable.ic_chat_empty).withBtnNoNetText("网弄好了,重试") .withLoadingIcon(R.drawable.loading_animation).withLoadingText("加载中...").withOnRetryListener(new XHLoadingView.OnRetryListener() { @Override public void onRetry() { SnackbarUtil.show(mLoadingView,"已经在努力重试了",0); } }).build(); } ........ //加载中 mLoadingView.setVisibility(View.VISIBLE); mLoadingView.setState(LoadingState.STATE_LOADING); //空数据 mLoadingView.setVisibility(View.VISIBLE); mLoadingView.setState(LoadingState.STATE_EMPTY) //无网络 mLoadingView.setVisibility(View.VISIBLE); mLoadingView.setState(LoadingState.STATE_NO_NET); //加载错误 mLoadingView.setVisibility(View.VISIBLE); mLoadingView.setState(LoadingState.STATE_ERROR); ....... }

上面我是为了展示不同的效果,所以写了几个点击事件呢,实际中不是这样的,初始化过后,在需要的地方调用OK, 源码中注释详细,就不用再做过多的解释了吧!

完整代码:XHLoadingView

作者:xuhao
QQ:504105930
转载请注明出处:http://blog.csdn.net/u011974987/article/details/51455333

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。

时间: 2024-07-28 21:20:50

一款超酷的Android自定义加载控件的相关文章

一款超酷的Android自定义加载控件_Android

在设计应用的时候,我们应该热爱极简主义,简单就是好的,对于很多用户来说,复杂的东西并不受欢迎. 我要实现的是根据不同的情况去显示不同的加载效果,随用随调,效果是借鉴于某一项目的效果,我认为有必要提取出来改善封装一下,供以后使用.情况大致分为:加载中.无网络.无数据.加载失败等,这些仅仅就需要一个View 就可以搞定啦! 预览下效果图: 我们怎么实现这种效果呢 view_loading.xml的布局如下: <?xml version="1.0" encoding="utf

Android自定义加载控件实现数据加载动画_Android

本文实例为大家分享了Android自定义加载控件,第一次小人跑动的加载效果眼前一亮,相比传统的PrograssBar高大上不止一点,于是走起,自定义了控件LoadingView去实现动态效果,可直接在xml中使用,具体实现如下 package com.*****.*****.widget; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.util.

Android下拉刷新上拉加载控件(适用于所有View)_Android

     前面写过一篇关于下拉刷新控件的文章下拉刷新控件终结者:PullToRefreshLayout,后来看到好多人还有上拉加载更多的需求,于是就在前面下拉刷新控件的基础上进行了改进,加了上拉加载的功能.不仅如此,我已经把它改成了对所有View都通用!可以随心所欲使用这两个功能~~     我做了一个大集合的demo,实现了ListView.GridView.ExpandableListView.ScrollView.WebView.ImageView.TextView的下拉刷新和上拉加载.后

Android自定义播放器控件VideoView_Android

介绍 最近要使用播放器做一个简单的视频播放功能,开始学习VideoView,在横竖屏切换的时候碰到了点麻烦,不过在查阅资料后总算是解决了.在写VideoView播放视频时候定义控制的代码全写在Actvity里了,写完一看我靠代码好乱,于是就写了个自定义的播放器控件,支持指定大小,可以横竖屏切换,手动左右滑动快进快退.好了,下面开始. 效果图有点卡,我也不知道为啥..... VideoView介绍 这个是我们实现视频播放最主要的控件,详细的介绍大家百度就去看,这里介绍几个常用的方法. 用于播放视频

Android下拉刷新上拉加载控件(适用于所有View)

前面写过一篇关于下拉刷新控件的文章下拉刷新控件终结者:PullToRefreshLayout,后来看到好多人还有上拉加载更多的需求,于是就在前面下拉刷新控件的基础上进行了改进,加了上拉加载的功能.不仅如此,我已经把它改成了对所有View都通用!可以随心所欲使用这两个功能~~ 我做了一个大集合的demo,实现了ListView.GridView.ExpandableListView.ScrollView.WebView.ImageView.TextView的下拉刷新和上拉加载.后面会提供demo的

Android自定义播放器控件VideoView

介绍 最近要使用播放器做一个简单的视频播放功能,开始学习VideoView,在横竖屏切换的时候碰到了点麻烦,不过在查阅资料后总算是解决了.在写VideoView播放视频时候定义控制的代码全写在Actvity里了,写完一看我靠代码好乱,于是就写了个自定义的播放器控件,支持指定大小,可以横竖屏切换,手动左右滑动快进快退.好了,下面开始. 效果图有点卡,我也不知道为啥..... VideoView介绍 这个是我们实现视频播放最主要的控件,详细的介绍大家百度就去看,这里介绍几个常用的方法. 用于播放视频

动态加载控件: 常见问题解决之道

动态加载控件貌似给很多程序员都带来了困扰,经常收到这样的邮件,干脆就写下面这个示 例来演示如何解决那些常见的问题吧. 其实常见的问题通常有这样两个: 1. 通常他们都通过一个按钮来添加一个UserControl 并将它们加入PlaceHolder 容器的 Controls 中.然后页面上就会有一个另外一个按钮,这个按钮什么相关的事也没做,就是做了 一次回发.这样的情况动态添加的控件就不翼而飞了. 2. 今天收到了一封邮件说是要追加控件,和上面的情况看上去好像不一样,但实质就是同 一回事. 原因:

解决tableView中cell动态加载控件的重用问题

解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问题,即使你能做到该cell只根据数值加载了一回控件,你也没法保证不出现重用问题:) 效果(请注意查看,移动下面的格子时,上面出现了重用的问题) 源码: YXCell.h // // YXCell.h // YXTableView // // Copyright (c) 2014年 Y.X. All

ASP.net中动态加载控件时一些问题的总结

asp.net|动态|加载|控件|问题 经常见到有人说在ASP.net中不要使用动态控件,我想主要的原因在于使用动态控件会带来一些问题,在做项目的过程中,我将由动态加载控件引发的总是作了一个小小的总结.1 .在使用LoadControl加载控件后,用户控件中的某些控件不再响应事件. 这个问题主要是由于将控件加载放在if (!Page.IsPostBack)之内引起的,放在外面即可.在思归的blog上对此问题进行了详细的说明.2.用户控件中某些控件的响应出现问题,如某个按钮第一次选择时不触发CLI