background

When the data is displayed in the list, some data needs to be displayed in gray because it is invalid. However, because each row of data contains multiple views (such as user name, gender and other information), it is not convenient to set the gray of each view, so a mask layer is needed to cover the whole row to achieve the gray effect. The general effect is as follows:

The solution

The layout of an item in a list uses a RelativeLayout or ConstraintLayout. Add an empty view (mask layer) to the layout, just enough to cover all other views. The default setting is invisible. Set whether to open this mask layer in adapter according to the validity of the data.

The following is an example of the masked Item layout file: maskLayer is the masked view. The following points need to be noted:

  1. Android :layout_height=”30dp” To set this height to a fixed value, it must be exactly the same as the original view height, cannot be set to wrap_content or match_parent. Therefore, this scheme is not applicable to the case where the height of item is not fixed.
  2. Android :background=”#80F9F9F9″ Set the mask layer color, the first two are transparency (00 to FF, the smaller the more transparent.)
  3. Android: Clickable =”true” and Android :focusable=”true” are used to block click events and render them invalid.
<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="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:textColor="@android:color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />

    <View
        android:id="@+id/maskLayer"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#80F9F9F9"
        android:clickable="true"
        android:focusable="true"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Copy the code

The demo:

Gitee.com/cxyzy1/mask…

Android development technology sharing: blog.csdn.net/yinxing2008…