I'm using the Shimmer layout
now in my new apps, and its really awesome.
I've started to use this library to handle the shimmer effect with RecyclerView
.
But, I dont know how to do use this shimmer effect in a static layout, like a detail activity of a product for example.
I need to set all my TextView
's background to gray and programmatically set to normal after a success request?
I really don't get it. Can you help me with this concept?
Ok I will just show you the code and explain it afterwards:
Layout file (only important part):
Kotlin using
kotlinx
for view binding:Java version of the Code (without
findViewById()
part):Okay first of all: You do not need the
ShimmerRecyclerView
Library for this use case. I have only used theShimmerLayout
(build.gradle
):My basic idea was to overlap the
Mock Layout
with theContent Layout
. To my knowledge best practice is using aRelativeLayout
rather than aFrameLayout
for this. To overlap I have setandroid:layout_centerHorizontal="@id/llRealContent"
in theMock Layout
(I'd advise you to setlayout_centerVertical
as well). TheShimmerFrameLayout
should represent theRoot-ViewGroup
of thisMock Layout
.In your Mock Layout you just create your Mock Views (meaning the "grey stripes" signaling the user where to expect content). I have made a very simplistic example but of course it will work on more complex layouts. The mock layout should look as close as possible to the real one (I would advise to just copy & paste the real layout and change every
TextView
etc. to aView
and set the background and size as needed).It is important to set the
visibility
of theRoot-ViewGroup
of theContent Layout
togone
so you won't see it and it won't take out any layout space.In the Kotlin/Java code I - for the sake of simplicity - just set the
TextView
texts with a 10 second delay. As a first step you must activate the shimmer effect viasflMockContent.startShimmerAnimation()
.Then fetch your content (from a REST-API, database, whatever). Once you have sucessfully fetched AND set all your data in your
Content Layout
you just have to set the visibility of the Mock Layout to Gone and the visibility of the Content Layout to VisibleVoilah!
(Note that the quality of this code is not great - for the sake of simplicity :D)