I have a problem with shadows that are created with Elevation
.
My screen consists of a ViewPager
with 4 different Fragments
and a TabLayout
for navigating. The first and last fragment both contain a RecyclerView
which is filled with CardView
elements. Whenever I switch between pages 1 and 4, the shadows below CardView
elements first appear way off their position and snap into their correct place after around half a second has passed.
This only occurs when switching between non-neighbouring fragments in the ViewPager
. If I move in order from 1 to 4, this doesn't occur.
Every other element with Elevation
set has this issue, not just CardView
.
My ViewPager implementation:
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (NoSwipeViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(4);
tabLayout.setupWithViewPager(mViewPager);
One of the CardViews used in RecyclerView
<android.support.v7.widget.CardView
style="@style/Custom.CarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<ImageView
android:id="@+id/post_image"
android:layout_width="match_parent"
android:layout_height="179dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
app:imageUrl="@{photo.avatar_url}"
tools:background="@color/com_facebook_blue" />
<Button
android:id="@+id/edit_item_imageView"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="29dp"
android:layout_height="37dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent" />
<ImageView
android:id="@+id/image_edit"
android:layout_width="12dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/profile_post_options" />
<TextView
android:id="@+id/textview_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/post_image"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="@{photo.description}"
android:textColor="@color/v3_primary_gray"
android:textSize="12sp"
android:visibility="@{photo.description.length() > 0 ? View.VISIBLE : View.GONE}"
tools:text="comment"
android:layout_marginBottom="@dimen/margin_bottom_matches"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
The style of the CardView
only has cardCornerRadius
set to 4dp.
How exactly do shadows of Elevation
work? Is there any specific attribute I need to set to prevent this? Or could I use a different method to get the same shadows I would with Elevation
?
Thanks for your help.
mViewPager.setOffscreenPageLimit(4);
to 1 ( 4->1), and page will be recreated each time, shadows won't render wrong? – EmbowedView
with background (with shadow). You can simply create it with XML (just two shapes with rounded corners). – Embowed