Skip to content

Commit 8ef03c1

Browse files
Support transition animation
1 parent 83431a1 commit 8ef03c1

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.dylanc.loadinghelper.sample.animation
2+
3+
import android.animation.Animator
4+
import android.animation.AnimatorListenerAdapter
5+
import android.view.View
6+
import com.dylanc.loadinghelper.LoadingHelper
7+
8+
class FadeAnimation : LoadingHelper.Animation {
9+
override fun onStartShowAnimation(view: View, viewType: Any) {
10+
view.alpha = 0f
11+
view.animate().alpha(1f).duration = 500
12+
}
13+
14+
override fun onStartHideAnimation(view: View, viewType: Any) {
15+
view.animate().alpha(0f).setDuration(500).setListener(object :
16+
AnimatorListenerAdapter() {
17+
override fun onAnimationEnd(animation: Animator?) {
18+
view.visibility = View.GONE
19+
}
20+
})
21+
}
22+
}

app/src/main/java/com/dylanc/loadinghelper/sample/ui/ActErrorActivity.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import androidx.appcompat.app.AppCompatActivity;
2323

2424
import com.dylanc.loadinghelper.LoadingHelper;
25+
import com.dylanc.loadinghelper.ViewType;
2526
import com.dylanc.loadinghelper.sample.R;
2627
import com.dylanc.loadinghelper.sample.adapter.NavIconType;
28+
import com.dylanc.loadinghelper.sample.animation.FadeAnimation;
2729
import com.dylanc.loadinghelper.sample.utils.HttpUtils;
2830
import com.dylanc.loadinghelper.sample.utils.ToolbarUtils;
2931

@@ -48,12 +50,12 @@ private void loadData() {
4850
HttpUtils.requestFailure(new HttpUtils.Callback() {
4951
@Override
5052
public void onSuccess() {
51-
loadingHelper.showContentView();
53+
loadingHelper.showContentView(new FadeAnimation());
5254
}
5355

5456
@Override
5557
public void onFailure() {
56-
loadingHelper.showErrorView();
58+
loadingHelper.showErrorView(new FadeAnimation());
5759
}
5860
});
5961
}
@@ -63,12 +65,12 @@ public void onReload() {
6365
HttpUtils.requestSuccess(new HttpUtils.Callback() {
6466
@Override
6567
public void onSuccess() {
66-
loadingHelper.showContentView();
68+
loadingHelper.showContentView(new FadeAnimation());
6769
}
6870

6971
@Override
7072
public void onFailure() {
71-
loadingHelper.showErrorView();
73+
loadingHelper.showErrorView(new FadeAnimation());
7274
}
7375
});
7476
}

library/src/main/java/com/dylanc/loadinghelper/LoadingHelper.kt

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import android.app.Activity
2222
import android.view.LayoutInflater
2323
import android.view.View
2424
import android.view.ViewGroup
25+
import android.view.animation.Animation
26+
import android.widget.FrameLayout
2527
import android.widget.LinearLayout
2628
import androidx.constraintlayout.widget.ConstraintLayout
2729
import 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

317344
enum class ViewType {

0 commit comments

Comments
 (0)