2015-03-07 11 views
12

मैं दो कॉलम सूची बनाने के लिए StaggeredGridLayoutManager के साथ RecyclerView का उपयोग कर रहा हूं। लेकिन बाएं कॉलम और दाएं कॉलम के बीच सही मार्जिन कैसे सेट करें। मैंने इस कोड का उपयोग शीर्ष से सही मार्जिन करने के लिए किया है, लेकिन स्तंभों के बीच डबल स्पेस को कैसे हल किया जाए।StaggeredGridLayoutManager के साथ RecyclerView का उपयोग करते समय आइटम के बीच डबल स्पेस से कैसे बचें?

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; 
} 

और गतिविधि में:

recyclerView.addItemDecoration(new SpacesItemDecoration(20)); 

मैं view.getX() उपयोग करने के लिए कोशिश की है, यह हमेशा वापस आ 0.

किसी को भी मेरी मदद कर सकते हैं? बहुत धन्यवाद!

उत्तर

3

मैंने आइटम मार्जिन और रीसाइक्लिंग व्यू पैडिंग सेट करके इसे स्वयं हल किया। मार्जिन और पैडिंग दोनों अपेक्षित स्थान के आधे हैं, इसलिए समस्या गायब हो गई।

6

निम्नलिखित कोड StaggeredGridLayoutManager, GridLayoutManager, और LinearLayoutManager को संभालेगा।

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

अपने मामले में आप मेरे मामले में दोनों RecyclerView .लेकिन को मार्जिन की स्थापना कर सकते हैं एक छवि RecyclerView में स्क्रीन की चौड़ाई से मेल नहीं है, और मैं ItemDecoration का उपयोग समस्या को हल करने के लिए।

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

मैं थोड़ा बदल सकते हैं और जवाब के दोनों spanindex के अनुसार ItemDecoration में

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

सेट अलग बायीं/दायीं अंतरिक्ष को संशोधित रिश्तेदार प्रश्न के लिए केवल अच्छी तरह से आइटम दृश्य के लिए काम करता आकार तय किया है जाएगा। यदि आइटम दृश्य में छवि दृश्य सेट wrap_content ऊंचाई में है, तो आइटम दृश्य अवधि की स्थिति बदल सकता है, लेकिन स्पैन इंडेक्स आपकी इच्छानुसार अपडेट नहीं किया गया है, मार्जिन एक गड़बड़ होगी।

मेरा तरीका दृश्य वस्तु के लिए सममित बाएं/दाएं स्थान का उपयोग कर रहा है। paddingLeft = "@ Dimen/अंतरिक्ष" एंड्रॉयड: paddingRight = "@ Dimen/अंतरिक्ष"

<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; 
    } 
} 

फिर RecylerView (बाएं/सही) एंड्रॉयड के लिए एक ही गद्दी सेट

संबंधित मुद्दे