我有一个ConstraintLayout,其中有一个View(称为Bar)和一个RecyclerView,约束到Bar的底部,RecyclerView的高度设置为与Constraint(0dp)匹配,
例如,如果我在Android布局编辑器中将Bar向上移动,则RecyclerView高度增加并且始终固定在Bar上,这是可行的,
但是在运行时,这是不一样的行为,当我移动Bar(使用onTouchListener)时,RecyclerView的高度根本不会改变,就像Bar在同一位置一样。.
要实现此行为(将Bar增大/减小RecyclerView高度),我已经考虑并尝试了ConstraintLayout解决方案,
所以我在Bar的底部设置了一个Constraint(Top),并且我将高度设置为与Constraint相匹配,
我也尝试使用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>
答案 0 :(得分:0)
您正在为视图的translationY
属性设置动画。更改此值将移动视图帖子布局。换句话说,先完成布局,然后进行移动。结果,RecyclerView
被限制在平移之前的布局后位置。参见setTranslationY(float):
设置此视图相对于其顶部位置的垂直位置。除了将对象的布局放置在任何位置之外,这还可以有效地放置对象的后布局。