Skip to content

Commit b6ff5fc

Browse files
authored
Merge pull request #3 from WendyYanto/feature/layout-placeholder
Feature: Layout Placeholder before load
2 parents 942a87e + 5ec937f commit b6ff5fc

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
<dev.wendyyanto.library.LazyLoadComponent
2525
android:id="@+id/llc"
2626
android:layout_width="match_parent"
27-
android:layout_height="10dp"
27+
android:layout_height="wrap_content"
28+
android:padding="16dp"
2829
app:layout_constraintLeft_toLeftOf="parent"
2930
app:layout_constraintRight_toRightOf="parent"
3031
app:layout_constraintTop_toBottomOf="@id/ll_dummy"
3132
app:layout_res="@layout/layout_multiple_textview"
32-
app:parent_layout_id="@id/cl_parent" />
33+
app:parent_layout_id="@id/cl_parent"
34+
app:reset_padding_on_load="true"
35+
app:text_placeholder="Please wait ....." />
3336

3437
</androidx.constraintlayout.widget.ConstraintLayout>
3538

library/src/main/java/dev/wendyyanto/library/LazyLoadComponent.kt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import android.content.Context
44
import android.graphics.Rect
55
import android.util.AttributeSet
66
import android.view.View
7+
import android.view.ViewGroup
78
import android.view.ViewStub
89
import android.view.ViewTreeObserver
910
import android.view.ViewTreeObserver.OnGlobalLayoutListener
11+
import android.widget.ImageView
1012
import android.widget.LinearLayout
13+
import android.widget.TextView
1114

1215
class LazyLoadComponent @JvmOverloads constructor(
1316
context: Context, attrs: AttributeSet? = null
@@ -29,7 +32,10 @@ class LazyLoadComponent @JvmOverloads constructor(
2932
private var screenHeight = -1
3033
private var parentResId = -1
3134
private var isInflated = false
35+
private var resetPaddingFlag = false
3236
private lateinit var parentView: View
37+
private var imageView: ImageView? = null
38+
private var textView: TextView? = null
3339

3440
init {
3541
orientation = VERTICAL
@@ -40,7 +46,13 @@ class LazyLoadComponent @JvmOverloads constructor(
4046
try {
4147
parentResId =
4248
typeArray.getResourceId(R.styleable.LazyLoadComponent_parent_layout_id, -1)
49+
resetPaddingFlag =
50+
typeArray.getBoolean(R.styleable.LazyLoadComponent_reset_padding_on_load, false)
4351
val layoutRes = typeArray.getResourceId(R.styleable.LazyLoadComponent_layout_res, -1)
52+
val imagePlaceholderRes =
53+
typeArray.getResourceId(R.styleable.LazyLoadComponent_image_placeholder, -1)
54+
val textPlaceHolder =
55+
typeArray.getString(R.styleable.LazyLoadComponent_text_placeholder).orEmpty()
4456
if (layoutRes == -1) {
4557
throw IllegalArgumentException(LAYOUT_RESOURCE_IS_INVALID)
4658
}
@@ -51,17 +63,40 @@ class LazyLoadComponent @JvmOverloads constructor(
5163
layoutResource = layoutRes
5264
addView(this)
5365
}
66+
if (imagePlaceholderRes != -1) {
67+
initPlaceHolderImage(imagePlaceholderRes)
68+
} else if (textPlaceHolder.isNotBlank()) {
69+
initPlaceholderText(textPlaceHolder)
70+
}
5471
} finally {
5572
typeArray.recycle()
5673
}
5774
setupLayoutListener()
5875
setupScrollListener()
5976
}
6077

78+
private fun initPlaceholderText(value: String) {
79+
textView = TextView(context)
80+
textView?.apply {
81+
text = value
82+
textAlignment = View.TEXT_ALIGNMENT_CENTER
83+
addView(this)
84+
}
85+
}
86+
87+
private fun initPlaceHolderImage(resource: Int) {
88+
imageView = ImageView(context)
89+
imageView?.apply {
90+
setImageResource(resource)
91+
addView(this)
92+
}
93+
}
94+
6195
private fun setupLayoutListener() {
6296
this.viewTreeObserver.addOnGlobalLayoutListener(object :
6397
OnGlobalLayoutListener {
6498
override fun onGlobalLayout() {
99+
setupPlaceholderLayout()
65100
getGlobalVisibleRect(componentRect)
66101
parentView = rootView.findViewById(parentResId)
67102
screenHeight = parentView.measuredHeight
@@ -70,6 +105,13 @@ class LazyLoadComponent @JvmOverloads constructor(
70105
})
71106
}
72107

108+
private fun setupPlaceholderLayout() {
109+
imageView?.layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
110+
imageView?.layoutParams?.height = ViewGroup.LayoutParams.WRAP_CONTENT
111+
textView?.layoutParams?.width = ViewGroup.LayoutParams.MATCH_PARENT
112+
textView?.layoutParams?.height = ViewGroup.LayoutParams.WRAP_CONTENT
113+
}
114+
73115
private fun setupScrollListener() {
74116
this.viewTreeObserver.addOnScrollChangedListener(object :
75117
ViewTreeObserver.OnScrollChangedListener {
@@ -87,9 +129,17 @@ class LazyLoadComponent @JvmOverloads constructor(
87129

88130
private fun loadComponent() {
89131
if (isInflated) return
90-
layoutParams.height = LayoutParams.WRAP_CONTENT
132+
resetUI()
91133
viewStub.inflate()
92134
isInflated = true
93135
}
94136

137+
private fun resetUI() {
138+
layoutParams.height = LayoutParams.WRAP_CONTENT
139+
removeView(imageView)
140+
removeView(textView)
141+
if (!resetPaddingFlag) return
142+
setPadding(0, 0, 0, 0)
143+
}
144+
95145
}

library/src/main/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
<declare-styleable name="LazyLoadComponent">
44
<attr name="layout_res" format="reference" />
55
<attr name="parent_layout_id" format="reference" />
6+
<attr name="image_placeholder" format="integer" />
7+
<attr name="text_placeholder" format="string" />
8+
<attr name="reset_padding_on_load" format="boolean" />
69
</declare-styleable>
710
</resources>

0 commit comments

Comments
 (0)