【问题标题】:The adapter of listview is not working perefctlistview 的适配器工作不完美
【发布时间】:2014-05-13 17:51:47
【问题描述】:

我想在列表项按钮上显示动画。动画效果很好。我没有点击的其他一些按钮也显示了动画。我发现问题出在适配器中的回收视图上。谁能帮我处理这种情况..这是我写的代码:在适配器的getview方法中:

viewHolder.getrate.setOnClickListener(
   new CompoundButton.OnClickListener() {
      public void onClick(View paramView) {
         ListData rateobj = (ListData) viewHolder.getrate.getTag();
         paramView.setBackgroundResource(R.drawable.spin_animation);

         // Get the background, which has been compiled to an AnimationDrawable object.
         frameAnimation = (AnimationDrawable) paramView.getBackground();

         // Start the animation (looped playback by default).
         frameAnimation.start();

         NetworkRun nt = new NetworkRun(rateobj);
         String number=rateobj.getDescription();
         String num=number.replaceAll("\\s+","");
         nt.execute(num);

         viewHolder.load.setEnabled(true);
         viewHolder.load.setVisibility(View.VISIBLE);
     }
  });

【问题讨论】:

    标签: android view adapter listitem recycle


    【解决方案1】:

    您应该以某种方式删除getView(...) 中的动画,因此每次重用视图时都会重置动画。 我建议以下想法:

    public View getView(int position, View convertView, ViewGroup parent) {
        // ... inflate convertView, create viewHolder, etc.
        convertView.setBackgroundResource(0); // <-- this will remove animation
        viewHolder.getrate.setOnClickListener(/* your code here */);
    }
    

    唯一的问题是即使对于应该存在的项目,动画也会消失。 对于这种情况,您可以通过存储动画项目的位置集来修改解决方案。

    Set<Integer> animatedPositions = new HashSet<Integer>();
    public View getView(final int position, View convertView, ViewGroup parent) {
        // ... inflate convertView, create viewHolder, etc.
        if(animatedPositions.contains(position)) {
             showAnimation(convertView);
        } else {
             hideAnimation(convertView);
        }    
        viewHolder.getrate.setOnClickListener(
          new OnClickListener() {
              public void onClick(View paramView) {
                  animatedPositions.add(position);
                  showAnimation(paramView);
                  // your code
              }
          }
        );
    }
    
    private void showAnimation(View view) {
        view.setBackgroundResource(R.drawable.spin_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) paramView.getBackground();
        frameAnimation.start();
    }
    
    private void hideAnimation(View view) {
        convertView.setBackgroundResource(0);
    }
    

    当然,您应该在移除动画时(在网络执行之后)从集合中移除位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多