android-discuss icon indicating copy to clipboard operation
android-discuss copied to clipboard

【问答】怎么限制FlexboxLayout的行数呢?

Open glad51 opened this issue 7 years ago • 13 comments

就是要限制 FlexboxLayout的行数为4行,多出来的数据就不在显示。我现在用获取FlexboxLayout的高度和子控件的高度来判断,是否添加到FlexboxLayout。但是,FlexboxLayout的getMeasuredHeight 一直是109。没有变化。有没有人好的想法呢?

glad51 avatar Aug 04 '17 03:08 glad51

目前好像没有对应的办法 ,不过如果子元素高度知道的会 ,设置一下maxheight 可以满足

Fengcoder avatar Aug 12 '17 07:08 Fengcoder

解决了吗?我也遇到这个问题

yubaokang avatar Sep 26 '17 15:09 yubaokang

如果业务只是需要 FlexboxLayout 的流式布局,又希望限制行高的话,用你现在的方式还不如直接自定义 ViewGroup 来实现。

另外,FlexboxLayout 的 issue 里面也有人提出希望有一个 maxline 的属性

diov avatar Sep 27 '17 02:09 diov

也不能获取flexboxlayout的行数。

ghost avatar Aug 23 '18 02:08 ghost

也不能获取flexboxlayout的行数。

1.0.0已经支持了

syzhugh avatar Oct 11 '18 09:10 syzhugh

设置了MAXLINE=2.第2行后面显示不下的ITEM 都挤压到第2行了,是我配置不对吗? 我是 1.0 版本 FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this); flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP); // flexboxLayoutManager.setFlexDirection(FlexDirection.ROW); //主轴项目排列方向 flexboxLayoutManager.setAlignItems(AlignItems.CENTER); flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START); //对齐的方式,居中 flexboxLayoutManager.setMaxLine(2); // 配合RECYCLERVIEW

AnyLifeZLB avatar Dec 20 '18 03:12 AnyLifeZLB

设置了MAXLINE=2.第2行后面显示不下的ITEM 都挤压到第2行了,是我配置不对吗? FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this); flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP); // flexboxLayoutManager.setFlexDirection(FlexDirection.ROW); //主轴项目排列方向 flexboxLayoutManager.setAlignItems(AlignItems.CENTER); flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START); //对齐的方式,居中 flexboxLayoutManager.setMaxLine(2); // 配合RECYCLERVIEW

好像有这个问题。。。那么setMaxLine并没什么卵用的样子 我这边是重写layoutmanager,一个一个测量摆放了。过程就跟viewGroup摆放控件似的。

syzhugh avatar Dec 21 '18 08:12 syzhugh

tAlignItems(AlignItems.CENTER); flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START); //对齐的方式,居中 flexboxLayoutManager.setMaxL

设置了MAXLINE=2.第2行后面显示不下的ITEM 都挤压到第2行了,是我配置不对吗? FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this); flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP); // flexboxLayoutManager.setFlexDirection(FlexDirection.ROW); //主轴项目排列方向 flexboxLayoutManager.setAlignItems(AlignItems.CENTER); flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START); //对齐的方式,居中 flexboxLayoutManager.setMaxLine(2); // 配合RECYCLERVIEW

好像有这个问题。。。那么setMaxLine并没什么卵用的样子 我这边是重写layoutmanager,一个一个测量摆放了。过程就跟viewGroup摆放控件似的。

你好,可以参考下代码吗

wangqm1993 avatar Dec 24 '18 03:12 wangqm1993

check this: https://github.com/google/flexbox-layout/issues/156#issuecomment-467769623

oyty avatar Feb 27 '19 08:02 oyty

可以设置RecyclerView的高度来限制行数,android:layout_height="xxdp"

ReeCod avatar Sep 28 '20 06:09 ReeCod

如何限制超过两行末尾加省略号呢?

githubZYQ avatar Dec 17 '20 09:12 githubZYQ

这是我的解决方案:

/**
 * FlexboxLayout设置maxLines之后,如果item超出maxLines,会全部罗列在最后一行,不符合需求;
 *
 * FlexBoxLayoutMaxLines支持设置MaxLines,并截断超出MaxLines的内容;
 */
public class FlexBoxLayoutMaxLines extends FlexboxLayout {
    private int maxLines = NOT_SET;

    @Override
    public void setMaxLine(int maxLine) {
        maxLines = maxLine;
    }

    public int getMaxLines() {
        return maxLines;
    }

    /**
     * see {@link #getMaxLines()}
     */
    @Deprecated
    @Override
    public int getMaxLine() {
        return NOT_SET;
    }

    public FlexBoxLayoutMaxLines(Context context) {
        this(context, null);
    }

    public FlexBoxLayoutMaxLines(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlexBoxLayoutMaxLines(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setMaxLine(super.getMaxLine());
        super.setMaxLine(NOT_SET);
    }

    @Override
    public List<FlexLine> getFlexLinesInternal() {
        List<FlexLine> flexLines = super.getFlexLinesInternal();
        int size = flexLines.size();
        if (maxLines > 0 && size > maxLines) {
            flexLines.subList(maxLines, size).clear();
        }
        return flexLines;
    }
}

完美解决

jinweime avatar Aug 17 '21 07:08 jinweime