ListView中的动画

时间:2011-12-07 10:53:49

标签: android android-listview android-animation

我正在尝试在ListView的项目中不时为视图设置动画。它是一个自定义视图,它扩展了RelativeLayout,其目的是从多个来源(包括互联网)不时获取一些信息,并显示它。 只要有新信息,就会以编程方式创建新的子视图。旧视图在自定义视图(RelativeLayout)中生成动画,而新视图则动画显示,与ViewFlipper的工作方式完全相同。 放置在常规布局中时,此视图工作正常,但将其放入ListView项目时出现问题。问题是它在列表上的一个或两个滚动之后停止重绘 - 就在它离开屏幕并再次返回之后。动画不再可见,只有在与列表交互时才会绘制自定义视图。 我尝试使视图无效,使列表和其他选项无效,但是,这不起作用......

非常感谢任何帮助。感谢。

这里有一些类似于我为自定义视图尝试的确切机制的代码(这里我使用ViewFlipper来检查我的视图是否有问题。显然它的行为相同,在与列表交互后没有重绘脚本)

package com.test.viewflipper;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class ViewFlipperTestActivity extends ListActivity {
  /** Called when the activity is first created. */

  private ViewFlipper viewFlipper;
  private Animation TRANSLATE_LEFT_TO_RIGHT_VIEW_IN = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f,
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private Animation TRANSLATE_LEFT_TO_RIGHT_VIEW_OUT = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
      Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    viewFlipper = new ViewFlipper(this);
    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    viewFlipper.setLayoutParams(new AbsListView.LayoutParams(params));
    viewFlipper.setInAnimation(TRANSLATE_LEFT_TO_RIGHT_VIEW_IN);
    viewFlipper.setOutAnimation(TRANSLATE_LEFT_TO_RIGHT_VIEW_OUT);
    for (int i = 0; i < 10; i++) {
      Button button = new Button(getApplicationContext());
      button.setText("Button " + i);
      viewFlipper.addView(button);
    }
    viewFlipper.setFlipInterval(1000);
    viewFlipper.setAutoStart(true);
    viewFlipper.startFlipping();

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    setListAdapter(new AdListAdapter(layoutInflater));
  }

  private class AdListAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;

    public AdListAdapter(LayoutInflater layoutInflater) {
      this.layoutInflater = layoutInflater;
    }

    @Override
    public int getCount() {
      return 10;
    }

    @Override
    public Object getItem(int position) {
      return null;
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      /* return the ad container for the first position */
      if (position == 0) {
        return viewFlipper;
      }
      else {
        View view = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setText("Item " + position);
        return view;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果是自定义视图,您是否尝试手动启动动画?以编程方式或通过xml创建动画,然后使用view.startAnimation(animation)

你也可以展示一些代码吗?


<强>更新

好的,首先,动画每秒开始。但我建议从活动中删除viewFlipper并将自定义适配器重写为:

private class AdListAdapter extends ArrayAdapter<String> {

private ViewFlipper viewFlipper;
private LayoutInflater layoutInflater;

public AdListAdapter(Context context) {
  super(context, android.R.layout.simple_list_item_1, android.R.id.text1);
  // NOTE: This should be done with an xml
  viewFlipper = new ViewFlipper(context);
  LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  viewFlipper.setLayoutParams(new AbsListView.LayoutParams(params));
  viewFlipper.setInAnimation(TRANSLATE_LEFT_TO_RIGHT_VIEW_IN);
  viewFlipper.setOutAnimation(TRANSLATE_LEFT_TO_RIGHT_VIEW_OUT);
  viewFlipper.setFlipInterval(1000);
  for (int i = 0; i < 10; i++) {
    Button button = new Button(getApplicationContext());
    button.setText("Button " + i);
    viewFlipper.addView(button);
    // add an item to the adapter
    add("Item " + i);
  }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  if (position == 0) {
    // check if it's flipping
    if(!viewFlipper.isFlipping()) {
      viewFlipper.startFlipping();
    }
    return viewFlipper;
  } else if(convertView == viewFlipper) {
    // makes sure that the flipping is stopped when the viewFlipper is reused
    if(viewFlipper.isFlipping()) {
      viewFlipper.stopFlipping();
    }
    convertView = null;
  }
  return super.getView(position, convertView, parent);
 }
}

然后使用:

创建它
setListAdapter(new AdListAdapter(this));

我还没有对它进行测试,但除了简化代码和重用视图(Android的小重要怪癖)之外,它可能会以这种方式工作