我有一个自定义类,用作View
中每个项目的ListView
。当View
初始化时,它会每秒开始自我更新。
我在哪里可以检测到视图被删除,并取消定时器?
修改:这是视图的代码。
public class CustomViewCell extends LinearLayout {
private static final String TAG = "CustomViewCell";
private ImageView itemImage;
private TextView itemTitle;
private TextView itemEstimate;
private CustomEvent event;
private Handler mHandler = new Handler();
public CustomViewCell(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
protected void onFinishInflate() {
Log.v(TAG, "OnFinishInflate");
itemImage = (ImageView) findViewById(R.id.itemImage);
itemTitle = (TextView) findViewById(R.id.itemTitle);
itemEstimate = (TextView) findViewById(R.id.itemEstimate);
super.onFinishInflate();
}
public void renderWithEvent(CustomEvent event){
this.event = event;
String imageURL = event.imageURL();
if (imageURL != null){
Picasso.with(getContext()).load(imageURL).into(itemImage);
}else{
itemImage.setImageResource(android.R.color.transparent);
}
itemTitle.setText(event.title);
mHandler.removeCallbacksAndMessages(null);
updateEstimate();
}
public void updateEstimate(){
itemEstimate.setText(event.getEstimate());
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
updateEstimate();
}
}, 1000);
}
}
这里是ListView适配器的代码:
private class EventsAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomEvent event = (CustomEvent) getItem(position);
Context context = getActivity();
CustomViewCell eventItem;
if (convertView == null){
eventItem = (CustomViewCell) LayoutInflater.from(context).inflate(R.layout.cell_event, parent, false);
}else{
eventItem = (CustomViewCell) convertView;
}
eventItem.renderWithEvent(event);
return eventItem;
}
...
}
答案 0 :(得分:1)
我想我明白你在寻找什么。试试onDetachedFromWindow()。
答案 1 :(得分:0)
mListView.setRecyclerListener(new AbsListView.RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
}
});
复制