【问题标题】:Android notifyDataSetChanged() only the last added itemAndroid notifyDataSetChanged() 仅最后添加的项
【发布时间】:2013-04-02 21:49:29
【问题描述】:

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

我想为列表视图中的一个小自定义部分设置动画。

我有从右向左滑动的局部动画 - 第一次效果很好。

在Adapter的方法中,类似如图所示,为简洁起见:

public View getView(){

    Animation an = AnimationUtils.loadAnimation(mContext, R.anim.transladar);
    an.reset();
    vi.startAnimation(an);
    return vi;
}

看起来不错,但是当我再添加一个项目时,chatAdapter.notifyDataSetChanged(); 会被触发,这会刷新我的所有项目并重新启动动画。

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

我希望你能帮助我。

【问题讨论】:

    标签: android android-listview android-animation notifydatasetchanged


    【解决方案1】:

    您可以添加公共布尔变量 (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);
       }
    
    }
    

    注意:这是快速解决方案,我不知道是否有其他方法。

    希望这对您有所帮助。

    【讨论】:

      【解决方案2】:

      您可以检查转换视图是否为空,然后准备动画。经过测试并为我工作:

      @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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-18
        • 2014-10-08
        相关资源
        最近更新 更多