在Android中如何获取流式布局的行数

huangapple go评论63阅读模式
英文:

how to get flow layout's row count in android

问题

我有3个视图,它们包含在约束布局的流助手中,并排列如下:

|TextView1| |View(分隔符)| |TextView2|

当TextView2的文本不适合时,它会移到下一行:

|TextView1| |View(分隔符)|
|TextView2|

这里的问题是,当TextView2移到下一行时,我需要删除或将View(分隔符)的可见性设置为GONE,为此我需要一种方法来知道流式布局是否有多于一行。

我尝试查找流助手内的内置函数,但没有找到任何内容。

英文:

I have 3 views that i included in flow helper of constraint layout and are laid out like this:

|TextView1| |View(delimiter)| |TextView2|

when text of TextView2 doesn't fit, it moves to next line:

|TextView1| |View(delimiter)|
|TextView2|

the problem here is that when TextView2 moves to next line i need to remove or make View(delimiter) visibility as gone, and to do so i need a way to know if flow layout has more than one row.

i tried to look for built-in functions inside flow helper but found nothing.

答案1

得分: 1

目前没有获取“流式布局(Flow Layout)”行数的方法,但您可以通过创建一个androidx.constraintlayout.helper.widget.Flow的子类并覆盖函数fun updatePostLayout(container: ConstraintLayout)来实现类似的效果。该函数在您想要在布局后更新其内部对象或为指定的元素设置连接时使用。在此方法中,您可以编写自定义逻辑以相应地更改视图(分隔符)的可见性。以下是CustomFlow子类的示例:

class CustomFlow : Flow {

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun updatePreLayout(container: ConstraintLayout) {
        super.updatePreLayout(container)
    }

    override fun updatePostLayout(container: ConstraintLayout) {
        super.updatePostLayout(container)

        val tv1 = container.getViewById(R.id.textView1) as TextView
        val delView = container.getViewById(R.id.delimiterView) as View
        val tv2 = container.getViewById(R.id.textView2) as TextView

        if (tv2.top != tv1.top || tv2.lineCount > 1) {
            delView.visibility = GONE
        } else {
            delView.visibility = VISIBLE
        }
    }
}

并且可以在您的XML文件中像下面这样使用:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[TEXTVIEW 1 TEXT]"
        tools:ignore="MissingConstraints" />

    <View
        android:id="@+id/delimiterView"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@android:color/holo_green_light"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[TEXTVIEW 2 TEXT]"
        tools:ignore="MissingConstraints" />

    <com.mypackage.name.CustomFlow
        android:id="@+id/flow"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:constraint_referenced_ids="textView1,delimiterView,textView2"
        app:flow_horizontalBias="0"
        app:flow_horizontalGap="10dp"
        app:flow_horizontalStyle="packed"
        app:flow_verticalBias="0"
        app:flow_wrapMode="chain"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

当TextView2较短时的结果(View-分隔符可见):

在Android中如何获取流式布局的行数

当TextView2很长并换行时的结果(View-分隔符消失):

在Android中如何获取流式布局的行数

英文:

Currently there is no method to get the Flow Layout row count but you can achieve this kind of effect by creating a subclass of androidx.constraintlayout.helper.widget.Flow and override the function fun updatePostLayout(container: ConstraintLayout) which is used when you want to update its internal object post layout or set up connections for the pointed elements. In this method you can have your custom logics to change the visibility of the View(delimiter) accordingly. Below is an example of the CustomFlow subclass:

class CustomFlow : Flow {

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun updatePreLayout(container: ConstraintLayout) {
        super.updatePreLayout(container)
    }

    override fun updatePostLayout(container: ConstraintLayout) {
        super.updatePostLayout(container)

        val tv1 = container.getViewById(R.id.textView1) as TextView
        val delView = container.getViewById(R.id.delimiterView) as View
        val tv2 = container.getViewById(R.id.textView2) as TextView

        if (tv2.top != tv1.top || tv2.lineCount &gt; 1) {
            delView.visibility = GONE
        } else {
            delView.visibility = VISIBLE
        }
    }
}

And can be used in your xml like below:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/textView1&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;[TEXTVIEW 1 TEXT]&quot;
        tools:ignore=&quot;MissingConstraints&quot; /&gt;

    &lt;View
        android:id=&quot;@+id/delimiterView&quot;
        android:layout_width=&quot;100dp&quot;
        android:layout_height=&quot;50dp&quot;
        android:background=&quot;@android:color/holo_green_light&quot;
        tools:ignore=&quot;MissingConstraints&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/textView2&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;[TEXTVIEW 2 TEXT]&quot;
        tools:ignore=&quot;MissingConstraints&quot; /&gt;

    &lt;com.mypackage.name.CustomFlow
        android:id=&quot;@+id/flow&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:padding=&quot;10dp&quot;
        app:constraint_referenced_ids=&quot;textView1,delimiterView,textView2&quot;
        app:flow_horizontalBias=&quot;0&quot;
        app:flow_horizontalGap=&quot;10dp&quot;
        app:flow_horizontalStyle=&quot;packed&quot;
        app:flow_verticalBias=&quot;0&quot;
        app:flow_wrapMode=&quot;chain&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Result when TextView2 is short (View-delimiter is visible):

在Android中如何获取流式布局的行数

Result when TextView2 is long and goes to the next line (View-delimiter is gone):

在Android中如何获取流式布局的行数

答案2

得分: 0

没有在约束布局内部流中获取行数的方法。
您可以使用这个流布局库。

implementation 'com.nex3z:flow-layout:1.3.3'

GitHub

与您的问题相同

英文:

There's no method in the constraint layout internal flow to get the number of row.
Instead you can use this flow library.

implementation &#39;com.nex3z:flow-layout:1.3.3&#39;

GitHub

Same issue as yours

huangapple
  • 本文由 发表于 2023年7月20日 12:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定