Recyclerview - GridLayoutManager:组方维 [英] Recyclerview - GridLayoutManager: Set square dimensions

查看:849
本文介绍了Recyclerview - GridLayoutManager:组方维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与方形图像的网格布局。我认为必须有可能通过操纵onMeasure做给maniuplate的GridLayoutManager

super.onMeasure(回收,州,widthSpec,widthSpec);

而不是

super.onMeasure(回收,州,widthSpec,heightSpec);

但遗憾的是没有工作。任何想法?

解决方案

要在我的RecyclerView广场的元素,我提供一个简单的包装,我的根视图元素;我用下面的 SquareRelativeLayout 代替 RelativeLayout的的。

 包net.simplyadvanced.widget;

进口android.content.Context;
进口android.util.AttributeSet;
进口android.widget.RelativeLayout;

/ **一个RelativeLayout的,将永远是正方形 - 相同的宽度和高度,
 *其中高度是基于离宽度。 * /
公共类SquareRelativeLayout扩展RelativeLayout的{

    公共SquareRelativeLayout(上下文的背景下){
        超(上下文);
    }

    公共SquareRelativeLayout(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共SquareRelativeLayout(上下文的背景下,ATTRS的AttributeSet,诠释defStyleAttr){
        超(背景下,ATTRS,defStyleAttr);
    }

    @TargetApi(VERSION_ codeS.LOLLIPOP)
    公共SquareRelativeLayout(上下文的背景下,ATTRS的AttributeSet,诠释defStyleAttr,诠释defStyleRes){
        超(背景下,ATTRS,defStyleAttr,defStyleRes);
    }

    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
        //设置一个方形的布局。
        super.onMeasure(widthMeasureSpec,widthMeasureSpec);
    }

}
 

于是,在我的适配器XML布局,我刚刚提到的自定义视图显示在下面。虽然,你可以做到这一点编程也。

 < XML版本=1.0编码=UTF-8&GT?;
< net.simplyadvanced.widget.SquareRelativeLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / elementRootView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>

    <! - 更多的小部件在这里。 - >

< /net.simplyadvanced.widget.SquareRelativeLayout>
 

注:根据所定位网格的话,那么你可能要根据关高度有宽度( GridLayoutManager.HORIZONTAL ),而不是被关基础的高度宽度( GridLayoutManager.VERTICAL )。

I try to make a grid-layout with square images. I thought that it must be possible to maniuplate the GridLayoutManager by manipulating onMeasure to do a

super.onMeasure(recycler, state, widthSpec, widthSpec);

instead of

super.onMeasure(recycler, state, widthSpec, heightSpec);

but unfortunately that didnt work. Any ideas?

解决方案

To have the square elements in my RecyclerView, I provide a simple wrapper for my root View element; I use the following SquareRelativeLayout in place of RelativeLayout.

package net.simplyadvanced.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/** A RelativeLayout that will always be square -- same width and height,
 * where the height is based off the width. */
public class SquareRelativeLayout extends RelativeLayout {

    public SquareRelativeLayout(Context context) {
        super(context);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(VERSION_CODES.LOLLIPOP)
    public SquareRelativeLayout(Context context, AttributeSet attrs,         int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Set a square layout.
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

}

Then, in my XML layout for the adapter, I've just referenced the custom view as shown in the following. Though, you can do this programmatically also.

<?xml version="1.0" encoding="utf-8"?>
<net.simplyadvanced.widget.SquareRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/elementRootView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- More widgets here. -->

</net.simplyadvanced.widget.SquareRelativeLayout>

Note: Depending on which orientation your grid is, then you may want to have the width based off of height (GridLayoutManager.HORIZONTAL) instead of the height being based off the width (GridLayoutManager.VERTICAL).

这篇关于Recyclerview - GridLayoutManager:组方维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆