我試圖在ListView
的某個項目內不時動畫一個視圖。這是一個自定義的視圖,它擴展了RelativeLayout
,其目的是不時從幾個來源(包括互聯網)獲取一些信息,並顯示它。 只要有新信息可用,就會以編程方式創建新的子視圖。舊視圖是在自定義視圖(RelativeLayout)外動畫的,而新視圖是動態顯示的,就像ViewFlipper的工作原理一樣。 此視圖在放置在常規佈局中時工作正常,但將其放入ListView項目時遇到問題。問題在於它在列表中的一個或兩個滾動後停止重繪自己 - 在它離開屏幕並再次返回後立即停止。動畫不再可見,只有在與列表進行交互時纔會繪製自定義視圖。 我試圖使視圖無效,無效列表和其他選項,但是,這是行不通的...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;
}
}
}
}
好動畫手動啓動每10秒的代碼和重用(安卓小重要怪癖)。問題在於,在退出列表中的可見項並返回之後,視圖不會重新繪製。我把日誌放入我的應用程序,並且動畫過程被觸發。但是,在UI上沒有更新(未重繪)。 – Daniel
我在我的問題中添加了一些代碼。 – Daniel
我已經用可能的解決方案更新了答案。雖然暫時未經測試,但請告訴我是否可行;) – pablisco