之間添加分隔符我試圖在回收站視圖中的每個列表項之間添加一些空間,但由於某些原因它不能正常工作。我一直在遵循一個指南來實現這個解決方案,但似乎無法找到答案。它增加了空間,但空間放置在下一個列表項目上。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;
}
我已經修改了類,但我不認爲網格間距將工作完全像我想在recyclerview,因爲列表項被移動到左側和右側。有沒有辦法忘記左右間距,只考慮頂部和底部? – Nero