3

我有一个 ConstraintLayout,其中我有一个 View(称为 Bar)和一个 RecyclerView 限制在 Bar 的底部,RecyclerView 的高度设置为匹配 Constraint(0dp),

因此,如果在 Android 布局编辑器中我向上移动 Bar 例如,RecyclerView 高度增加并且总是“锚定”到 Bar,它的工作,

但这在运行时的行为不一样,当我移动 Bar(使用 onTouchListener)时,RecyclerView 的高度根本不会改变,并且如果 Bar 位于同一位置,则保持不变。

为了实现这种行为(移动 Bar 增加/减少 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
    }

编辑 :

这是<include>布局 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>

4

1 回答 1

0

您正在translationY为视图的属性设置动画。更改此值会移动视图帖子布局。换句话说,布局完成,然后进行移动。结果,RecyclerView在翻译之前被限制在布局后的位置。见setTranslationY(float)

设置此视图相对于其顶部位置的垂直位置。除了对象的布局放置它的任何位置之外,这有效地定位了布局后的对象。

于 2019-03-28T19:42:49.613 回答