在我的recyclerview
项目中,我需要检查 textview 是否为椭圆形。基于这个条件,我需要做一些逻辑。这是我的项目布局 xml 代码:
<androidx.constraintlayout.widget.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:id="@+id/activity_list_item_root_view"
style="@style/ClickableRectanglePrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
android:paddingTop="13.5dp"
android:paddingBottom="14.5dp">
<ImageView
android:id="@+id/activity_list_item_contact_image"
android:layout_width="@dimen/ui_size_xl_2"
android:layout_height="@dimen/ui_size_xl_2"
tools:ignore="ContentDescription"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/activity_list_item_name"
style="@style/activity_list_item_name_v2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text="ABC India Pvt. Ltd."
tools:ignore="HardcodedText"
app:layout_constraintStart_toEndOf="@+id/activity_list_item_contact_image"
app:layout_constraintEnd_toStartOf="@+id/activity_list_item_amount"
app:layout_constraintTop_toTopOf="@+id/activity_list_item_contact_image"
android:layout_marginStart="12dp"
android:layout_marginEnd="4dp"/>
<TextView
android:id="@+id/activity_list_item_date"
style="@style/activity_list_item_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text="Yesterday"
tools:ignore="HardcodedText"
app:layout_constraintTop_toBottomOf="@id/activity_list_item_name"
app:layout_constraintStart_toEndOf="@id/activity_list_item_contact_image"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"/>
<TextView
android:id="@+id/activity_list_item_dot_text"
style="@style/activity_list_item_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text=" \u00b7 "
tools:ignore="HardcodedText"
app:layout_constraintStart_toEndOf="@id/activity_list_item_date"
app:layout_constraintTop_toBottomOf="@id/activity_list_item_name"
android:layout_marginTop="4dp"/>
<ImageView
android:id="@+id/activity_list_item_status_icon"
android:layout_width="@dimen/ui_size_xs"
android:layout_height="@dimen/ui_size_xs"
android:layout_marginEnd="6dp"
android:scaleType="fitXY"
tools:ignore="ContentDescription"
app:srcCompat="@drawable/ui_purchase_protection_alt"
app:layout_constraintStart_toEndOf="@id/activity_list_item_dot_text"
app:layout_constraintTop_toBottomOf="@id/activity_list_item_name"
android:layout_marginTop="4dp"/>
<TextView
android:id="@+id/activity_list_item_status_text"
style="@style/activity_list_item_details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text="Request Received"
tools:ignore="HardcodedText"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintStart_toEndOf="@id/activity_list_item_status_icon"
app:layout_constraintTop_toBottomOf="@id/activity_list_item_name"
app:layout_constraintEnd_toStartOf="@id/activity_list_item_amount"
android:layout_marginEnd="4dp"
android:layout_marginTop="4dp"/>
<TextView
android:id="@+id/activity_list_item_status_spillover_text"
style="@style/activity_list_item_details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text="Request Received"
tools:ignore="HardcodedText"
android:visibility="gone"
tools:visibility="visible"
app:layout_constraintTop_toBottomOf="@id/activity_list_item_date"
app:layout_constraintStart_toEndOf="@id/activity_list_item_contact_image"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"/>
<TextView
android:id="@+id/activity_list_item_amount"
style="@style/UiTextView.Xl.Regular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|end"
android:text="$149.99"
android:textColor="?attr/ui_v2_color_grey_600"
android:textSize="18sp"
tools:ignore="HardcodedText"
app:layout_constraintTop_toTopOf="@id/activity_list_item_contact_image"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
现在,我想检查这个 textview 是否activity_list_item_status_text
是椭圆的,如果它是椭圆的,则在第三行显示 textview activity_list_item_status_spillover_text
。如果没有,请隐藏它。我曾经TreeObserver
检查过这个。这是里面的代码ViewHolder
:
TextView statusTextView = binding.activityListItemStatusText;
statusTextView.getViewTreeObserver().addOnDrawListener(() -> {
Layout layout = statusTextView.getLayout();
if (layout != null) {
int lines = layout.getLineCount();
if (layout.getEllipsisCount(lines - 1) > 0) {
binding.activityListItemStatusSpilloverText.setVisibility(View.VISIBLE);
}else {
binding.activityListItemStatusSpilloverText.setVisibility(View.GONE);
}
}
});
当我打开 recyclerview 时,此代码工作正常。但是,当我滚动 recyclerview 时,对于某些项目,layout.getEllipsisCount(lines - 1) > 0
即使文本不是椭圆形并且activity_list_item_status_spillover_text
变得可见,这种情况也会变为真。它的类似文本在滚动过程中变得椭圆形。我无法弄清楚为什么会这样。代码有问题吗?
编辑: 添加问题的屏幕录像: https ://drive.google.com/file/d/1l-xUyBJzHy-7BScsbcJPpznhwVSN7Ah4/view?usp=sharing