约束布局流助手子项的分隔线装饰
如您所知,您可以为RecyClerview的项目设置recyclerview.itemdemdecoration
。但是,我想实现与此类似的东西,但是对于constraintlayout
's flow
。
目前,我可以创建多个分隔视图,并在每个正常视图之后将它们放入我的流程中重复自己的例子。同样,值得注意的是,您不能在Flow
中不多次引用相同的视图,因此您不能在不同位置中多次创建分隔视图并多次引用它。务实地,我可以为textView
在其下面创建自定义背景/样式以某种方式复制分隔线,但是如果flow
s子女不是textView ?
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:checkable="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/product_item_detail_flow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:padding="8dp"
app:constraint_referenced_ids="product_item_category,materialDivider,product_item_name,materialDivider2,product_item_price"
app:flow_verticalGap="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/product_item_image"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.divider.MaterialDivider
android:id="@+id/materialDivider"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<com.google.android.material.divider.MaterialDivider
android:id="@+id/materialDivider2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/product_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
tools:text="@tools:sample/lorem" />
<TextView
android:id="@+id/product_item_category"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="14sp"
tools:text="@tools:sample/lorem" />
<TextView
android:id="@+id/product_item_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="@tools:sample/us_zipcodes" />
<ImageView
android:id="@+id/product_item_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/product_image"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/product_item_detail_flow"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".33"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
考虑另一个类似的流程,我认为在每个项目不明智的情况下将分隔线放置在多个视图中:
因此,我如何创建一个分隔物的装饰,以使约束布局的流子孩子?
编辑:这两个流都实际上都是回收瓶的项目
As you may know, you can set a RecyclerView.ItemDecoration
for RecyclerView's Items. However, I want to achieve something similar to that but for ConstraintLayout
's Flow
.
Currently, I can create multiple divider views and put them inside my flow after each normal view but that can be cumbersome when my Flow
has multiple views and that's not DRY
and is an example of repeating yourself. Also, it's worth noting that you cannot reference the same view more than once in Flow
so you cannot create a divider view and reference it multiple times in different positions. Pragmatically I can create a custom background/style for TextView
with a line under them to somehow replicate a divider but what if Flow
s children are not TextView
?
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:checkable="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/product_item_detail_flow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
android:padding="8dp"
app:constraint_referenced_ids="product_item_category,materialDivider,product_item_name,materialDivider2,product_item_price"
app:flow_verticalGap="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/product_item_image"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.divider.MaterialDivider
android:id="@+id/materialDivider"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<com.google.android.material.divider.MaterialDivider
android:id="@+id/materialDivider2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/product_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
tools:text="@tools:sample/lorem" />
<TextView
android:id="@+id/product_item_category"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="14sp"
tools:text="@tools:sample/lorem" />
<TextView
android:id="@+id/product_item_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="@tools:sample/us_zipcodes" />
<ImageView
android:id="@+id/product_item_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/product_image"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/product_item_detail_flow"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".33"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Consider another flow like this with multiple views that putting a divider after each item is not wise in my opinion:
So in short how can I create a divider item decoration for constraint layout's flow children?
Edit: both of these flows are actually item for a recyclerview
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有找到一种使用
bentaintlayout
和flow
来实现此目的的方法,但是,可以使用flexboxlayout
。因为flexboxlayout
具有dividerDrawable
属性,该属性会自动将分隔线放在子视图之间。将
flexboxlayout
库添加到您的应用/模块后,您可以这样使用它:for
@drawable/flex_divider
创建以下内容:这最终导致:
I didn't find a way to achieve this using
ConstraintLayout
andFlow
, however, this can be achieved usingFlexboxLayout
. BecauseFlexboxLayout
has adividerDrawable
attribute that automatically puts a divider between child views.After adding the
FlexboxLayout
library to your app/module you can use it like this:and for
@drawable/flex_divider
create this:This ultimately results in: