簡體   English   中英

順利從芯片組中取出一塊android芯片

[英]Remove an android Chip from a Chip Group smoothly

在我正在開發的應用程序的一個片段中,我讓用戶創建各種芯片,每個芯片都代表一個選項。 我能夠為芯片創建動畫

現在,當用戶點擊芯片時,我將其從組中刪除。 我能夠將自定義 animation 與刪除相關聯(參見 gif),但是當“中間芯片”被刪除時,當 animation 結束時,右側的芯片突然向左移動

在此處輸入圖像描述

布局:

       <HorizontalScrollView
           android:layout_width="match_parent"
           android:layout_height="@dimen/horizontal_scroll_height"
           android:scrollbars="none">

           <com.google.android.material.chip.ChipGroup
               android:id="@+id/rouletteChipList"
               style="@style/Widget.MaterialComponents.ChipGroup"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:paddingStart="@dimen/chip_horizontal_margin"
               android:paddingEnd="@dimen/chip_horizontal_margin"
               app:chipSpacing="@dimen/chip_spacing"
               app:singleLine="true">

           </com.google.android.material.chip.ChipGroup>
       </HorizontalScrollView> 

芯片刪除:

private void removeChip(final Chip chip) {
        @SuppressWarnings("ConstantConditions") final ChipGroup optionsList = getView().findViewById(R.id.ChipList);
        // Remove the chip with an animation
        if (chip == null) return;
        final Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.chip_exit_anim);
        chip.startAnimation(animation);
        chip.postDelayed(new Runnable() {
            @Override
            public void run() {
                optionsList.removeView(chip);
            }
        }, 400);
}

芯片布局:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip 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:text="@string/placeholder"
    android:textAlignment="center"
    app:chipBackgroundColor="@color/grayTranslucent"
    app:rippleColor="?colorAccent" />

我想要一個平滑的 animation ,當刪除“中間芯片”時,芯片會平滑地向左移動。 我嘗試了幾件事,但沒有運氣。

如果你的意思是這樣的

動畫去除芯片

我已將其添加到 HSV 中,並在 chip_group 上添加了 android: chip_group android:animateLayoutChanges="true" ,請參見下面的代碼

<HorizontalScrollView
        android:scrollbars="none"
        .
        >
        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chip_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true">

        </com.google.android.material.chip.ChipGroup>

    </HorizontalScrollView>

在我的代碼中,我已將芯片動態添加到該chip_group中。

val newChip = Chip(this, null, R.attr.chipStyle)
newChip.text = "text"
newChip.isClickable = true
newChip.isFocusable = true
newChip.setOnClickListener(chipClickListener)
chip_group.addView(newChip)

並且每個芯片上都有一個onClickListener 。在它里面我開始淡入淡出 animation 並在onAnimationEndchip_group組執行removeView

private val chipClickListener = View.OnClickListener {

        val anim = AlphaAnimation(1f,0f)
        anim.duration = 250
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {}

            override fun onAnimationEnd(animation: Animation?) {
                chip_group.removeView(it)
            }

            override fun onAnimationStart(animation: Animation?) {}
        })

        it.startAnimation(anim)
    }

暫無
暫無

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

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