Reputation: 43
I have a Recyclerview the items of which contain icons that I am trying to rotate by specific degrees (counter clockwise). Problem is some of the icons do not rotate at all, or after I scroll (or when the activity resumes) they are set back to their initial position. As seen in the image below, the first 3 arrows have not been rotated at all, while the 4th arrow is pointing correctly. Below is the custom adapter
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();
}
}
And below is the way I initialize the adapter (which is within a class that extends AsyncTask).
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);
Could someone explain to me what I am doing wrong and why this is happening?
Upvotes: 0
Views: 113
Reputation: 62831
Since the duration is zero and you only want the result of the animation and not the animation itself, I suggest that you simplify the code by replacing the line
rotateHeading((float)transportation.getHeading(), holder.headingImageView);
with
holder.headingImageView.seRotation(360 = (float)transportation.getHeading())
and deleting all the animation code.
This change may not fix the underlying issue that you are having, but it will eliminate animation as the source of the problem.
Upvotes: 1