如何在 ConstraintLayout 中刷新 Constrained View 的高度
How to refresh height of a Constrained View in ConstraintLayout
我有一个 ConstraintLayout,其中有一个 View(称为 Bar)和一个限制在 Bar 底部的 RecyclerView,RecyclerView 的高度设置为匹配 Constraint(0dp),
因此,如果在 Android 布局编辑器中我将 The Bar 向上移动,例如 RecyclerView 高度增加并且始终 "anchored" 到 The Bar,它的工作,
但这在运行时是不一样的行为,当我移动 Bar(使用 onTouchListener)时,RecyclerView 高度根本没有改变并且保持就像 Bar 处于相同位置一样..
为了实现这个行为(将 Bar increase/decrease 移动到 RecyclerView 高度),我考虑并尝试了 ConstraintLayout 解决方案,
所以我已经为条形图的底部设置了一个约束(顶部),并且我已经设置了与约束相匹配的高度,
我也尝试通过 LayoutParam 实现这种行为,根据移动的像素改变高度,但公式不是 100% 好,使用这种方式改变视图底部和顶部的高度(显然不是使用约束解决方案)
<ConstraintLayout>
...
<!-- Removed all Constraint to the Bar to let it freely move on Touch on it's Y Axis, also tried to constraint start, end, top to the parent, but same behavior -->
<include layout="@layout/theBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/theBarWhoMove"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewConstrainedToTheBar"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="2dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="2dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/theBarWhoMove"/>
...
</ConstraintLayout>
barWhoMove.setOnTouchListener { v, event ->
when(event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
barWhoMoveDY = v.y - event.rawY
return@setOnTouchListener true
}
MotionEvent.ACTION_MOVE -> {
////We move the Bar
v.animate().translationY(barWhoMoveDY +
event.rawY).setDuration(0).start()
/* To try to relayout and resize based on match Constrain taking in consideration the new barWhoMove position*/
recyclerViewConstrainedToTheBar.invalidate()
recyclerViewConstrainedToTheBar.requestLayout()
recyclerViewConstrainedToTheBar.forceLayout()
////Tried Also
constraintSet.constrainHeight(recyclerViewConstrainedToTheBar.id,
ConstraintSet.MATCH_CONSTRAINT)
constraintSet.applyTo(rootConstraintLayout)
////Tried Also
rootConstraintLayout.invalidate()
rootConstraintLayout.requestLayout()
rootConstraintLayout.forceLayout()
if(v.y < oldDY) {
/*#Solution 2 increase with LayoutParam but The formula is not 100% correct and it change height from both bottom and top, When View is moving Up or Down(it's work for detecting this)*/
....
oldDy = v.y
}
else {
....
oldDy = v.y
}
return@setOnTouchListener true
}
MotionEvent.ACTION_UP -> {
}
}
return@setOnTouchListener false
}
编辑:
这是布局 1 中 <include>
标签的内容:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:showIn="@layout/fragment_layout_main">
<data>
<variable name="variable" type="com.demo.ourClass" />
</data>
<android.support.v7.widget.CardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textViewTitle"
android:layout_marginTop="8dp"
app:cardElevation="6dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_round"
android:id="@+id/imageViewA"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<TextView
android:text="@{variable.name}"
tools:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewA"
app:layout_constraintStart_toEndOf="@+id/imageViewA"
app:layout_constraintBottom_toBottomOf="@id/imageViewA"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
您正在为视图的 translationY
属性 设置动画。更改此值会移动视图 post 布局。换句话说,布局完成,然后移动。结果,RecyclerView
被限制在翻译前的 post-layout 位置。见 setTranslationY(float):
Sets the vertical location of this view relative to its top position. This effectively positions the object post-layout, in addition to wherever the object's layout placed it.
我有一个 ConstraintLayout,其中有一个 View(称为 Bar)和一个限制在 Bar 底部的 RecyclerView,RecyclerView 的高度设置为匹配 Constraint(0dp),
因此,如果在 Android 布局编辑器中我将 The Bar 向上移动,例如 RecyclerView 高度增加并且始终 "anchored" 到 The Bar,它的工作,
但这在运行时是不一样的行为,当我移动 Bar(使用 onTouchListener)时,RecyclerView 高度根本没有改变并且保持就像 Bar 处于相同位置一样..
为了实现这个行为(将 Bar increase/decrease 移动到 RecyclerView 高度),我考虑并尝试了 ConstraintLayout 解决方案,
所以我已经为条形图的底部设置了一个约束(顶部),并且我已经设置了与约束相匹配的高度,
我也尝试通过 LayoutParam 实现这种行为,根据移动的像素改变高度,但公式不是 100% 好,使用这种方式改变视图底部和顶部的高度(显然不是使用约束解决方案)
<ConstraintLayout>
...
<!-- Removed all Constraint to the Bar to let it freely move on Touch on it's Y Axis, also tried to constraint start, end, top to the parent, but same behavior -->
<include layout="@layout/theBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/theBarWhoMove"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewConstrainedToTheBar"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="2dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="2dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/theBarWhoMove"/>
...
</ConstraintLayout>
barWhoMove.setOnTouchListener { v, event ->
when(event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
barWhoMoveDY = v.y - event.rawY
return@setOnTouchListener true
}
MotionEvent.ACTION_MOVE -> {
////We move the Bar
v.animate().translationY(barWhoMoveDY +
event.rawY).setDuration(0).start()
/* To try to relayout and resize based on match Constrain taking in consideration the new barWhoMove position*/
recyclerViewConstrainedToTheBar.invalidate()
recyclerViewConstrainedToTheBar.requestLayout()
recyclerViewConstrainedToTheBar.forceLayout()
////Tried Also
constraintSet.constrainHeight(recyclerViewConstrainedToTheBar.id,
ConstraintSet.MATCH_CONSTRAINT)
constraintSet.applyTo(rootConstraintLayout)
////Tried Also
rootConstraintLayout.invalidate()
rootConstraintLayout.requestLayout()
rootConstraintLayout.forceLayout()
if(v.y < oldDY) {
/*#Solution 2 increase with LayoutParam but The formula is not 100% correct and it change height from both bottom and top, When View is moving Up or Down(it's work for detecting this)*/
....
oldDy = v.y
}
else {
....
oldDy = v.y
}
return@setOnTouchListener true
}
MotionEvent.ACTION_UP -> {
}
}
return@setOnTouchListener false
}
编辑:
这是布局 1 中 <include>
标签的内容:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:showIn="@layout/fragment_layout_main">
<data>
<variable name="variable" type="com.demo.ourClass" />
</data>
<android.support.v7.widget.CardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textViewTitle"
android:layout_marginTop="8dp"
app:cardElevation="6dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_round"
android:id="@+id/imageViewA"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<TextView
android:text="@{variable.name}"
tools:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewA"
app:layout_constraintStart_toEndOf="@+id/imageViewA"
app:layout_constraintBottom_toBottomOf="@id/imageViewA"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
您正在为视图的 translationY
属性 设置动画。更改此值会移动视图 post 布局。换句话说,布局完成,然后移动。结果,RecyclerView
被限制在翻译前的 post-layout 位置。见 setTranslationY(float):
Sets the vertical location of this view relative to its top position. This effectively positions the object post-layout, in addition to wherever the object's layout placed it.