1

Im buildind a android app and I'm using <Cardviews> inside a <RecyclerView>. My problem is when I remove a CardView. If I have 3 <CardView> and I remove one, the next <CardView> will have the size of the old one.

See:

enter image description here

Now when I remove the 1st:

enter image description here

I know that the problem is here:

 public void onDismiss(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                Tipss.remove(position);
                            }

                            adapter.notifyDataSetChanged();
                        }

Am I using the wrong adapter.notify method?

EDIT Main:

  rv=(RecyclerView)findViewById(R.id.rv);
    rv.setHasFixedSize(true);

Adapter:

public class TipsAdapter extends RecyclerView.Adapter<TipsAdapter.TipsViewHolder> {

public static class TipsViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView cardTitle;
    TextView cardText;

    TipsViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.card_view);
        cardTitle = (TextView)itemView.findViewById(R.id.card_title);
        cardText = (TextView)itemView.findViewById(R.id.card_text);
    }
}

List<Tips> Tipss;

TipsAdapter(List<Tips> Tipss){
    this.Tipss = Tipss;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public TipsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
    TipsViewHolder pvh = new TipsViewHolder(v);
    return pvh;
}

public void addListItem(Tips tip, int i)
{
    Tipss.add(tip);
    notifyItemInserted(i);
}
@Override
public void onBindViewHolder(TipsViewHolder TipsViewHolder, int i) {
    TipsViewHolder.cardTitle.setText(Tipss.get(i).title);
    TipsViewHolder.cardText.setText(Tipss.get(i).text);
}

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

You have already the remove upthere and that one is located in main.

António Paulo
  • 351
  • 3
  • 18

2 Answers2

0

Set on your recyclerView this method:

yourRecycler.setHasFixedSize(true)

This content is from Android APIs docs:

RecyclerView can perform several optimizations if it can know in advance that changes in adapter content cannot change the size of the RecyclerView itself. If your use of RecyclerView falls into this category, set this to true.

Hope it will help.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
thuongle
  • 537
  • 6
  • 10
0

I figured it out. The problem was on the method onDismiss. I need to remove the view adapter.notifyItemRemoved(position);, call the adapter and then I call this adapter.notifyDataSetChanged();

António Paulo
  • 351
  • 3
  • 18