Elevated RecyclerView items gradually warp (height changes)

Any idea why the RecyclerView elements are warping, each subsequent element getting worse?

UPDATE: I use CardViews, and now there is no representation of "warp", but the height of the shadow / height changes.

It seems that the height is getting higher depending on where on the screen the element is located vertically, the shadow grows as the element scrolls. Watch the video: https://youtu.be/nROYq8rpUMs .

I created a new project, leaving only the code needed to demonstrate the problem:

MainActivity.java

package tzig.schm00.masterdetail; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import tzig.schm00.masterdetail.dummy.DummyContent; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View recyclerView = findViewById(R.id.item_list); setupRecyclerView((RecyclerView) recyclerView); } private void setupRecyclerView(@NonNull RecyclerView recyclerView) { recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS)); } public class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> { private final List<DummyContent.DummyItem> mValues; public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) { mValues = items; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, int position) { holder.mItem = mValues.get(position); holder.mIdView.setText(mValues.get(position).id); holder.mContentView.setText(mValues.get(position).content); } @Override public int getItemCount() { return mValues.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public final View mView; public final TextView mIdView; public final TextView mContentView; public DummyContent.DummyItem mItem; public ViewHolder(View view) { super(view); mView = view; mIdView = (TextView) view.findViewById(R.id.id); mContentView = (TextView) view.findViewById(R.id.content); } @Override public String toString() { return super.toString() + " '" + mContentView.getText() + "'"; } } } } 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/item_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" app:layoutManager="LinearLayoutManager" tools:context="tzig.schm00.myapplication.MainActivity" /> </FrameLayout> 

list_item.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cont_item_root" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardCornerRadius="8dp" card_view:cardElevation="10dp" card_view:cardMaxElevation="10dp" card_view:cardUseCompatPadding="true"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/text_margin" android:textAppearance="?attr/textAppearanceListItem" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/text_margin" android:textAppearance="?attr/textAppearanceListItem" /> </LinearLayout> </android.support.v7.widget.CardView> 

New observation: this seems to be standard behavior. It looks the same as using Image Picker, displaying folders using CardViews ...

Any help is much appreciated!

Thanks!!

+5
source share
2 answers

Add a background to your RelativeLayout - it seems that promotion requires it to work correctly. I checked this with your layout and it works.

0
source

I had the same problem and in my case the problem was caused by the ability to highlight the background that I set in my RecyclerViewAdapter#onBindViewHolder :

 // this spoiled the shadows! cardView.setBackgroundDrawable(ContextCompat.getDrawable(context, myColorResourceId); 

After deleting the line above the problem disappeared!

(To have both background and nice shadows, I used an internal element to fill in CardView and apply the background to it instead of CardView)

0
source

Source: https://habr.com/ru/post/1265055/


All Articles