Android禁用剪辑recyclerview的项目

时间:2019-07-02 11:46:50

标签: android android-layout android-recyclerview

To-be and As-is

第一张图片描述了我想要的东西,第二张图片描述了我想要的东西。

黑色矩形表示RecyclerView,深红色表示该项目。海军是ImageView

我在recyclerview所属的父视图组上尝试了clipChildren。

有没有实现这个目标的愿望? (我也尝试过动画)

val animation = TranslateAnimation(
    Animation.ABSOLUTE, 0f,
    Animation.ABSOLUTE, 0f,
    Animation.ABSOLUTE, 0f,
    Animation.ABSOLUTE, -dpToPixel(21f, itemView.context)
)
animation.duration = 0

imageView.startAnimation(animation)

---编辑---


活动布局

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView"
                                               android:layout_width="match_parent"
                                               android:layout_height="60dp"
                                               android:background="@android:color/black"
                                               app:layout_constraintLeft_toLeftOf="parent"
                                               app:layout_constraintRight_toRightOf="parent"
                                               app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>


ViewHolder布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                   xmlns:app="http://schemas.android.com/apk/res-auto"
                                                   android:orientation="vertical"
                                                   android:layout_width="50dp"
                                                   android:layout_height="50dp">


    <!-- image -->
    <View android:id="@+id/dummy"
          android:layout_width="0dp"
          android:layout_height="0dp"
          android:background="@android:color/holo_green_dark"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintBottom_toBottomOf="parent" />

    <!-- marker here -->
    <View android:layout_width="20dp"
          android:layout_height="20dp"
          android:translationX="-5dp"
          android:translationY="-5dp"
          android:background="@android:color/holo_red_dark"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

请注意,我不能提供超过60dp的高度,并且内部物品的高度固定为(50dp)。

enter image description here

2 个答案:

答案 0 :(得分:2)

在您的示例中,视图持有者布局包含在 RecyclerView 中,而 RecyclerView 包含在a_ConstraintLayout_中。

ConstraintLayout
----> RecyclerView
--------> ConstraintLayout(视图持有者)
------------> ImageView

根据documentation for clipChildren

  

android:clipChildren

     

定义是否限制孩子在其边界内绘画。

您希望视图持有人能够在视图持有人 RecyclerView 的边界之外绘制 ImageView RecyclerView 范围之外的所有者。为此,您需要在 RecyclerView 顶级 ConstraintLayout 上设置android:clipChildren="false"

答案 1 :(得分:0)

借助ConstraintLayout,您可以在几秒钟内实现“视图之上”:

您所需要做的就是进行适当的约束,上方视图将被限制为另一个视图,并且他将位于他之上。

类似的事情:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
  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="match_parent">

<ImageView
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:src="@drawable/ic_launcher_background"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Lower view" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintEnd_toStartOf="@+id/button2"
    app:layout_constraintStart_toStartOf="@+id/button2"
    app:layout_constraintTop_toTopOf="@+id/button2"
    tools:text="top" />
</android.support.constraint.ConstraintLayout>

它看起来像这样:

enter image description here

这是将1个视图放在另一个视图之上的方式,现在剩下要做的就是为您的案例实现它。