Skip to content

Instantly share code, notes, and snippets.

@jojemapa
Created February 21, 2019 20:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jojemapa/9138edba9c5476f577d60ecb8b1af266 to your computer and use it in GitHub Desktop.
Save jojemapa/9138edba9c5476f577d60ecb8b1af266 to your computer and use it in GitHub Desktop.
How to make placeholder items in Android RecyclerView Android
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MockPlaceHolder">
<attr name="repeact" format="integer"/>
<attr name="layoutRes" format="integer"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#bbb"/>
<corners android:radius="3dp"/>
</shape>
package com.store.ui.widget
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
class MockPlaceHolder(context: Context, attrs: AttributeSet?): LinearLayout(context, attrs) {
var repeact = 1
var layoutResource = android.R.layout.activity_list_item
init { init(attrs) }
private fun init(attrs: AttributeSet?) {
orientation = VERTICAL
with(context.theme.obtainStyledAttributes(attrs, R.styleable.MockPlaceHolder, 0, 0)){
try {
repeact = getInteger(R.styleable.MockPlaceHolder_repeact, 1)
layoutResource = getResourceId(R.styleable.MockPlaceHolder_layoutRes, android.R.layout.activity_list_item)
for (i in 1..repeact){
addView(inflate(context, layoutResource, null))
}
} finally { recycle() }
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.6"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:duration="1000"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<com.store.ui.widget.PlaceHolderView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center" />
<LinearLayout
android:layout_marginLeft="16dp"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.store.ui.widget.PlaceHolderView
android:layout_width="wrap_content"
android:layout_height="15dp" />
<com.store.ui.widget.PlaceHolderView
android:layout_marginTop="6dp"
android:layout_width="match_parent"
android:layout_marginRight="32dp"
android:layout_height="15dp" />
<com.store.ui.widget.PlaceHolderView
android:layout_marginTop="6dp"
android:layout_width="match_parent"
android:layout_marginRight="64dp"
android:layout_height="15dp" />
</LinearLayout>
</LinearLayout>
package com.store.ui.widget
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.animation.AnimationUtils
class PlaceHolderView(context: Context, attrs: AttributeSet?): View(context, attrs) {
init {
setBackgroundResource(R.drawable.loading_placeholder)
startAnimation(AnimationUtils.loadAnimation(context, R.anim.placeholder))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment