Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android notifyDataSetChanged()仅显示最后添加的项_Android_Android Listview_Android Animation_Notifydatasetchanged - Fatal编程技术网

Android notifyDataSetChanged()仅显示最后添加的项

Android notifyDataSetChanged()仅显示最后添加的项,android,android-listview,android-animation,notifydatasetchanged,Android,Android Listview,Android Animation,Notifydatasetchanged,我有一个ListView及其扩展了BaseAdapter的适配器类 我想给listview中的一个自定义片段设置动画 我有动画的部分,这是从右到左滑动-这是非常好的第一次 在适配器的方法中,为简洁起见,类似于图中所示: public View getView(){ Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar); an.reset(); vi.startAnimation(

我有一个
ListView
及其扩展了
BaseAdapter
的适配器类

我想给listview中的一个自定义片段设置动画

我有动画的部分,这是从右到左滑动-这是非常好的第一次

在适配器的方法中,为简洁起见,类似于图中所示:

public View getView(){

    Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
    an.reset();
    vi.startAnimation(an);
    return vi;
}
看起来不错,但当我再添加一项时,
chatAdapter.notifyDataSetChanged()被触发,刷新所有我的项目并重新启动动画

我只想在适配器本身中设置最后一项的动画


希望您能帮助我。

您可以添加公共布尔变量(
isFirstTime=true;
)并在您的
getView()中进行如下检查

if(isFirstTime){
    Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
    an.reset();
    vi.startAnimation(an);

   if(position == getCount()-1) isFirstTime =false;
}else{
  if(position == getCount()-1){
     Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
    an.reset();
    vi.startAnimation(an);
   }

}
注:这是一个快速解决方案,我不知道是否还有其他方法


希望这对你有帮助

您可以检查convertview是否为空,然后准备动画。测试并为我工作:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (null == convertView) {
        convertView = LayoutInflater.from(getActivity()).inflate(
                R.layout.workshop_components_list_item, null);
    }

    Animation animation = null;
    ViewHolder holder = (ViewHolder) convertView.getTag();
    if (null == holder) {
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView_listItemName);
        holder.position = position;
        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.components_list_item);
        animation.setDuration(500);
    }

    ListItem item = getItem(position);
    holder.name.setText(item.getName());

    if (null != animation) {
        convertView.startAnimation(animation);
        animation = null;
    }

    return convertView;
}