1.View作用:
在我们请求后台数据时,可能会遇到一些特殊情况;比如没有网络、接口地址错误、请求回来的接口没有数据,这时候我们就需要一些指定的页面来代替正常显示的view布局。
2.效果图:
效果图
3.OtherView:
public class OtherView extends LinearLayout {
private final int KEY_NORMAL= 0, KEY_LOADING = 1, KEY_EMPTY= 2, KEY_RETRY = 3;
private int mViewType = KEY_NORMAL;
private OtherHolder mHolder;
public OtherView(Context context) {
super(context);
}
public OtherView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setHolder(OtherHolder holder){
mHolder = holder;
initView();
}
private void initView() {
addView(mHolder.mLoadingView,0);
addView(mHolder.mEmptyView,0);
addView(mHolder.mRetryView,0);
setShowOrHide();
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mHolder.mLoadingView.setLayoutParams(ll);
mHolder.mEmptyView.setLayoutParams(ll);
mHolder.mRetryView.setLayoutParams(ll);
}
public void showLoadingView(){
if (mViewType == KEY_LOADING) return;
mViewType = KEY_LOADING;
setShowOrHide();
}
public void showEmptyView(){
if (mViewType == KEY_EMPTY) return;
mViewType = KEY_EMPTY;
setShowOrHide();
}
public void showRetryView(){
if (mViewType == KEY_RETRY) return;
mViewType = KEY_RETRY;
setShowOrHide();
}
public void showContentView(){
if (mViewType == KEY_NORMAL) return;
mViewType = KEY_NORMAL;
setShowOrHide();
}
private void setShowOrHide(){
if (mHolder == null){
throw new RuntimeException("OtherView::请先设置OtherHolder");
}
mHolder.mLoadingView.setVisibility(mViewType == KEY_LOADING ? VISIBLE : GONE);
mHolder.mEmptyView.setVisibility(mViewType == KEY_EMPTY ? VISIBLE : GONE);
mHolder.mRetryView.setVisibility(mViewType == KEY_RETRY ? VISIBLE : GONE);
}
}
3.1这个view比较简单,主要是通过一个Holder类来实现:加载错误页面、空白页、正在加载页。
3.2对外提供方法,实现具体要显示那个页面
4.抽象类 OtherHolder:
public abstract class OtherHolder {
public View mLoadingView;
public View mEmptyView;
public View mRetryView;
public OtherHolder(Context context){
init(context);
}
private void init(Context context){
mEmptyView = setEmpty(context);
mRetryView = setRetry(context);
mLoadingView = setLoading(context);
}
public abstract View setEmpty(Context context);
public abstract View setRetry(Context context);
public abstract View setLoading(Context context);
}
4.1该抽象类主要是为了向OtherView提供三个布局
5.OtherViewHolder:
public class OtherViewHolder extends OtherHolder {
private Context mContext;
private RetryBtnListener mListener;
public OtherViewHolder(Context context) {
super(context);
mContext = context;
}
public void setOnListener(RetryBtnListener listener){
mListener = listener;
}
@Override
public View setEmpty(Context context) {
return LayoutInflater.from(context).inflate(R.layout.base_empty, null);
}
@Override
public View setRetry(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.base_retry, null);
view.findViewById(R.id.m_retry).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null){
mListener.onListener();
}
}
});
return view;
}
@Override
public View setLoading(Context context) {
return LayoutInflater.from(context).inflate(R.layout.base_loading, null);
}
public interface RetryBtnListener{
void onListener();
}
}
5.1实现父类接口,间接为OtherView提供布局(使用者可以根据自己项目需要,更改此holder;界面点击事件自行处理)
6.使用:
mainActivity.png
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.m_other_view)
OtherView mOtherView;
@InjectView(R.id.m_empty_view)
Button mEmptyView;
@InjectView(R.id.m_retry_view)
Button mRetryView;
@InjectView(R.id.m_login_view)
Button mLoginView;
@InjectView(R.id.m_content_view)
Button mContentView;
private OtherViewHolder mHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
mHolder = new OtherViewHolder(MainActivity.this);
mOtherView.setHolder(mHolder);
mHolder.setOnListener(new OtherViewHolder.RetryBtnListener() {
@Override
public void onListener() {
mOtherView.showLoadingView();
}
});
}
@OnClick({R.id.m_empty_view, R.id.m_retry_view, R.id.m_login_view, R.id.m_content_view})
public void onClick(View view) {
switch (view.getId()) {
case R.id.m_empty_view:
mOtherView.showEmptyView();
break;
case R.id.m_retry_view:
mOtherView.showRetryView();
break;
case R.id.m_login_view:
mOtherView.showLoadingView();
break;
case R.id.m_content_view:
mOtherView.showContentView();
break;
}
}
}
6.1这里唯一要主要的一点是必须new 一个OtherHolder子类对象;并使用OtherView的setHolder()方法进行管联
mOtherView.showEmptyView(); //显示空白页
mOtherView.showRetryView(); //显示加载错误页
mOtherView.showLoadingView(); //显示正在加载页
mOtherView.showContentView(); //显示正常数据页
地址:https://github.com/zuohaoyang/OtherView
在这里我没有用到请求,用的按钮模拟请求;使用者可以根据请求时的回调来判断,具体使用哪个view