@@ -22,6 +22,8 @@ import android.app.Activity
2222import android.view.LayoutInflater
2323import android.view.View
2424import android.view.ViewGroup
25+ import android.view.animation.Animation
26+ import android.widget.FrameLayout
2527import android.widget.LinearLayout
2628import androidx.constraintlayout.widget.ConstraintLayout
2729import java.util.*
@@ -33,8 +35,7 @@ class LoadingHelper @JvmOverloads constructor(
3335 private val contentView : View ,
3436 contentAdapter : ContentAdapter <* >? = null
3537) {
36- lateinit var decorView: View
37- private set
38+ lateinit var decorView: View private set
3839 private lateinit var contentParent: ViewGroup
3940 private val parent: ViewGroup ?
4041 private var currentViewHolder: ViewHolder ? = null
@@ -169,27 +170,42 @@ class LoadingHelper @JvmOverloads constructor(
169170 override fun onReload () = onReload()
170171 })
171172
172- fun showLoadingView () = showView(ViewType .LOADING )
173+ @JvmOverloads
174+ fun showLoadingView (animation : Animation ? = null) = showView(ViewType .LOADING , animation)
173175
174- fun showContentView () = showView(ViewType .CONTENT )
176+ @JvmOverloads
177+ fun showContentView (animation : Animation ? = null) = showView(ViewType .CONTENT , animation)
175178
176- fun showErrorView () = showView(ViewType .ERROR )
179+ @JvmOverloads
180+ fun showErrorView (animation : Animation ? = null) = showView(ViewType .ERROR , animation)
177181
178- fun showEmptyView () = showView(ViewType .EMPTY )
182+ @JvmOverloads
183+ fun showEmptyView (animation : Animation ? = null) = showView(ViewType .EMPTY , animation)
179184
180185 /* *
181186 * Shows the view by view type
182187 *
183188 * @param viewType the view type of adapter
184189 */
185- fun showView (viewType : Any ) {
190+ @JvmOverloads
191+ fun showView (viewType : Any , animation : Animation ? = null) {
192+ val currentViewHolder = currentViewHolder
186193 if (currentViewHolder == null ) {
187194 addView(viewType)
188195 } else {
189- if (viewType != = currentViewHolder!! .viewType && currentViewHolder!! .rootView.parent != null ) {
190- contentParent.removeView(currentViewHolder!! .rootView)
196+ if (viewHolders[viewType] == null ) {
191197 addView(viewType)
192198 }
199+ if (viewType != currentViewHolder.viewType) {
200+ getViewHolder(viewType).rootView.visibility = View .VISIBLE
201+ if (animation != null ) {
202+ animation.onStartHideAnimation(currentViewHolder.rootView, currentViewHolder.viewType!! )
203+ animation.onStartShowAnimation(getViewHolder(viewType).rootView, getViewHolder(viewType).viewType!! )
204+ } else {
205+ currentViewHolder.rootView.visibility = View .GONE
206+ }
207+ this .currentViewHolder = getViewHolder(viewType)
208+ }
193209 }
194210 }
195211
@@ -292,15 +308,20 @@ class LoadingHelper @JvmOverloads constructor(
292308 }
293309
294310 private class LinearDecorAdapter (private val views : List <View >) : DecorAdapter() {
311+ private lateinit var contentParent: FrameLayout
312+
295313 override fun onCreateDecorView (inflater : LayoutInflater ) =
296314 LinearLayout (inflater.context).apply {
297315 orientation = LinearLayout .VERTICAL
298- for (view in views) {
299- addView(view)
300- }
316+ contentParent = FrameLayout (inflater.context)
317+ contentParent.layoutParams = FrameLayout .LayoutParams (
318+ ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT
319+ )
320+ views.forEach { addView(it) }
321+ addView(contentParent)
301322 }
302323
303- override fun getContentParent (decorView : View ) = decorView as ViewGroup
324+ override fun getContentParent (decorView : View ) = contentParent
304325 }
305326
306327 class AdapterPool internal constructor(private val helper : LoadingHelper ) {
@@ -312,6 +333,12 @@ class LoadingHelper @JvmOverloads constructor(
312333 interface OnReloadListener {
313334 fun onReload ()
314335 }
336+
337+ interface Animation {
338+ fun onStartShowAnimation (view : View , viewType : Any )
339+
340+ fun onStartHideAnimation (view : View , viewType : Any )
341+ }
315342}
316343
317344enum class ViewType {
0 commit comments