Skip to content

Commit eb57cc2

Browse files
feat: add onViewAttached and onViewDetached methods to ViewDelegate
- Added onViewAttached method to handle view attachment logic - Added onViewDetached method to handle view detachment logic Closes #40
1 parent d00b32e commit eb57cc2

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

loadingstateview/src/main/java/com/dylanc/loadingstateview/LoadingStateView.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ class LoadingStateView @JvmOverloads constructor(
148148
if (viewType != currentViewType) {
149149
val view = getView(viewType)
150150
view.visibility = View.VISIBLE
151+
getViewDelegate<ViewDelegate>(viewType)?.onViewAttached(view)
151152
if (animation != null) {
152153
animation.onStartHideAnimation(currentView, currentViewType)
153154
animation.onStartShowAnimation(view, getViewDelegate<ViewDelegate>(viewType)!!.viewType)
154155
} else {
155156
currentView.visibility = View.GONE
156157
}
158+
getViewDelegate<ViewDelegate>(currentViewType)?.onViewDetached(view)
157159
this.currentView = view
158160
}
159161
}
@@ -190,10 +192,11 @@ class LoadingStateView @JvmOverloads constructor(
190192
}
191193

192194
abstract class ViewDelegate(val viewType: Any) {
195+
abstract fun onCreateView(inflater: LayoutInflater, parent: ViewGroup): View
196+
open fun onViewAttached(view: View) = Unit
197+
open fun onViewDetached(view: View) = Unit
193198
var onReloadListener: OnReloadListener? = null
194199
internal set
195-
196-
abstract fun onCreateView(inflater: LayoutInflater, parent: ViewGroup): View
197200
}
198201

199202
private inner class ContentViewDelegate : LoadingStateView.ViewDelegate(ViewType.CONTENT) {

sample-kotlin/src/main/java/com/dylanc/loadingstateview/sample/kotlin/delegate/LoadingViewDelegate.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ class LoadingViewDelegate : LoadingStateView.ViewDelegate(ViewType.LOADING) {
3030

3131
override fun onCreateView(inflater: LayoutInflater, parent: ViewGroup): View =
3232
inflater.inflate(R.layout.layout_loading, parent, false)
33+
34+
override fun onViewAttached(view: View) {
35+
view.animate().rotationBy(360f)
36+
.setDuration(800)
37+
.withEndAction { onViewAttached(view) }
38+
.start()
39+
}
40+
41+
override fun onViewDetached(view: View) {
42+
view.animate().cancel()
43+
}
3344
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="48dp"
3+
android:height="48dp"
4+
android:tint="#bbbbbb"
5+
android:viewportWidth="24"
6+
android:viewportHeight="24">
7+
8+
<path
9+
android:fillColor="@android:color/white"
10+
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z" />
11+
12+
</vector>

sample-kotlin/src/main/res/layout/layout_loading.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
android:layout_width="match_parent"
55
android:layout_height="match_parent">
66

7-
<ProgressBar
8-
android:id="@+id/progress_bar"
9-
style="?android:attr/progressBarStyle"
7+
<ImageView
8+
android:id="@+id/iv_loading"
109
android:layout_width="wrap_content"
1110
android:layout_height="wrap_content"
12-
android:layout_marginStart="8dp"
13-
android:layout_marginTop="24dp"
14-
android:layout_marginEnd="8dp"
15-
android:layout_marginBottom="24dp"
11+
android:importantForAccessibility="no"
1612
app:layout_constraintBottom_toBottomOf="parent"
1713
app:layout_constraintEnd_toEndOf="parent"
1814
app:layout_constraintStart_toStartOf="parent"
1915
app:layout_constraintTop_toTopOf="parent"
20-
app:layout_constraintVertical_chainStyle="packed" />
16+
app:srcCompat="@drawable/ic_refresh" />
2117
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)