android - 为什么我的 RecyclerView 项目之间的间距不一样?

标签 android android-recyclerview

我使用ItemDecoration设置网格 RecyclerView 项目之间的空间,我希望每个项目之间的空间相等。

enter image description here 元素装饰

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
    this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanSize = layoutParams.getSpanSize();
        int spanIndex = layoutParams.getSpanIndex();
        int totalSpanSize = gridLayoutManager.getSpanCount();
        if (spanIndex == 0) {//left
            outRect.left = space;
            outRect.right = space / 2;
        } else if (spanSize + spanIndex == totalSpanSize) {//right
            outRect.right = space;
            outRect.left = space / 2;
        } else if (spanIndex > 0 && spanSize + spanIndex < totalSpanSize) {
            outRect.left = space / 2;
            outRect.right = space / 2;
        }
        outRect.bottom = space;
    }
}

回收站 View

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>

项目 xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#ff6600"
android:orientation="vertical">

</LinearLayout>

最佳答案

试试这个,

int spanCount;
final private Context mContext =YourActivity.this;
int value = activity.getResources().getConfiguration().orientation;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);
    recycler_view=(RecyclerView)findViewById(R.id.recycler_view);

    recycler_view.setLayoutManager(new GridLayoutManager(mContext, 2));
    if (value == Configuration.ORIENTATION_PORTRAIT) {
        spanCount = 2;
    }
    else if (value == Configuration.ORIENTATION_LANDSCAPE) {
        spanCount = 3;
    }
    int spacing_left = 50; // 50px
    int spacing_top=10;
    
    recycler_view.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing_left, spacing_top));
}

@Override
protected void onResume() {

    if (value == Configuration.ORIENTATION_PORTRAIT) {
        spanCount = 2;
    }
    else if (value == Configuration.ORIENTATION_LANDSCAPE) {
        spanCount = 3;
    }

    super.onResume();
}

GridSpacingItemDecoration:

/* set spacing for grid view */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration
{
    final private int spanCount,spacing,spacing_top;
    final private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing_left, int spacing_top)
    {
        this.spanCount = spanCount;
        this.spacing = spacing_left;
        this.includeEdge = true;
        this.spacing_top=spacing_top;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
    {
        int position = parent.getChildAdapterPosition(view); // item phases_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_top;
            }
            outRect.bottom = spacing_top; // 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_top; // item top
            }
        }
    }
}

关于android - 为什么我的 RecyclerView 项目之间的间距不一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43488365/

相关文章:

java - 套接字连接需要android应用程序锁吗?

android - 我需要 getter 和 setter 以及额外的实体构造函数吗?

android - 如何解决 "ADB server didn' t ACK"错误?

java - 什么是 alertcontroller.recyclelistview

android - 动态创建 RecyclerView UI - Android

java - 使用 Gson 将 ArrayList 转换为字符串

java - 安卓UDP丢包

java - android - OnClickListener 不适用于 recyclerview 中的第一次点击

android - 嵌套的 RecyclerView,如 Google Play 商店应用

java - 无法将 getUrl() 传入 onBindViewHolder?