簡體   English   中英

如何從回收器視圖適配器開始顯示帶有 Lottie 動畫的 snakbar

[英]How to show a snakbar with Lottie animation in start from a recycler view adapter

我正在制作一個聊天應用程序,我想添加一個像電報這樣的功能來復制鏈接、電話號碼等。通過長時間點擊自動鏈接。我使用這個庫在自動鏈接上添加長點擊偵聽器。我實現了它successfully.But當我這樣做,我想說明一個洛蒂動畫起步一樣可是一個鏈接的長按。 我嘗試了很多答案,但我得到了一個例外。我已經為自定義 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>

現在我怎樣才能做到這一點?

您可以使用適配器中的接口。 並從活動中接收並顯示 snakbar

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM