GridLayoutManager 不居中对齐问题

Paste_Image.png

蛋疼的问题,右边留空那么多。

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#ff0"
    >

</android.support.v7.widget.RecyclerView>

item项的布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:background="#f0f"
    android:gravity="center"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="52dp"
        android:layout_height="52dp"
        app:srcCompat="@mipmap/home_new"
        android:id="@+id/iv_icon" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="#4A4A4A"
        android:layout_marginTop="10dp"
        android:id="@+id/tv_name" />
</LinearLayout>

设置GridLayoutManager 设置一行显示4个

                GridLayoutManager  gridLayoutManager = new GridLayoutManager(container.getContext(), 4);
                recyclerView.setLayoutManager(gridLayoutManager);//一行显示4个

效果就成上面那样蛋疼了,经分析后,发现调整下item的根LinearLayout 的宽属性即可

android:layout_width="match_parent"
Paste_Image.png

效果如下

Paste_Image.png

若需要间距,就配置一下ItemDecoration


Paste_Image.png
    class MarginDecoration extends RecyclerView.ItemDecoration {
        private int margin;

        public MarginDecoration(Context context) {
            margin = PxUtils.dpToPx(10,context);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.set(margin, margin, margin, margin);
        }
    }


作者:浩运
链接:https://www.jianshu.com/p/a6cd97d8646c
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让 `GridLayoutManager` 水平居中,可以使用自定义的 `CenterGridLayoutManager` 类,如下所示: ```java public class CenterGridLayoutManager extends GridLayoutManager { public CenterGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public CenterGridLayoutManager(Context context, int spanCount) { super(context, spanCount); } public CenterGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) { super(context, spanCount, orientation, reverseLayout); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { super.onLayoutChildren(recycler, state); if (getItemCount() == 0) { return; } int viewWidth = getWidth(); int viewHeight = getHeight(); int childWidth = getDecoratedMeasuredWidth(getChildAt(0)); int childHeight = getDecoratedMeasuredHeight(getChildAt(0)); int spanCount = getSpanCount(); int rowCount = getItemCount() / spanCount + (getItemCount() % spanCount == 0 ? 0 : 1); int horizontalSpace = getDecoratedWidth(getChildAt(0)) * (spanCount - 1); int verticalSpace = getDecoratedHeight(getChildAt(0)) * (rowCount - 1); int totalWidth = childWidth * spanCount + horizontalSpace; int totalHeight = childHeight * rowCount + verticalSpace; int left = (viewWidth - totalWidth) / 2; int top = (viewHeight - totalHeight) / 2; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); int position = getPosition(child); int row = position / spanCount; int col = position % spanCount; int leftPos = left + col * childWidth; int topPos = top + row * childHeight; layoutDecorated(child, leftPos, topPos, leftPos + childWidth, topPos + childHeight); } } } ``` 该类继承了 `GridLayoutManager`,并在 `onLayoutChildren` 方法中实现了居中布局的逻辑。在 `onLayoutChildren` 方法中,先计算出所有子项的总宽度和总高度,然后计算出左边和上边的偏移量,最后遍历所有子项,分别设置子项的位置。设置子项位置时,根据子项所在的行列计算出左上角和右下角的坐标,然后调用 `layoutDecorated` 方法设置子项的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值