如何使用 recyclerview 设置动画可绘制对象?

How to set up animated drawable with recyclerview?

大家好,我成功地设置了回收站视图并实现了点赞功能。我还为可绘制对象设置了动画。当用户竖起大拇指时,会显示动画并填充可绘制对象。我还设置了一个反向动画来取消填充可绘制对象。当我为 recyclerview 获取数据时,我正在处理状态。如果状态为 0,则可绘制对象未填充,如果其状态为 1,则已填充。 我的 drawable 是一个 drawable 状态列表。我的问题是加载数据时它会显示动画,所以我尝试在开始时将可绘制状态列表替换为仅填充或未填充的可绘制对象,当用户得到一个项目竖起大拇指时,我将其替换为可绘制状态列表和为此设置一个状态。问题是有时它没有为可绘制对象设置动画。 我在回收器项目的细节片段中遇到了同样的问题,有时它会自发地改变可绘制对象的状态。在回收者视图中具有不同状态的可绘制对象的最佳解决方案是什么。 如有任何答复,将不胜感激。

可绘制状态列表:

<!-- provide a different drawable for each state-->
<item
    android:id="@+id/state_off"
    android:drawable="@drawable/ic_herzspaceliked"
    android:state_activated="false"
    />
<item
    android:id="@+id/state_on"
    android:drawable="@drawable/heartfill"
    android:state_activated="true"
    />

<transition
    android:drawable="@drawable/heartfill"
    android:fromId="@id/state_off"
    android:toId="@id/state_on"
    android:reversible="true"
    />


<!-- specify transitions -->

我如何在 recylcerview 中设置可绘制状态:

              ((Eventholder)holder).likestate = Objects.requireNonNull(getItem(position)).getUserLikedState();
                if (((Eventholder)holder).likestate== 0) {
                    //((Eventholder)holder).eventCardviewBinding.likebutton.getBackground().setState(new int[]{-android.R.attr.state_activated});
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setBackgroundResource(R.drawable.ic_herzspaceliked);
                } else if (((Eventholder)holder).likestate==1) {
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setBackgroundResource(R.drawable.ic_herzliked);
                    //((Eventholder) holder).eventCardviewBinding.likebutton.getBackground().setState(new int[]{android.R.attr.state_activated});
                }
              
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setOnClickListener(v -> {
                if (((Eventholder)holder).likestate==0) {
                    ((Eventholder)holder).likestate=1;
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setBackgroundResource(R.drawable.heartanimation);
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setActivated(true);
                    ((Eventholder) holder).eventCardviewBinding.LikeCount.setText(String.valueOf(Integer.parseInt(((Eventholder) holder).eventCardviewBinding.LikeCount.getText().toString()) + 1));
                }
                else if (((Eventholder)holder).likestate==1) {
                    ((Eventholder)holder).likestate=0;
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setBackgroundResource(R.drawable.heartanimation);
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setActivated(false);
                    ((Eventholder) holder).eventCardviewBinding.LikeCount.setText(String.valueOf(Integer.parseInt(((Eventholder) holder).eventCardviewBinding.LikeCount.getText().toString()) -1 ));
                }
                itemClickHandler.likestate(getItem(position));
            });

如何在细节片段中设置可绘制状态列表:

  public void ButtonUI()
{
    //LikeButton
    binding.likeButtonholder.setOnClickListener(v -> {
        if (likestate==0)
            likestate=1;
        else
            likestate=0;

        mainViewModel.init(firebaseAuth.getUid(),Events.getId(),Events.getEventName(),Events.getArt(),Events.getGENRE());
        mainViewModel.getLikeStateLiveData().observe(getViewLifecycleOwner(), state -> {
            if (state==1){
                Events.setUserLikedState(1);
                binding.likeButtonholder.setSelected(true);
                binding.likecounter.setText(String.valueOf((Events.getLikes()) + 1));
                Events.setLikes(Events.getLikes() + 1);
            }
            else if (state==0){
                //binding.likeButtonholder.setBackgroundResource(R.drawable.white_heartanimation);
                binding.likeButtonholder.setSelected(false);
                binding.likecounter.setText(String.valueOf((Events.getLikes()) - 1));
                Events.setUserLikedState(0);
                Events.setLikes(Events.getLikes() - 1);
            }
            else
                Toast.makeText(getContext(),"Error",Toast.LENGTH_LONG).show();
        });
    });
}
 public void Observe()
{
    //AccountData
    //EventDetailsData
    viewModel.getEvents().observe(getViewLifecycleOwner(), events -> {

      
        Events=events;
        likestate=events.getUserLikedState();
        if (likestate==0) {
            binding.likeButtonholder.getBackground().setState(new int[]{-android.R.attr.state_selected});
        }
        else
            binding.likeButtonholder.getBackground().setState(new int[]{android.R.attr.state_selected});
)};
}

我通过将动画可绘制对象也添加为 state_off 可绘制对象解决了我的问题!

<animated-selector
xmlns:android="http://schemas.android.com/apk/res/android">

<!-- provide a different drawable for each state-->

<item
    android:id="@+id/state_on"
    android:drawable="@drawable/heartfill"
    android:state_selected="true"
    />

<item
    android:id="@+id/state_off"
    android:drawable="@drawable/heart_unfill"
    />

<transition
    android:drawable="@drawable/heartfill"
    android:fromId="@id/state_off"
    android:toId="@id/state_on"
    />
<transition
    android:drawable="@drawable/heart_unfill"
    android:fromId="@id/state_on"
    android:toId="@id/state_off"
    />
<!-- specify transitions -->
这是正确的方法。 正如 Pawel 告诉我的那样,还更改了我的代码:
((Eventholder)holder).likestate=Objects.requireNonNull(getItem(position)).getUserLikedState();
            ((Eventholder)holder).eventCardviewBinding.likebutton.setSelected((((Eventholder)holder).likestate==1));
            ((Eventholder)holder).eventCardviewBinding.likebutton.jumpDrawablesToCurrentState();
            
                    ((Eventholder)holder).eventCardviewBinding.likebutton.setOnClickListener(v -> {
                if (((Eventholder)holder).likestate==1) {
                    ((Eventholder)holder).likestate=0;
                    ((Eventholder) holder).eventCardviewBinding.LikeCount.setText(String.valueOf(Integer.parseInt(((Eventholder) holder).eventCardviewBinding.LikeCount.getText().toString()) -1 ));
                }
                else {
                    ((Eventholder)holder).likestate=1;
                    ((Eventholder) holder).eventCardviewBinding.LikeCount.setText(String.valueOf(Integer.parseInt(((Eventholder) holder).eventCardviewBinding.LikeCount.getText().toString()) + 1));
                }
                ((Eventholder)holder).eventCardviewBinding.likebutton.setSelected(((Eventholder)holder).likestate==1);
                itemClickHandler.likestate(getItem(position));
            });