2015-03-07 27 views
12

İki sütunlu liste oluşturmak için RecyclerView ile StaggeredGridLayoutManager kullanıyorum. Ancak sol sütun ve sağ sütun arasında doğru bir marj nasıl ayarlanır. Bu kodu en üstte sağ kenar boşluğu yapmak için kullandım, ancak sütunlar arasındaki çift boşluk nasıl çözülür. RecyclerView with StaggeredGridLayoutManager ile öğeler arasında çift boşluk nasıl önlenir?

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { 
    private int space; 

    public SpacesItemDecoration(int space) { 
     this.space = space; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     outRect.left = space; 
     outRect.right = space; 
     outRect.bottom = space; 

     // Add top margin only for the first or second item to avoid double space between items 
     // Add top margin only for the first or second item to avoid double space between items 
     if((parent.getChildCount() > 0 && parent.getChildPosition(view) == 0) 
      || (parent.getChildCount() > 1 && parent.getChildPosition(view) == 1)) 
     outRect.top = space; 
} 

Ve Faaliyet içinde

: Ben view.getX() kullanmayı denedim

recyclerView.addItemDecoration(new SpacesItemDecoration(20)); 

, her zaman 0.

Herkes bana yardımcı olabilir dönmek? Çok teşekkürler!

cevap

3

Öğe kenar boşluğu ve RecyclerView dolgularını ayarlayarak kendimi çözdüm. Hem marj hem de dolgu, beklenen alanın yarısıdır, bu yüzden problem ortadan kalktı.

6

Aşağıdaki kod, StaggeredGridLayoutManager, GridLayoutManager ve LinearLayoutManager öğelerini ele alacaktır. Senin durumunda

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { 

    private int halfSpace; 

    public SpacesItemDecoration(int space) { 
     this.halfSpace = space/2; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 

     if (parent.getPaddingLeft() != halfSpace) { 
      parent.setPadding(halfSpace, halfSpace, halfSpace, halfSpace); 
      parent.setClipToPadding(false); 
     } 

     outRect.top = halfSpace; 
     outRect.bottom = halfSpace; 
     outRect.left = halfSpace; 
     outRect.right = halfSpace; 
    } 
} 
7

sen benim durumumda RecyclerView .ama için marjı hem ayar yapabilirsiniz orada görüntü RecyclerView ekran genişliğine uyacak ve ben sorunu çözmek için ItemDecoration kullanın.

class ViewItemDecoration extends RecyclerView.ItemDecoration { 

    public ViewItemDecoration() { 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) { 
     super.getItemOffsets(outRect, view, parent, state); 
     int position = parent.getChildAdapterPosition(view); 
     int spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).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; 
       } 
     } 
    } 

} 
0

Biraz değişecek ve sadece boyutu sabit olan öğe görünümü için iyi çalışır spanindex göre ItemDecoration göreli soru için

public class SpacesItemDecoration extends RecyclerView.ItemDecoration { 
    private final int mSpace; 

    public SpacesItemDecoration(int space) { 
     this.mSpace = space; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) { 
     super.getItemOffsets(outRect, view, parent, state); 
     int position = parent.getChildAdapterPosition(view); 
     int spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).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 margin only for the first item to avoid double space between items 
     if (parent.getChildAdapterPosition(view) == 0) { 
      outRect.top = 30; 
      outRect.right = 30; 
     } 
    } 
} 
0

Set farklı sol/sağ boşluk cevap ikisini de değiştirin. Öğe görünümünde, wrap_content öğesinin yükseklik olarak ayarlanmış resim görünümü varsa, öğe görünümü yayılma konumunu değiştirebilir, ancak span dizini dilediğiniz şekilde güncellenmez; kenar boşluğu karışıklık olur.

Yolum, görünüm öğesinin simetrik sol/sağ alanını kullanarak. paddingLeft = "@ Dimen/boşluk" robot: paddingRight = "Dimen/space '"

<android.support.v7.widget.RecyclerView 
     android:id="@+id/img_waterfall" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/space" 
     android:paddingRight="@dimen/space"/> 
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; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 

     StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams(); 

     if (layoutParams.isFullSpan()) { 
      outRect.set(0, 0, 0, 0); 
     } else { 
      int spanIndex = layoutParams.getSpanIndex(); 
      int layoutPosition = layoutParams.getViewLayoutPosition(); 
      int itemCount = parent.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 
      ); 
     } 
    } 
} 
@Override 
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; 
    } 
    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 
           RecyclerView.State state) { 
     outRect.bottom = 2 * space; 
     int pos = parent.getChildAdapterPosition(view); 
     outRect.left = space; 
     outRect.right = space; 
     if (pos < 2) 
      outRect.top = 2 * space; 
    } 
} 

Daha sonra, (sol/sağ) robot RecylerView için aynı dolgu ayarlamak
İlgili konular