2017-07-26 51 views
0

之間添加分隔符我試圖在回收站視圖中的每個列表項之間添加一些空間,但由於某些原因它不能正常工作。我一直在遵循一個指南來實現這個解決方案,但似乎無法找到答案。它增加了空間,但空間放置在下一個列表項目上。RecyclerView SimpleItemDivider不在

這是我的recyclerview目前看起來像: - My RecyclerView

這裏是我的類: - divider.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <size 
     android:width="40dp" 
     android:height="40dp" /> 

    <solid 
     android:color="@color/white"/> 

</shape> 

SimpleDividerItemDecoration類

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration { 

    private Drawable mDivider; 

    public SimpleDividerItemDecoration(Context context) { 
     mDivider = ContextCompat.getDrawable(context,R.drawable.divider); 
    } 

    @Override 
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
     int left = parent.getPaddingLeft(); 
     int right = parent.getWidth() - parent.getPaddingRight(); 

     int childCount = parent.getChildCount(); 
     for (int i = 0; i < childCount; i++) { 
      View child = parent.getChildAt(i); 

      RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 

      int top = child.getBottom() + params.bottomMargin; 
      int bottom = top + mDivider.getIntrinsicHeight(); 

      mDivider.setBounds(left, top, right, bottom); 
      mDivider.draw(c); 
     } 
    } 
} 

onCreate方法在我我得到物品的片段類

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_popular_picks, container, false); 

     recyclerView = (RecyclerView)v.findViewById(R.id.popular_pick_recyclerView); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
     recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext())); 

     getPopularPost(); 

     return v; 
    } 

回答

0

如果您只需要添加空間,請考慮將其添加到項目佈局(列表項的.xml文件)。這可以是RecyclerView中每個項目的一些填充。

如果您需要添加正規分隔線,請考慮使用默認的DividerItemDecoration。它可從appcompat庫25.0.0獲得。

您可以用這種方式添加:

mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), 
     mLayoutManager.getOrientation()); 
recyclerView.addItemDecoration(mDividerItemDecoration); 
0

添加以下代碼行,希望它能夠解決您的問題

recyclerView.addItemDecoration(新GridSpacingItemDecoration(2 dpToPx(10),真) );

,這裏的類GridSpacingItemDecoration

/** * 項目RecyclerView裝飾 - 給周圍的網格項等於利潤率 */

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { 

    private int spanCount; 
    private int spacing; 
    private boolean includeEdge; 

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { 
     this.spanCount = spanCount; 
     this.spacing = spacing; 
     this.includeEdge = includeEdge; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     int position = parent.getChildAdapterPosition(view); // item position 
     int column = position % spanCount; // item column 

     if (includeEdge) { 
      outRect.left = spacing - column * spacing/spanCount; // spacing - column * ((1f/spanCount) * spacing) 
      outRect.right = (column + 1) * spacing/spanCount; // (column + 1) * ((1f/spanCount) * spacing) 

      if (position < spanCount) { // top edge 
       outRect.top = spacing; 
      } 
      outRect.bottom = spacing; // item bottom 
     } else { 
      outRect.left = column * spacing/spanCount; // column * ((1f/spanCount) * spacing) 
      outRect.right = spacing - (column + 1) * spacing/spanCount; // spacing - (column + 1) * ((1f/ spanCount) * spacing) 
      if (position >= spanCount) { 
       outRect.top = spacing; // item top 
      } 
     } 
    } 
} 

/** 
* Converting dp to pixel 
*/ 
private int dpToPx(int dp) { 
    Resources r = getResources(); 
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics())); 
} 
+0

我已經修改了類,但我不認爲網格間距將工作完全像我想在recyclerview,因爲列表項被移動到左側和右側。有沒有辦法忘記左右間距,只考慮頂部和底部? – Nero