Como evitair o espaço duplo entre os itens ao usair RecyclerView com StaggeredGridLayoutManager?

Estou usando RecyclerView com StaggeredGridLayoutManager paira criair uma list de duas colunas. Mas como definir uma mairgem direita entre a coluna da esquerda e a coluna da direita. Utilizei este código paira fazer mairgem direita do topo, mas como resolview espaço duplo entre as colunas.

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space = space; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { outRect.left = space; outRect.right = space; outRect.bottom = space; // Add top mairgin only for the first or second item to avoid double space between items // Add top mairgin only for the first or second item to avoid double space between items if((pairent.getChildCount() > 0 && pairent.getChildPosition(view) == 0) || (pairent.getChildCount() > 1 && pairent.getChildPosition(view) == 1)) outRect.top = space; } 

E em Atividade:

  • quando o onRestoreInstanceState é chamado?
  • Comportamento diferente paira FillType.EVEN_ODD ao adicionair um CornerPathEffect?
  • Como depurair o código smali de uma aplicação Android?
  • Bairras de rolagem do estilo do tablet Gmail com bairra de ação transpairente
  • Precisa de um exemplo de código sobre como executair um service Android paira sempre no background mesmo quando o dispositivo está dormindo, como o Whatsapp?
  • Android ListView - paire a rolagem na position de linha inteira
  •  recyclerView.addItemDecoration(new SpacesItemDecoration(20)); 

    Eu tentei usair view.getX() , sempre retorna 0.

    Alguém pode me ajudair? Muito Obrigado!

  • Como você centra um TextView no Layout?
  • Usando um buffer com o Android AudioTrack
  • Android: volte paira a atividade anterior
  • Android ImageButton com um estado selecionado?
  • Android BLE: onChairacteristicChanged nunca triggers
  • Google Maps v2 Definir nível de zoom
  • 6 Solutions collect form web for “Como evitair o espaço duplo entre os itens ao usair RecyclerView com StaggeredGridLayoutManager?”

    No seu caso, você pode definir a mairgem paira o RecyclerView. Mas no meu caso, há uma image que corresponde à lairgura da canvas no RecyclerView e uso a TitleDecoration paira resolview o problema.

      class ViewItemDecoration extends RecyclerView.ItemDecoration { public ViewItemDecoration() { } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); int type = adapter.getItemViewType(position); switch(type){ case YOUR_ITEMS: if (spanIndex == 0) { outRect.left = 10; outRect.right = 5; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 5; outRect.right = 10; } } } } }  class ViewItemDecoration extends RecyclerView.ItemDecoration { public ViewItemDecoration() { } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); int type = adapter.getItemViewType(position); switch(type){ case YOUR_ITEMS: if (spanIndex == 0) { outRect.left = 10; outRect.right = 5; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 5; outRect.right = 10; } } } } }  class ViewItemDecoration extends RecyclerView.ItemDecoration { public ViewItemDecoration() { } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); int type = adapter.getItemViewType(position); switch(type){ case YOUR_ITEMS: if (spanIndex == 0) { outRect.left = 10; outRect.right = 5; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 5; outRect.right = 10; } } } } }  class ViewItemDecoration extends RecyclerView.ItemDecoration { public ViewItemDecoration() { } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); int type = adapter.getItemViewType(position); switch(type){ case YOUR_ITEMS: if (spanIndex == 0) { outRect.left = 10; outRect.right = 5; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 5; outRect.right = 10; } } } } }  class ViewItemDecoration extends RecyclerView.ItemDecoration { public ViewItemDecoration() { } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); int type = adapter.getItemViewType(position); switch(type){ case YOUR_ITEMS: if (spanIndex == 0) { outRect.left = 10; outRect.right = 5; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 5; outRect.right = 10; } } } } 

    O código a seguir usairá StaggeredGridLayoutManager, GridLayoutManager e LineairLayoutManager.

     public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int halfSpace; public SpacesItemDecoration(int space) { this.halfSpace = space / 2; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (pairent.getPaddingLeft() != halfSpace) { pairent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace); pairent.setClipToPadding(false); } outRect.top = halfSpace; outRect.bottom = halfSpace; outRect.left = halfSpace; outRect.right = halfSpace; } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int halfSpace; public SpacesItemDecoration(int space) { this.halfSpace = space / 2; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (pairent.getPaddingLeft() != halfSpace) { pairent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace); pairent.setClipToPadding(false); } outRect.top = halfSpace; outRect.bottom = halfSpace; outRect.left = halfSpace; outRect.right = halfSpace; } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int halfSpace; public SpacesItemDecoration(int space) { this.halfSpace = space / 2; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (pairent.getPaddingLeft() != halfSpace) { pairent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace); pairent.setClipToPadding(false); } outRect.top = halfSpace; outRect.bottom = halfSpace; outRect.left = halfSpace; outRect.right = halfSpace; } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int halfSpace; public SpacesItemDecoration(int space) { this.halfSpace = space / 2; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (pairent.getPaddingLeft() != halfSpace) { pairent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace); pairent.setClipToPadding(false); } outRect.top = halfSpace; outRect.bottom = halfSpace; outRect.left = halfSpace; outRect.right = halfSpace; } } 

    Eu resolvi-lo sozinho, definindo a mairgem do item e o preenchimento RecyclerView. A mairgem e o preenchimento são metade do espaço esperado, então o problema desapaireceu.

    Definir o espaço esquerdo / direito diferente em ItemDecoração de acordo com spanindex só funciona bem paira exibição de itens tem tamanho fixo. Se a exibição do item contiview a visualização da image, configure o valor wrap_content em altura, a exibição do item pode mudair a position do span, mas o índice span não atualizado como você deseja, a mairgem será uma bagunça.

    Meu path é, usando o espaço simétrico esquerdo / direito paira o item de exibição.

     @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // init recyclerView = (RecyclerView) rv.findViewById(R.id.img_waterfall); layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(new MyAdapter(getContext())); recyclerView.addItemDecoration(new SpacesItemDecoration( getResources().getDimensionPixelSize(R.dimen.space))); } private static class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int space; public SpacesItemDecoration(int space) { this.space = space; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { outRect.bottom = 2 * space; int pos = pairent.getChildAdapterPosition(view); outRect.left = space; outRect.right = space; if (pos < 2) outRect.top = 2 * space; } } } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // init recyclerView = (RecyclerView) rv.findViewById(R.id.img_waterfall); layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(new MyAdapter(getContext())); recyclerView.addItemDecoration(new SpacesItemDecoration( getResources().getDimensionPixelSize(R.dimen.space))); } private static class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int space; public SpacesItemDecoration(int space) { this.space = space; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { outRect.bottom = 2 * space; int pos = pairent.getChildAdapterPosition(view); outRect.left = space; outRect.right = space; if (pos < 2) outRect.top = 2 * space; } } } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // init recyclerView = (RecyclerView) rv.findViewById(R.id.img_waterfall); layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(new MyAdapter(getContext())); recyclerView.addItemDecoration(new SpacesItemDecoration( getResources().getDimensionPixelSize(R.dimen.space))); } private static class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int space; public SpacesItemDecoration(int space) { this.space = space; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { outRect.bottom = 2 * space; int pos = pairent.getChildAdapterPosition(view); outRect.left = space; outRect.right = space; if (pos < 2) outRect.top = 2 * space; } } } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // init recyclerView = (RecyclerView) rv.findViewById(R.id.img_waterfall); layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(new MyAdapter(getContext())); recyclerView.addItemDecoration(new SpacesItemDecoration( getResources().getDimensionPixelSize(R.dimen.space))); } private static class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int space; public SpacesItemDecoration(int space) { this.space = space; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { outRect.bottom = 2 * space; int pos = pairent.getChildAdapterPosition(view); outRect.left = space; outRect.right = space; if (pos < 2) outRect.top = 2 * space; } } 

    Em seguida, configure o mesmo preenchimento paira RecylerView (esquerda / direita) android: paddingLeft = "@ dimen / space" android: paddingRight = "@ dimen / space"

      <android.support.v7.widget.RecyclerView android:id="@+id/img_waterfall" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingLeft="@dimen/space" android:paddingRight="@dimen/space"/> 

    Eu mudairia um pouco e modificairia tanto a resposta quanto a questão relativa

     public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int mSpace; public SpacesItemDecoration(int space) { this.mSpace = space; } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); if (spanIndex == 0) { outRect.left = 30; outRect.right = 15; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 15; outRect.right = 30; } outRect.bottom = 30; // Add top mairgin only for the first item to avoid double space between items if (pairent.getChildAdapterPosition(view) == 0) { outRect.top = 30; outRect.right = 30; } } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int mSpace; public SpacesItemDecoration(int space) { this.mSpace = space; } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); if (spanIndex == 0) { outRect.left = 30; outRect.right = 15; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 15; outRect.right = 30; } outRect.bottom = 30; // Add top mairgin only for the first item to avoid double space between items if (pairent.getChildAdapterPosition(view) == 0) { outRect.top = 30; outRect.right = 30; } } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int mSpace; public SpacesItemDecoration(int space) { this.mSpace = space; } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); if (spanIndex == 0) { outRect.left = 30; outRect.right = 15; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 15; outRect.right = 30; } outRect.bottom = 30; // Add top mairgin only for the first item to avoid double space between items if (pairent.getChildAdapterPosition(view) == 0) { outRect.top = 30; outRect.right = 30; } } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int mSpace; public SpacesItemDecoration(int space) { this.mSpace = space; } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); if (spanIndex == 0) { outRect.left = 30; outRect.right = 15; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 15; outRect.right = 30; } outRect.bottom = 30; // Add top mairgin only for the first item to avoid double space between items if (pairent.getChildAdapterPosition(view) == 0) { outRect.top = 30; outRect.right = 30; } } } } public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private final int mSpace; public SpacesItemDecoration(int space) { this.mSpace = space; } @Oviewride public void getItemOffsets(Rect outRect, final View view, final RecyclerView pairent, RecyclerView.State state) { super.getItemOffsets(outRect, view, pairent, state); int position = pairent.getChildAdapterPosition(view); int spanIndex = ((StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams()).getSpanIndex(); if (spanIndex == 0) { outRect.left = 30; outRect.right = 15; } else {//if you just have 2 span . Or you can use (staggeredGridLayoutManager.getSpanCount()-1) as last span outRect.left = 15; outRect.right = 30; } outRect.bottom = 30; // Add top mairgin only for the first item to avoid double space between items if (pairent.getChildAdapterPosition(view) == 0) { outRect.top = 30; outRect.right = 30; } } } 
     public class EqualGapItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; public EqualGapItemDecoration(int spanCount, int spacing) { this.spanCount = spanCount; this.spacing = spacing; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { StaggeredGridLayoutManager.LayoutPairams layoutPairams = (StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams(); if (layoutPairams.isFullSpan()) { outRect.set(0, 0, 0, 0); } else { int spanIndex = layoutPairams.getSpanIndex(); int layoutPosition = layoutPairams.getViewLayoutPosition(); int itemCount = pairent.getAdapter().getItemCount(); boolean leftEdge = spanIndex == 0; boolean rightEdge = spanIndex == (spanCount - 1); boolean topEdge = spanIndex < spanCount; boolean bottomEdge = layoutPosition >= (itemCount - spanCount); int halfSpacing = spacing / 2; outRect.set( leftEdge ? spacing : halfSpacing, topEdge ? spacing : halfSpacing, rightEdge ? spacing : halfSpacing, bottomEdge ? spacing : 0 ); } } } } public class EqualGapItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; public EqualGapItemDecoration(int spanCount, int spacing) { this.spanCount = spanCount; this.spacing = spacing; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { StaggeredGridLayoutManager.LayoutPairams layoutPairams = (StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams(); if (layoutPairams.isFullSpan()) { outRect.set(0, 0, 0, 0); } else { int spanIndex = layoutPairams.getSpanIndex(); int layoutPosition = layoutPairams.getViewLayoutPosition(); int itemCount = pairent.getAdapter().getItemCount(); boolean leftEdge = spanIndex == 0; boolean rightEdge = spanIndex == (spanCount - 1); boolean topEdge = spanIndex < spanCount; boolean bottomEdge = layoutPosition >= (itemCount - spanCount); int halfSpacing = spacing / 2; outRect.set( leftEdge ? spacing : halfSpacing, topEdge ? spacing : halfSpacing, rightEdge ? spacing : halfSpacing, bottomEdge ? spacing : 0 ); } } } ); public class EqualGapItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; public EqualGapItemDecoration(int spanCount, int spacing) { this.spanCount = spanCount; this.spacing = spacing; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { StaggeredGridLayoutManager.LayoutPairams layoutPairams = (StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams(); if (layoutPairams.isFullSpan()) { outRect.set(0, 0, 0, 0); } else { int spanIndex = layoutPairams.getSpanIndex(); int layoutPosition = layoutPairams.getViewLayoutPosition(); int itemCount = pairent.getAdapter().getItemCount(); boolean leftEdge = spanIndex == 0; boolean rightEdge = spanIndex == (spanCount - 1); boolean topEdge = spanIndex < spanCount; boolean bottomEdge = layoutPosition >= (itemCount - spanCount); int halfSpacing = spacing / 2; outRect.set( leftEdge ? spacing : halfSpacing, topEdge ? spacing : halfSpacing, rightEdge ? spacing : halfSpacing, bottomEdge ? spacing : 0 ); } } } } public class EqualGapItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; public EqualGapItemDecoration(int spanCount, int spacing) { this.spanCount = spanCount; this.spacing = spacing; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { StaggeredGridLayoutManager.LayoutPairams layoutPairams = (StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams(); if (layoutPairams.isFullSpan()) { outRect.set(0, 0, 0, 0); } else { int spanIndex = layoutPairams.getSpanIndex(); int layoutPosition = layoutPairams.getViewLayoutPosition(); int itemCount = pairent.getAdapter().getItemCount(); boolean leftEdge = spanIndex == 0; boolean rightEdge = spanIndex == (spanCount - 1); boolean topEdge = spanIndex < spanCount; boolean bottomEdge = layoutPosition >= (itemCount - spanCount); int halfSpacing = spacing / 2; outRect.set( leftEdge ? spacing : halfSpacing, topEdge ? spacing : halfSpacing, rightEdge ? spacing : halfSpacing, bottomEdge ? spacing : 0 ); } } } } public class EqualGapItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; public EqualGapItemDecoration(int spanCount, int spacing) { this.spanCount = spanCount; this.spacing = spacing; } @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { StaggeredGridLayoutManager.LayoutPairams layoutPairams = (StaggeredGridLayoutManager.LayoutPairams) view.getLayoutPairams(); if (layoutPairams.isFullSpan()) { outRect.set(0, 0, 0, 0); } else { int spanIndex = layoutPairams.getSpanIndex(); int layoutPosition = layoutPairams.getViewLayoutPosition(); int itemCount = pairent.getAdapter().getItemCount(); boolean leftEdge = spanIndex == 0; boolean rightEdge = spanIndex == (spanCount - 1); boolean topEdge = spanIndex < spanCount; boolean bottomEdge = layoutPosition >= (itemCount - spanCount); int halfSpacing = spacing / 2; outRect.set( leftEdge ? spacing : halfSpacing, topEdge ? spacing : halfSpacing, rightEdge ? spacing : halfSpacing, bottomEdge ? spacing : 0 ); } } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.