我正在尝试在RecyclerView
的项目之间放置相等的空间。为此,我使用下面的SpanningLinearLayoutManager
。它的作用是,它将自动在RecyclerView
中的项目之间分配空间,但会使RecyclerView
不可滚动。所有项目都将在父项的宽度之内。这适用于5或6个项目。但是,如果列表中包含约10个元素,则所有元素之间都将变得非常接近。我想平均分配项目之间的空间,并使recyclerview
滚动方式。
我在父母RecyclerView
内有一个孩子RecyclerView
。我想在子RecyclerView
这是我的代码:
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class SpanningLinearLayoutManager extends LinearLayoutManager {
public SpanningLinearLayoutManager(Context context) {
super(context);
}
public SpanningLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpanningLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return spanLayoutSize(super.generateDefaultLayoutParams());
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs) {
return spanLayoutSize(super.generateLayoutParams(c, attrs));
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return spanLayoutSize(super.generateLayoutParams(lp));
}
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return super.checkLayoutParams(lp);
}
private RecyclerView.LayoutParams spanLayoutSize(RecyclerView.LayoutParams layoutParams){
if(getOrientation() == HORIZONTAL){
layoutParams.width = (int) Math.round(getHorizontalSpace() / (double) getItemCount());
}
else if(getOrientation() == VERTICAL){
layoutParams.height = (int) Math.round(getVerticalSpace() / (double) getItemCount());
}
return layoutParams;
}
@Override
public boolean canScrollVertically() {
return false;
}
@Override
public boolean canScrollHorizontally() {
return false;
}
private int getHorizontalSpace() {
return getWidth() - getPaddingRight() - getPaddingLeft();
}
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
}
答案 0 :(得分:0)
声明这两个变量flagDecoration
和mItemDecoration
:
private var flagDecoration = false
private var mItemDecoration: RecyclerView.ItemDecoration = object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.left = 20 //use the value as per your need
outRect.right = 20
outRect.top = 20
outRect.bottom = 20
flagDecoration = true
}
}
在您的recyclerView
中添加它。
if (!flagDecoration) {
recyclerView.addItemDecoration(mItemDecoration)
}
答案 1 :(得分:0)
您不需要自定义LayoutManager即可实现此目的。您可以使用LinearLayoutManager获得所需的结果。
您需要为每个子项目增加间距的是ItemDecorator。
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration?hl=en
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.top = spacing;
}
..
recyclerView.layoutManager = LinearLayoutManager(..)
recyclerView.addItemDecoration(MyItemDecorator())