gpt4 book ai didi

android - 检测何时从 Android ListView 中删除 View

转载 作者:行者123 更新时间:2023-11-30 02:05:44 26 4
gpt4 key购买 nike

我有一个自定义类,用作 ListView 中每个项目的 View。当 View 初始化时,它开始每秒更新一次。

我在哪里可以检测到 View 正在被删除,并取消计时器?

编辑:这是 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;
}

...
}

最佳答案

我想我明白你在找什么。试试 onDetachedFromWindow()。

关于android - 检测何时从 Android ListView 中删除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30697528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com