Android listview的适配器无法正常工作
我想在listitem按钮上显示动画。动画效果很好。我没有点击的其他一些按钮也会显示动画。我发现问题在于在适配器中循环使用视图。有人能帮我处理这种情况吗。。以下是我编写的代码:在适配器的getview方法中:Android listview的适配器无法正常工作,android,view,adapter,listitem,recycle,Android,View,Adapter,Listitem,Recycle,我想在listitem按钮上显示动画。动画效果很好。我没有点击的其他一些按钮也会显示动画。我发现问题在于在适配器中循环使用视图。有人能帮我处理这种情况吗。。以下是我编写的代码:在适配器的getview方法中: viewHolder.getrate.setOnClickListener( new CompoundButton.OnClickListener() { public void onClick(View paramView) { ListData ra
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);
}
});
您应该以某种方式删除
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 */);
}
public View getView(int位置、视图转换视图、视图组父视图){
//…为convertView充气、创建viewHolder等。
convertView.setBackgroundResource(0)//
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);
}