【问题标题】:Add marquee to TextView inside RecyclerView在 RecyclerView 内的 TextView 中添加选取框
【发布时间】:2021-01-06 10:16:37
【问题描述】:

我有一个RecyclerView,其中有一个ImageView 和一个TextView

由于TextView 中的文字可能很长,我希望它可以连续滚动。

RecyclerView里面的项目如下:(我在里面都写了一样长的文字只是为了举例)

TextView 的 xml 是:

<TextView
    android:id="@+id/tv_Link"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_alignStart="@+id/imagecard"
    android:layout_alignEnd="@+id/imagecard"
    android:layout_alignBottom="@+id/imagecard"
    android:elevation="50dp"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:text="1111111222222223333333" />

在我的适配器中为RecyclerView

tv_Link.setSingleLine();
tv_Link.setSelected(true);

问题是当 RecyclerView 被加载时,文本并没有移动。只有当我移动到另一个活动然后返回到该布局时,它才会显示文本正在移动。

如何确保它在 RecyclerView 加载时正确移动,而不仅仅是在活动之间移动时?

谢谢

此布局的完整 XML 为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:id="@+id/imagecard"
        android:layout_width="110dp"
        android:layout_height="160dp"
        android:layout_marginEnd="24dp"
        app:cardCornerRadius="10dp">

        <ImageView
            android:id="@+id/iv_Image"
            android:layout_width="110dp"
            android:layout_height="140dp"
            android:scaleType="fitXY" />

    </androidx.cardview.widget.CardView>

    <TextView
        android:id="@+id/tv_Link"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignStart="@+id/imagecard"
        android:layout_alignEnd="@+id/imagecard"
        android:layout_alignBottom="@+id/imagecard"
        android:elevation="50dp"
        android:fontFamily="@font/assistant_semibold"
        android:gravity="center"
        android:textColor="@color/colorLightPurple"
        android:textSize="8sp" />

    <TextView
        android:id="@+id/tv_Name"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imagecard"
        android:layout_marginTop="8dp"
        android:drawablePadding="8dp"
        android:ellipsize="end"
        android:fontFamily="@font/assistant_semibold"
        android:maxLines="1"
        android:textColor="@color/colorGrayText"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/tv_A"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_Name"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="12dp"
        android:drawablePadding="8dp"
        android:ellipsize="end"
        android:fontFamily="@font/assistant"
        android:maxLines="1"
        android:textColor="@color/colorGrayText"
        android:textSize="10sp" />

</RelativeLayout>

【问题讨论】:

    标签: android android-recyclerview textview marquee


    【解决方案1】:

    要让它移动,试试这个:

     tv_Link.setMovementMethod(LinkMovementMethod.getInstance());
     tv_Link.setEllipsize(TextUtils.TruncateAt.MARQUEE);
     tv_Link.setSelected(true);
     tv_Link.setSingleLine(true);
    

    在您的 XML 中:

        <TextView 
        android:id="@+id/tv_Link"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignStart="@+id/imagecard"
        android:layout_alignEnd="@+id/imagecard"
        android:layout_alignBottom="@+id/imagecard"
        android:elevation="50dp"
        android:text="1111111222222223333333"
        android:marqueeRepeatLimit="marquee_forever" 
        android:singleLine="true" />
    

    【讨论】:

    • 对不起队友没有工作。它只是让文字消失了
    • @Ben 我已经测试过了,效果很好,能否请您添加项目的整个 XML 代码
    • 添加了完整的 xml 伴侣。据我了解,它不起作用,因为应该在同一个回收站视图中移动多个文本视图,而不是显示它只是使文本消失。当我在单个 textview 上使用但不在 recyclerview 内使用时,它确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多