英文:
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-分隔符可见):
当TextView2很长并换行时的结果(View-分隔符消失):
英文:
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 > 1) {
delView.visibility = GONE
} else {
delView.visibility = VISIBLE
}
}
}
And can be used in your xml like below:
<?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>
Result when TextView2 is short (View-delimiter is visible):
Result when TextView2 is long and goes to the next line (View-delimiter is gone):
答案2
得分: 0
没有在约束布局内部流中获取行数的方法。
您可以使用这个流布局库。
implementation 'com.nex3z:flow-layout:1.3.3'
英文:
There's no method in the constraint layout internal flow to get the number of row.
Instead you can use this flow library.
implementation 'com.nex3z:flow-layout:1.3.3'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论