ViewUtils.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.cooleshow.base.utils;
  2. import android.content.Context;
  3. import android.os.Build;
  4. import android.text.TextUtils;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import java.util.Locale;
  9. import androidx.annotation.LayoutRes;
  10. /**
  11. * <pre>
  12. * author: Blankj
  13. * blog : http://blankj.com
  14. * time : 2019/06/18
  15. * desc : utils about view
  16. * </pre>
  17. */
  18. public class ViewUtils {
  19. /**
  20. * Set the enabled state of this view.
  21. *
  22. * @param view The view.
  23. * @param enabled True to enabled, false otherwise.
  24. */
  25. public static void setViewEnabled(View view, boolean enabled) {
  26. setViewEnabled(view, enabled, (View) null);
  27. }
  28. /**
  29. * Set the enabled state of this view.
  30. *
  31. * @param view The view.
  32. * @param enabled True to enabled, false otherwise.
  33. * @param excludes The excludes.
  34. */
  35. public static void setViewEnabled(View view, boolean enabled, View... excludes) {
  36. if (view == null) return;
  37. if (excludes != null) {
  38. for (View exclude : excludes) {
  39. if (view == exclude) return;
  40. }
  41. }
  42. if (view instanceof ViewGroup) {
  43. ViewGroup viewGroup = (ViewGroup) view;
  44. int childCount = viewGroup.getChildCount();
  45. for (int i = 0; i < childCount; i++) {
  46. setViewEnabled(viewGroup.getChildAt(i), enabled, excludes);
  47. }
  48. }
  49. view.setEnabled(enabled);
  50. }
  51. /**
  52. * @param runnable The runnable
  53. */
  54. public static void runOnUiThread(final Runnable runnable) {
  55. UtilsBridge.runOnUiThread(runnable);
  56. }
  57. /**
  58. * @param runnable The runnable.
  59. * @param delayMillis The delay (in milliseconds) until the Runnable will be executed.
  60. */
  61. public static void runOnUiThreadDelayed(final Runnable runnable, long delayMillis) {
  62. UtilsBridge.runOnUiThreadDelayed(runnable, delayMillis);
  63. }
  64. /**
  65. * Return whether horizontal layout direction of views are from Right to Left.
  66. *
  67. * @return {@code true}: yes<br>{@code false}: no
  68. */
  69. public static boolean isLayoutRtl() {
  70. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
  71. Locale primaryLocale;
  72. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  73. primaryLocale = Utils.getApp().getResources().getConfiguration().getLocales().get(0);
  74. } else {
  75. primaryLocale = Utils.getApp().getResources().getConfiguration().locale;
  76. }
  77. return TextUtils.getLayoutDirectionFromLocale(primaryLocale) == View.LAYOUT_DIRECTION_RTL;
  78. }
  79. return false;
  80. }
  81. /**
  82. * Fix the problem of topping the ScrollView nested ListView/GridView/WebView/RecyclerView.
  83. *
  84. * @param view The root view inner of ScrollView.
  85. */
  86. public static void fixScrollViewTopping(View view) {
  87. view.setFocusable(false);
  88. ViewGroup viewGroup = null;
  89. if (view instanceof ViewGroup) {
  90. viewGroup = (ViewGroup) view;
  91. }
  92. if (viewGroup == null) {
  93. return;
  94. }
  95. for (int i = 0, n = viewGroup.getChildCount(); i < n; i++) {
  96. View childAt = viewGroup.getChildAt(i);
  97. childAt.setFocusable(false);
  98. if (childAt instanceof ViewGroup) {
  99. fixScrollViewTopping(childAt);
  100. }
  101. }
  102. }
  103. public static View layoutId2View(@LayoutRes final int layoutId) {
  104. LayoutInflater inflate =
  105. (LayoutInflater) Utils.getApp().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  106. return inflate.inflate(layoutId, null);
  107. }
  108. }