0

我正在制作一个聊天应用程序,我想添加一个像电报这样的功能来复制链接、电话号码等。我使用这个库在自动链接上添加长点击监听器。我实现了它成功。但是当我这样做时,我想在开始时显示一个 Lottie 动画,但要长按链接。我尝试了很多答案,但我得到了一个例外。我已经为自定义 snakbar 制作了布局。它在下面给出

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="56dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="@dimen/_3sdp"
android:backgroundTint="@color/snakbar_background"
app:cardCornerRadius="@dimen/_4sdp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottieSnakbar"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginStart="@dimen/_5sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        app:lottie_rawRes="@drawable/copy"
        app:lottie_autoPlay="true"
        app:lottie_loop="false"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Text copied to clipboard"
        android:gravity="center_vertical"
        android:theme="?snackbarTextViewStyle"
        tools:ignore="HardcodedText" />

</LinearLayout>

</androidx.cardview.widget.CardView>

现在我怎样才能做到这一点?

4

2 回答 2

1

您可以使用适配器中的接口。并从活动中接收并显示 snakbar

于 2021-11-24T10:57:50.810 回答
0

您可以使用localBroadcast和广播来自适配器的本地信号,并让另一个Activity(可能是接收器)处理 snakbar。

AdapterClass{
   LocalBroadcastManager localBroadcastManager = null;
   
   void sendB(parms){
      if(localBroadcastManager != null){
            localBroadcastManager.sendBroadcast(Intent("NOTIFICATION_RECEIVER"))
      }

   onCreateViewHolder(){
       localBroadcastManager = LocalBroadcastManager.getInstance(applicationContext)
      }

  onBindViewHolder(){
      if(someCondition){
           sendB(parms);
    }
}

然后从活动中接收它然后调用 snakBar

于 2021-11-23T13:51:01.613 回答