gpt4 book ai didi

android - 在 ListView 中交换项目动画

转载 作者:行者123 更新时间:2023-11-30 02:18:15 25 4
gpt4 key购买 nike

在我的 ListView 中,当我单击一个项目时,我想将它排序在列表的末尾,而列表的其余部分应该通过向上滚动来填补空白。我已经设法用这段代码为项目制作动画直到结束:

    public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater
.inflate(R.layout.list_item_goal_content, null);
}

...
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (animationRunning) {
return;
}

// only animate if item is not already at the end of list.
if (goals.indexOf(goal) != goals.size() - 1) {
int path = (getCount() - position) * animview.getHeight();

Animation animation = new TranslateAnimation(0, 0, 0, path);
animation.setDuration(700);


animation.setZAdjustment(Animation.ZORDER_TOP);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
animationRunning = true;
}

@Override
public void onAnimationEnd(Animation animation) {
animationRunning = false;
// simply remove from its position and append to end
goals.remove(goal);
goals.add(goal);
notifyDataSetChanged();
}

@Override
public void onAnimationRepeat(Animation animation) { }
});
convertView.startAnimation(animation);
}
}
});
}
return convertView;
}

但我不知道如何向上滚动项目以填补空白。

最佳答案

最后我决定重建我的列表并使用 RecyclerView。它包含一个内置的 ItemAnimator。我没有像我问题中的代码那样启动自己的动画,而是简单地用它来交换项目:

// get position of the item to be moved
int indexOf = goals.indexOf(goal);
goals.remove(goal); // remove from its position
goals.add(goal); // add to end of list
// Notify view about the move: it will handle the animation itself :)
notifyItemMoved(indexOf, goals.size()-1);

重建我的代码花了几个小时,但现在我很高兴没有卡在 Listview 上。

关于android - 在 ListView 中交换项目动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com