@@ -7,6 +7,8 @@ import android.util.AttributeSet
7
7
import android.view.View
8
8
import android.widget.FrameLayout
9
9
import androidx.annotation.IntDef
10
+ import androidx.annotation.StringRes
11
+ import androidx.core.view.children
10
12
import com.valartech.commons.R
11
13
12
14
/* *
@@ -63,10 +65,10 @@ class LoadingLayout @JvmOverloads constructor(
63
65
errorView = getChildAt(3 )
64
66
65
67
// If views are specified with tags, override
66
- findViewWithTag< View ?>(context.getString( R .string.ll_loading) )?.let { loadingView = it }
67
- findViewWithTag< View ?>(context.getString( R .string.ll_complete) )?.let { completeView = it }
68
- findViewWithTag< View ?>(context.getString( R .string.ll_empty) )?.let { emptyView = it }
69
- findViewWithTag< View ?>(context.getString( R .string.ll_error) )?.let { errorView = it }
68
+ findChildViewWithTag( R .string.ll_loading)?.let { loadingView = it }
69
+ findChildViewWithTag( R .string.ll_complete)?.let { completeView = it }
70
+ findChildViewWithTag( R .string.ll_empty)?.let { emptyView = it }
71
+ findChildViewWithTag( R .string.ll_error)?.let { errorView = it }
70
72
71
73
if (! isInEditMode) {
72
74
if (loadingView == null ) {
@@ -111,8 +113,12 @@ class LoadingLayout @JvmOverloads constructor(
111
113
loadingView?.visibility = View .GONE
112
114
completeView?.visibility = View .VISIBLE
113
115
} else if (currentState == LOADING ) {
116
+ // if we're showing results after loading, animate the appearance of the
117
+ // complete view
114
118
crossfadeCompleteView()
115
119
} else {
120
+ // if we're showing the complete state in any other case, then just show the
121
+ // view immediately
116
122
loadingView?.visibility = View .GONE
117
123
completeView?.visibility = View .VISIBLE
118
124
}
@@ -159,6 +165,18 @@ class LoadingLayout @JvmOverloads constructor(
159
165
})
160
166
}
161
167
168
+ /* *
169
+ * Searches only the direct children of this view for a child view with the specified tag.
170
+ */
171
+ private fun findChildViewWithTag (@StringRes tagRes : Int ): View ? {
172
+ children.forEach {
173
+ if (context.getString(tagRes) == it.tag) {
174
+ return it
175
+ }
176
+ }
177
+ return null
178
+ }
179
+
162
180
companion object {
163
181
// the values of these constants is significant, they correspond to params in attrs.xml
164
182
const val LOADING = 1
0 commit comments