【发布时间】:2016-10-21 18:49:33
【问题描述】:
我正在开发一个editText 和一个recyclerView。 当我在 EditText 中写信时,我的 recyclerView 会更新。
我在 textWatcher 中放置了一个 Timer,以避免每次用户写信时都发送请求。
searchDestinationEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
//There is nothing to do here
}
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
if (timer != null) {
timer.cancel();
}
}
@Override
public void afterTextChanged(final Editable s) {
timer = new Timer();
//we schedule this in order to avoid sending useless request.
//We wait the user is finishing writing before sending requests
timer.schedule(new TimerTask() {
@Override
public void run() {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
actionsListener.onDestinationSearch(s.toString());
}
});
}
}, DELAY_SEND_REQUEST);
}
});
它工作得很好,但leakcanary 说我在这部分代码中有泄漏。 有什么想法吗?
【问题讨论】:
-
@oiZo 谢谢,我会试试的。但这并没有解释为什么那里有泄漏:/。
标签: android memory-leaks timer textwatcher