1414 * limitations under the License.
1515 */
1616
17- @file:Suppress(" unused" )
17+ @file:Suppress(" unused" , " MemberVisibilityCanBePrivate " )
1818
1919package com.dylanc.loadinghelper
2020
2121import android.app.Activity
2222import android.view.LayoutInflater
2323import android.view.View
2424import android.view.ViewGroup
25- import android.view.animation.Animation
2625import android.widget.FrameLayout
2726import android.widget.LinearLayout
2827import androidx.constraintlayout.widget.ConstraintLayout
28+ import androidx.core.view.updateLayoutParams
2929import java.util.*
3030
3131/* *
@@ -96,6 +96,20 @@ class LoadingHelper @JvmOverloads constructor(
9696 showView(ViewType .CONTENT )
9797 }
9898
99+ /* *
100+ * Adds one or more views to decorate content in the header.
101+ *
102+ * @param adapters the adapters of creating view
103+ * @since v2.2.1
104+ */
105+ fun setDecorHeader (vararg adapters : Adapter <* >) {
106+ val viewType = Array <Any >(adapters.size) { adapters[it].javaClass.name }
107+ for (i in adapters.indices) {
108+ register(viewType[i], adapters[i])
109+ }
110+ setDecorHeader(* viewType)
111+ }
112+
99113 /* *
100114 * Adds one or more views to decorate content in the header.
101115 *
@@ -125,6 +139,20 @@ class LoadingHelper @JvmOverloads constructor(
125139 showView(ViewType .CONTENT )
126140 }
127141
142+ /* *
143+ * Adds child decorative header between the content and the decorative view.
144+ *
145+ * @param adapters the adapters of creating view
146+ * @since v2.2.1
147+ */
148+ fun addChildDecorHeader (vararg adapters : Adapter <* >) {
149+ val viewType = Array <Any >(adapters.size) { adapters[it].javaClass.name }
150+ for (i in adapters.indices) {
151+ register(viewType[i], adapters[i])
152+ }
153+ addChildDecorHeader(* viewType)
154+ }
155+
128156 /* *
129157 * Adds child decorative header between the content and the decorative view.
130158 *
@@ -165,11 +193,6 @@ class LoadingHelper @JvmOverloads constructor(
165193 this .onReloadListener = onReloadListener
166194 }
167195
168- fun setOnReloadListener (onReload : () -> Unit ) =
169- setOnReloadListener(object : OnReloadListener {
170- override fun onReload () = onReload()
171- })
172-
173196 @JvmOverloads
174197 fun showLoadingView (animation : Animation ? = null) = showView(ViewType .LOADING , animation)
175198
@@ -216,8 +239,10 @@ class LoadingHelper @JvmOverloads constructor(
216239 (rootView.parent as ViewGroup ).removeView(rootView)
217240 }
218241 if (parent is ConstraintLayout && viewType == ViewType .CONTENT ) {
219- if (rootView.measuredWidth == 0 ) rootView.setWidth(ViewGroup .LayoutParams .MATCH_PARENT )
220- if (rootView.measuredHeight == 0 ) rootView.setHeight(ViewGroup .LayoutParams .MATCH_PARENT )
242+ rootView.updateLayoutParams {
243+ if (rootView.measuredWidth == 0 ) width = ViewGroup .LayoutParams .MATCH_PARENT
244+ if (rootView.measuredHeight == 0 ) height = ViewGroup .LayoutParams .MATCH_PARENT
245+ }
221246 }
222247 contentParent.addView(rootView)
223248 currentViewHolder = viewHolder
@@ -242,18 +267,6 @@ class LoadingHelper @JvmOverloads constructor(
242267 return null
243268 }
244269
245- private fun View.setWidth (width : Int ) {
246- val layoutParams = layoutParams
247- layoutParams.width = width
248- this .layoutParams = layoutParams
249- }
250-
251- private fun View.setHeight (height : Int ) {
252- val layoutParams = layoutParams
253- layoutParams.height = height
254- this .layoutParams = layoutParams
255- }
256-
257270 @Suppress(" UNCHECKED_CAST" )
258271 fun <T : Adapter <out ViewHolder >> getAdapter (viewType : Any ) = adapters[viewType] as T
259272
@@ -330,7 +343,7 @@ class LoadingHelper @JvmOverloads constructor(
330343 }
331344 }
332345
333- interface OnReloadListener {
346+ fun interface OnReloadListener {
334347 fun onReload ()
335348 }
336349
0 commit comments