Recyclerview动画问题

Recyclerview animation issue

我有一个 Recyclerview,其中的项目包含我试图按特定度数(逆时针)旋转的图标。问题是一些图标根本不旋转,或者在我滚动后(或 activity 恢复时)它们被设置回初始位置。如下图所示,前 3 个箭头根本没有旋转,而第 4 个箭头指向正确。 下面是自定义适配器

public class CustomListAdapter extends RecyclerView.Adapter<CustomListAdapter.TransportationViewHolder> {

Context ctx;
ArrayList<Transportation> transportationArrayList, copy;

public CustomListAdapter(Context ctx, ArrayList<Transportation> transportation) {
    this.ctx = ctx;
    this.transportationArrayList = new ArrayList<>(transportation);
  
}

@NonNull
@Override
public TransportationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.transportation_item_layout, parent, false);
    return new CustomListAdapter.TransportationViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull TransportationViewHolder holder, int position) {
    Transportation transportation = transportationArrayList.get(position);
    holder.transportationFleetTypeTextView.setText(description);
    rotateHeading((float)transportation.getHeading(), holder.headingImageView);
   
}

@Override
public int getItemCount() {
    return transportationArrayList.size();
}


public static class TransportationViewHolder extends RecyclerView.ViewHolder {
    TextView transportationFleetTypeTextView;
    ImageView transportationImageView, headingImageView;

    public TransportationViewHolder(View itemView) {
        super(itemView);
        transportationFleetTypeTextView = itemView.findViewById(R.id.transportation_item_text);
        transportationImageView = itemView.findViewById(R.id.transportation_item_image);
        headingImageView = itemView.findViewById(R.id.heading_image);
    }

    public void clearAnimation() {
        itemView.clearAnimation();
    }
}

@Override
public long getItemId(int position) {
    return position;
}

public void rotateHeading(float rotation, ImageView imageView)
{
    AnimationSet animSet = new AnimationSet(true);
    animSet.setInterpolator(new DecelerateInterpolator());
    animSet.setFillAfter(true);
    animSet.setFillEnabled(true);

    final RotateAnimation animRotate = new RotateAnimation(0f, 360f-rotation,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    animRotate.setDuration(0);
    animRotate.setFillAfter(true);
    animSet.addAnimation(animRotate);
    imageView.startAnimation(animSet);
}

@Override
public void onViewDetachedFromWindow(@NonNull final TransportationViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    holder.clearAnimation();
}
}

下面是我初始化适配器的方式(在扩展 AsyncTask 的 class 中)。

mainActivity.customListAdapter = new CustomListAdapter(mainActivity, transportationArrayList);
mainActivity.customListAdapter.setHasStableIds(true);
mainActivity.customListAdapter.notifyDataSetChanged();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mainActivity);
recyclerView = findViewById(R.id.transportationRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(customListAdapter);

有人可以向我解释我做错了什么以及为什么会这样吗?

由于持续时间为零并且您只想要动画的结果而不是动画本身,我建议您通过替换行来简化代码

rotateHeading((float)transportation.getHeading(), holder.headingImageView); 

holder.headingImageView.seRotation(360 = (float)transportation.getHeading())

并删除所有动画代码。

此更改可能无法解决您遇到的根本问题,但它会消除动画作为问题的根源。