Android 如何在ConstraintLayout中刷新受约束视图的高度
我有一个ConstraintLayout,其中我有一个视图(称为Bar)和一个约束在Bar底部的RecyclerView,RecyclerView的高度设置为匹配约束(0dp) 因此,如果在Android布局编辑器中,我向上移动条,例如,RecyclerView高度增加,并且始终“锚定”到条,这就是工作 但这与运行时的行为不同,当我移动条(使用onTouchListener)时,RecyclerView高度根本不会改变,就像条在同一位置一样 为了实现此行为(移动条增加/减少RecyclerView高度),我考虑并尝试了ConstraintLayout解决方案 因此,我在条的底部设置了一个约束(顶部),并设置了与约束匹配的高度 我还尝试使用LayoutParam实现这种行为,根据移动的像素更改高度,但公式不是100%好,使用这种方式可以从视图的底部和顶部更改高度(显然不是使用约束解决方案)Android 如何在ConstraintLayout中刷新受约束视图的高度,android,kotlin,android-constraintlayout,ontouchlistener,android-motionlayout,Android,Kotlin,Android Constraintlayout,Ontouchlistener,Android Motionlayout,我有一个ConstraintLayout,其中我有一个视图(称为Bar)和一个约束在Bar底部的RecyclerView,RecyclerView的高度设置为匹配约束(0dp) 因此,如果在Android布局编辑器中,我向上移动条,例如,RecyclerView高度增加,并且始终“锚定”到条,这就是工作 但这与运行时的行为不同,当我移动条(使用onTouchListener)时,RecyclerView高度根本不会改变,就像条在同一位置一样 为了实现此行为(移动条增加/减少RecyclerVie
...
...
barWhoMove.setOnTouchListener{v,事件->
何时(event.actionMasked){
MotionEvent.ACTION\u向下->{
barWhoMoveDY=v.y-event.rawY
return@setOnTouchListener真的
}
MotionEvent.ACTION\u MOVE->{
////我们移动酒吧
v、 动画().translationY(barWhoMoveDY+
setDuration(0.start())
/*尝试基于匹配约束重新显示和调整大小,同时考虑新的barWhoMove位置*/
RecycleServiceConstrainedTotheBar.invalidate()
RecycleServiceConstrainedTotheBar.requestLayout()
RecycleServiceConstrainedTotheBar.forceLayout()
////也试过
constraintSet.ConstraintHeight(RecycleServiceWConstrainedTotheBar.id,
ConstraintSet.MATCH_约束)
constraintSet.applyTo(rootConstraintLayout)
////也试过
rootConstraintLayout.invalidate()
rootConstraintLayout.requestLayout()
rootConstraintLayout.forceLayout()
if(v.y{
}
}
return@setOnTouchListener假的
}
编辑:
这是布局1中
标记的内容:
<?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
属性设置动画。更改此值会移动“查看立柱”布局。换言之,布局完成后移动。因此,RecyclerView
在转换之前被约束到后期布局位置。见:
设置此视图相对于其顶部位置的垂直位置。这将有效地定位对象的后期布局,以及对象的布局所放置的位置
谢谢你的解释,如果我理解的很好,我需要一种方法来移动视图,然后完成布局,而不是相反?我也想到过类似的事情,所以我尝试设置view.y=position以查看这是否会改变一些东西,我还尝试了viewwhomove.invalidate/requestLayout等…,我还尝试了onAnimationEnd中的动画更新侦听器…@UserOne如何在ConstraintLayout中定位该条?发布一些XML以供查看。当然,我已经编辑了我的帖子,添加了标记的内容,要移动此版面,我从版面1中获得了对包含的引用。注意:在包含标记的版面内容中,应用程序:layout_constraintTop_toBottomOf=“@id/textViewTitle”,如果我删除if,则视图将被取消(预期行为)@UserOne我对你的整个布局没有很好的了解。我可以建议,如果你想移动的酒吧,然后改变它的底部边缘。它应该是移动的,
RecyclerView
应该跟随并展开。(回收视图的RecyclerView
将不考虑条形图的边距。)
<?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>