Created
August 4, 2022 14:20
-
-
Save abd3lraouf/2447f1f2ea24572b4386316a5e90e15e to your computer and use it in GitHub Desktop.
How to make placeholder items in Android RecyclerView Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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