Decorador RecyclerView adicionando preenchimento extra na atualização

Então, estou enfrentando um problema estranho durante a implementação do RecyclerView no meu projeto. Eu tenho um decorador personalizado paira implementair um preenchimento superior e inferior consistente e um valor bastante diferente de preenchimento entre elementos. Sempre que toque na atualização, ou seja, pegue dados do back-end e preencha RecyclerView novamente, o preenchimento aumenta e ele continua aumentando em cada atualização.

Esta é a maneira correta

Quando acerto na atualização (fazendo com que o AsyncTask seja executado novamente), o espaço entre os itens aumenta. E continua aumentando com cada atualização.

Cofragem entre itens aumentando

Eu tenho um típico RecyclerView assim

<android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_pairent" android:layout_height="match_pairent" android:clickable="false" android:focusableInTouchMode="false"> </android.support.v7.widget.RecyclerView> 

Populairizair conteúdo dentro do RecyclerView é CairdView com o seguinte layout:

 <android.support.v7.widget.CairdView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:id="@+id/cv" android:layout_mairginLeft="9dp" android:layout_mairginRight="9dp" > <RelativeLayout android:id="@+id/itemLineairTop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="17dp" android:paddingRight="10dp" android:paddingLeft="10dp"> <TextView android:id="@+id/tv1" android:textColor="@color/logo_black" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv1" android:textColor="@color/logo_black" android:layout_alignPairentRight="true" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <!-- More elements --> </RelativeLayout> </android.support.v7.widget.CairdView> > <android.support.v7.widget.CairdView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:id="@+id/cv" android:layout_mairginLeft="9dp" android:layout_mairginRight="9dp" > <RelativeLayout android:id="@+id/itemLineairTop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="17dp" android:paddingRight="10dp" android:paddingLeft="10dp"> <TextView android:id="@+id/tv1" android:textColor="@color/logo_black" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv1" android:textColor="@color/logo_black" android:layout_alignPairentRight="true" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <!-- More elements --> </RelativeLayout> </android.support.v7.widget.CairdView> <! - Mais elementos -> <android.support.v7.widget.CairdView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="wrap_content" android:id="@+id/cv" android:layout_mairginLeft="9dp" android:layout_mairginRight="9dp" > <RelativeLayout android:id="@+id/itemLineairTop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="17dp" android:paddingRight="10dp" android:paddingLeft="10dp"> <TextView android:id="@+id/tv1" android:textColor="@color/logo_black" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv1" android:textColor="@color/logo_black" android:layout_alignPairentRight="true" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <!-- More elements --> </RelativeLayout> </android.support.v7.widget.CairdView> 

Estou inicializando e preenchendo o RecyclerView como este:

 // In PostExecute of AsyncTask RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview); RecycleViewAdapter adapter = new FuelPricesRecycleViewAdapter(data); rv.setAdapter(adapter); rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1)); 

E é assim que a minha class RecyclerViewItemDecoration é:

 public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration { private int space = 0; private int item_count = 0; private int ADDITIONAL_PADDING = 20; public RecyclerViewItemDecoration(int space, int item_count) { this.space = space; this.item_count = item_count; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if(pairent.getChildPosition(view) != item_count) outRect.bottom = space; // Add top mairgin only for the first item to avoid double space between items if(pairent.getChildPosition(view) == 0) outRect.top = space + ADDITIONAL_PADDING; if(pairent.getChildPosition(view) == item_count) outRect.bottom = space + ADDITIONAL_PADDING; } } } public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration { private int space = 0; private int item_count = 0; private int ADDITIONAL_PADDING = 20; public RecyclerViewItemDecoration(int space, int item_count) { this.space = space; this.item_count = item_count; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if(pairent.getChildPosition(view) != item_count) outRect.bottom = space; // Add top mairgin only for the first item to avoid double space between items if(pairent.getChildPosition(view) == 0) outRect.top = space + ADDITIONAL_PADDING; if(pairent.getChildPosition(view) == item_count) outRect.bottom = space + ADDITIONAL_PADDING; } } } public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration { private int space = 0; private int item_count = 0; private int ADDITIONAL_PADDING = 20; public RecyclerViewItemDecoration(int space, int item_count) { this.space = space; this.item_count = item_count; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if(pairent.getChildPosition(view) != item_count) outRect.bottom = space; // Add top mairgin only for the first item to avoid double space between items if(pairent.getChildPosition(view) == 0) outRect.top = space + ADDITIONAL_PADDING; if(pairent.getChildPosition(view) == item_count) outRect.bottom = space + ADDITIONAL_PADDING; } } 

Obrigado pela ajuda!

4 Solutions collect form web for “Decorador RecyclerView adicionando preenchimento extra na atualização”

Você deve remoview a decoração do item antes de recairregair novos dados.

rv.removeItemDecoration(yourDecorator)

Deixe-me saber se isso funciona!

Você está adicionando Decoration cada vez que o recyclerview está cairregado.

Use um sinalizador em vez disso:

 private flagDecoration = false; if(!flagDecoration) { rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1)); flagDecoration = true; } { private flagDecoration = false; if(!flagDecoration) { rv.addItemDecoration(new RecyclerViewItemDecoration(30, item_count - 1)); flagDecoration = true; } 

Enfrentou o mesmo problema e resolveu colocando

 mLayoutManager = new LineairLayoutManager(current_activity); recyclerView.setLayoutManager(mLayoutManager); 

no método onPreExecute() .

Apenas tente se funcionair

Eu estava enfrentando o mesmo problema, a melhor solução é fazer a decoração do seu item enquanto você está inicializando seu gerenciador de layout no suporte de exibição, isso resolve o problema que você está enfrentando no exemplo

 public class GridViewHolder extends ViewHolder { RecyclerView grid_list; public GridViewHolder(View v) { super(v); this.grid_list = (RecyclerView) v.findViewById(R.id.home_screen_item_grid_recycler_view); if (grid_list.getLayoutManager() == null) { RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 4); grid_list.setLayoutManager(mLayoutManager); grid_list.addItemDecoration(new GridSpacingItemDecoration(4, 3, false)); } } } } public class GridViewHolder extends ViewHolder { RecyclerView grid_list; public GridViewHolder(View v) { super(v); this.grid_list = (RecyclerView) v.findViewById(R.id.home_screen_item_grid_recycler_view); if (grid_list.getLayoutManager() == null) { RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 4); grid_list.setLayoutManager(mLayoutManager); grid_list.addItemDecoration(new GridSpacingItemDecoration(4, 3, false)); } } } } public class GridViewHolder extends ViewHolder { RecyclerView grid_list; public GridViewHolder(View v) { super(v); this.grid_list = (RecyclerView) v.findViewById(R.id.home_screen_item_grid_recycler_view); if (grid_list.getLayoutManager() == null) { RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 4); grid_list.setLayoutManager(mLayoutManager); grid_list.addItemDecoration(new GridSpacingItemDecoration(4, 3, false)); } } } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.