Animation on reordering ListView Item

时间:2015-05-24 22:20:23

标签: android listview animation android-listview adapter

I have a ListView with items containing two TextViews. When I click on one of the TextViews, I reorder the list (by sending the clicked item to the bottom of the list) and I want to make an animation for that.

I have an OnClickListener on the TextView in the adapter, so I'll have to animate the ListView from the Adapter. Is that even possible ? What is the best way to achieve this ?

Right now, the animation I made only makes the clicked item disappear (along with other items, I don't know why...), and the rest of the list doesn't move.

Thanks in advance for the help ! Here's my adapter code :

public class ListAdapter extends BaseAdapter {

private final Context mContext;
private List<Card> cards;

public ListAdapter(Context context, List<Card> objects) {
    super();
    mContext = context;
    cards = objects;
}

@Override
public int getCount() {
    return cards.size();
}

@Override
public Card getItem(int position) {
    return cards.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext)
                .inflate(R.layout.card, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.setText((TextView) convertView.findViewById(R.id.text));
        viewHolder.setCompteur((TextView) convertView.findViewById(R.id.compteur));
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.getText().setText(getItem(position).getText());
    viewHolder.getCompteur().setText(String.valueOf(getItem(position).getCompteur()));

    TextView text = viewHolder.getText();
    TextView compteur = viewHolder.getCompteur();

    final View view = convertView;

    compteur.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            changeItem(position, view);
        }
    });

    return convertView;
}

public List<Card> getCards() {
    return cards;
}

public void changeItem(int position, final View v) {
    int compteur = getItem(position).getCompteur();
    if (compteur >= 1) {
         compteur--;
    }
    getItem(position).setCompteur(compteur);
    // animating the row if it's zero
    if (compteur == 0) {
        Animation animation = AnimationUtils.loadAnimation((ListActivity)mContext, R.anim.abc_shrink_fade_out_from_bottom);
        long duration = animation.getDuration();
        v.startAnimation(animation);
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                v.setVisibility(View.GONE);
            }
        }, duration);
        Collections.rotate(cards.subList(position, cards.size()), -1);
    }
    notifyDataSetChanged();
}

1 个答案:

答案 0 :(得分:0)

感谢CommonsWare的回复,我使用了RecyclerView,并且更简单地设置了项目的插入和删除动画。