阻止在现有条目上执行ListView插入动画

时间:2013-12-07 21:44:12

标签: java android listview animation android-listview

我观看了Android开发者的视频,了解如何在YouTube上插入和删除ListView动画。这些代码中包含非常复杂的代码,我尝试搜索其他内容并找到了它。

http://karnshah8890.blogspot.fi/2013/04/listview-animation-tutorial.html

它具有非常简单的实现,适用于大多数部分。我的问题是,当我在提供给ArrayAdapter的ArrayList中插入一个新项时,由于adapter.notifyDataSetChanged()而导致所有现有元素重新插入......我想。当我在ListView中插入一个条目时,如何防止所有条目再次动画?我只想让新的动画进入。

我在代码中使用我的ListView:

toggles = (ListView) v.findViewById(R.id.toggleListView);

adapter = new ToggleListAdapter(getActivity(), toggleTasks);
toggles.setAdapter(adapter);

插入新项目后,我更新ArrayList(toggleTasks),然后在适配器上调用notifyDataSetChanged()

toggleTasks.add(task);
adapter.notifyDataSetChanged();

这是ArrayAdapter中的getView()方法。

public View getView(int position, View convertView, ViewGroup parent) {     
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ToggleTask task = toggles.get(position);

    String startActionName = "";
    String finishActionName = ""; 
    int imageDrawableId = -1;

    // Deleted the part where I get the values for those three variables above

    final Holder holder;
    if(convertView == null) {
        convertView = inflater.inflate(R.layout.mute_toggles_toggle_view, null);

        holder = new Holder();
        holder.image = (ImageView) convertView.findViewById(R.id.image);
        holder.startAction = (TextView) convertView.findViewById(R.id.startAction);
        holder.finishAction = (TextView) convertView.findViewById(R.id.finishAction);

        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }

    holder.image.setImageResource(imageDrawableId);
    holder.startAction.setText(startActionName);
    holder.finishAction.setText(finishActionName);

    Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.listview_push_left_in); // Third animation example on the site linked earlier
    animation.setDuration(500);
    convertView.startAnimation(animation);
    animation = null;

    return convertView;
}

3 个答案:

答案 0 :(得分:1)

在ArrayAdatper类中添加一个全局整数(我将其称为changedIndex)并为其创建getter和setter。然后在向列表中添加项目之前说一下adapter.setChangedIndex(index)(无论你为什么设置setter)。你基本上是在告诉你想要制作动画的那个。

在getView中更改此内容

if(changedIndex == position){
    Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.listview_push_left_in); // Third animation example on the site linked earlier
    animation.setDuration(500);
    convertView.startAnimation(animation);
    animation = null;
}

然后改变

toggleTasks.add(task);
adapter.notifyDataSetChanged();

toggleTasks.add(task);
toggles.getPosition(task);
adapter.notifyDataSetChanged();

*我没有检查过这些代码,但我相信它应该可行

答案 1 :(得分:1)

如果你想animate只插入last item,那么在设置动画之前检查它是否是List中的最后一项。

if (position == getCount() - 1) {

Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.listview_push_left_in); // Third animation example on the site linked earlier
    animation.setDuration(500);
    convertView.startAnimation(animation);
    animation = null;

}

答案 2 :(得分:1)

使用ListViewAnimations库。设置ListViewListAdapter之后:

MyListAdapter mAdapter = new MyListAdapter(this, getItems());
SwingRightInAnimationAdapter swingRightInAnimationAdapter = new SwingRightInAnimationAdapter(mAdapter);

// Assign the ListView to the AnimationAdapter and vice versa
swingRightInAnimationAdapter.setAbsListView(getListView());
getListView().setAdapter(swingRightInAnimationAdapter);

稍微调用AnimationAdapter.setShouldAnimateFromPosition(int position)

swingRightInAnimationAdapter.setShouldAnimateFromPosition(swingRightInAnimationAdapter.getCount());
swingRightInAnimationAdapter.add(myItem);

有关如何使用该库的更多信息,请查看wiki

相关问题