I'm trying to make cardviews in recyclerview expand. I got the expanding part working, but when adding transition to it, some visual bugs start to occur. The transitions works fine, when there are no off-screen items, but when I add more than (in my case) 4 items to the recyclerview, it starts to occur.
GIF with 4 items
GIF with more than 4 items
The cardview expanding works fine with more than 4 items when I disable the transition animation. I think the problem is related to positions changing, but I can't find any solution to the problem.
The guide I used to implement the cardview expanding can be found here: https://stackoverflow.com/a/38623873/6673949
And my complete recyclerview adapter
public class BasketRecyclerAdapter extends RecyclerView.Adapter<BasketRecyclerAdapter.CustomViewHolder> {
private String letter;
private Context mContext;
private ColorGenerator generator = ColorGenerator.MATERIAL;
private List<Basket> baskets;
private int mExpandedPosition = -1;
private RecyclerView r1;
public BasketRecyclerAdapter(Context context, List<Basket> baskets, RecyclerView r1) {
this.mContext = context;
this.baskets = baskets;
this.r1 = r1;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.basket_menu_item, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final BasketRecyclerAdapter.CustomViewHolder holder, final int position) {
String basketName = baskets.get(position).getBasketName();
holder.basketName.setText(basketName);
letter = "" + basketName.charAt(0);
TextDrawable drawable = TextDrawable.builder()
.buildRound(letter, generator.getColor(basketName));
holder.imageLetter.setImageDrawable(drawable);
final boolean isExpanded = position == mExpandedPosition;
holder.expandedLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:position;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionManager.beginDelayedTransition(r1);
}
notifyDataSetChanged();
}
});
}
What can be done to solve the problem? Thanks.
Edit: I tried to get it to work by using just ListView instead of RecyclerView, but ListView adapter doesn't expand with same code - will try to figure out why.
Edit2: Got it working by using another library called "ExpandableLayout" but still can't seem to figure out why is it not working without additional libraries.
I had similar issue. Add in adapter:
@Override
public long getItemId(int position) {
return position;
}
And myAdapter.setHasStableIds(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With