You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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