23cc850ee2f0af6d38c039f1e426800f0e338ab8.svn-base 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.hjq.widget;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.drawable.Drawable;
  5. import android.os.Build;
  6. import android.support.annotation.AttrRes;
  7. import android.support.annotation.DrawableRes;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.Nullable;
  10. import android.support.annotation.StringRes;
  11. import android.util.AttributeSet;
  12. import android.view.LayoutInflater;
  13. import android.view.MotionEvent;
  14. import android.view.ViewGroup;
  15. import android.widget.ImageView;
  16. import android.widget.TextView;
  17. /**
  18. * desc : 状态布局(网络错误,异常错误,空数据)
  19. */
  20. public final class HintLayout extends SimpleLayout {
  21. //提示布局
  22. private ViewGroup mMainLayout;
  23. //提示图标
  24. private ImageView mImageView;
  25. //提示文本
  26. private TextView mTextView;
  27. public HintLayout(@NonNull Context context) {
  28. super(context);
  29. }
  30. public HintLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
  31. super(context, attrs);
  32. }
  33. public HintLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
  34. super(context, attrs, defStyleAttr);
  35. }
  36. /**
  37. * 显示
  38. */
  39. public void show() {
  40. if (mMainLayout == null) {
  41. //初始化布局
  42. initLayout();
  43. }
  44. if (!isShow()) {
  45. // 显示布局
  46. mMainLayout.setVisibility(VISIBLE);
  47. }
  48. }
  49. /**
  50. * 隐藏
  51. */
  52. public void hide() {
  53. if (mMainLayout != null && isShow()) {
  54. //隐藏布局
  55. mMainLayout.setVisibility(INVISIBLE);
  56. }
  57. }
  58. /**
  59. * 是否显示了
  60. */
  61. public boolean isShow() {
  62. return mMainLayout != null && mMainLayout.getVisibility() == VISIBLE;
  63. }
  64. /**
  65. * 设置提示图标,请在show方法之后调用
  66. */
  67. public void setIcon(@DrawableRes int iconId) {
  68. setIcon(getResources().getDrawable(iconId));
  69. }
  70. public void setIcon(Drawable drawable) {
  71. if (mImageView != null) {
  72. mImageView.setImageDrawable(drawable);
  73. }
  74. }
  75. /**
  76. * 设置提示文本,请在show方法之后调用
  77. */
  78. public void setHint(@StringRes int textId) {
  79. setHint(getResources().getString(textId));
  80. }
  81. public void setHint(CharSequence text) {
  82. if (mTextView != null && text != null) {
  83. mTextView.setText(text);
  84. }
  85. }
  86. /**
  87. * 初始化提示的布局
  88. */
  89. private void initLayout() {
  90. mMainLayout = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.widget_hint_layout, null);
  91. mImageView = mMainLayout.findViewById(R.id.iv_hint_icon);
  92. mTextView = mMainLayout.findViewById(R.id.iv_hint_text);
  93. if (getBackground() == null) {
  94. // 默认使用 windowBackground 作为背景
  95. TypedArray ta = getContext().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
  96. setBackground(ta.getDrawable(0));
  97. ta.recycle();
  98. }
  99. addView(mMainLayout);
  100. }
  101. @Override
  102. public void setBackground(Drawable background) {
  103. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  104. super.setBackground(background);
  105. mMainLayout.setBackground(background);
  106. } else {
  107. setBackgroundDrawable(background);
  108. mMainLayout.setBackgroundDrawable(background);
  109. }
  110. }
  111. @Override
  112. public boolean onInterceptTouchEvent(MotionEvent ev) {
  113. if (isShow()) {
  114. // 拦截布局中的触摸事件,拦截事件,防止传递
  115. return true;
  116. }
  117. return super.onInterceptTouchEvent(ev);
  118. }
  119. }