問題描述
我在列表視圖中有一些編輯文本.我在edittext上有一個通用的focuslistener,它通過調用notifydatachanged更新數據模型的值以及當焦點丟失時edittext的背景.問題是,如果其中一個編輯文本保持焦點,當我觸摸下一個編輯文本時,它會暫時獲得焦點然后失去焦點.我懷疑這是由于 notifydatachanged 方法調用導致所有視圖被重繪,之后焦點丟失.有人對此問題有建議或解決方法嗎?謝謝.
I have a few edittext within a listview. i have a generic focuslistener on the edittext that updates the value of the data model and also the background of the edittext when focus is lost by calling notifydatachanged . The problem is that if one of the edittext is holding focus, when i touch the next edittext, it gains focus momentarily then loses focus. I suspect it is due to the notifydatachanged method call that is causing all views to be redrawn, after which the focus is lost. Does anyone have a suggestion or work around on the issue? Thanks.
推薦答案
這確實發生了,因為所有的視圖都被重繪了,所以代表任何過去被聚焦的行的編輯文本現在是一個完全不同的對象.在適配器中設置一個變量:int currentFocusedRow;
It is indeed happening because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object. Set a variable in your adapter: int currentlyFocusedRow;
在您的適配器的 getView 中:為每個編輯文本添加一個 onFocusChanged 偵聽器,當該編輯文本獲得焦點時,設置 currentFocusedRow = 焦點編輯文本恰好位于的任何行.還將 currentFocusedRow 中的任何編輯文本設置為專注.
in getView for your adapter: Add an onFocusChanged listener to each edit text and when that edit text gains focus, set currentlyFocusedRow = whatever row the focused edit text happens to be in. Also set any edit text that is in the currentlyFocusedRow to be focused.
這篇關于listview中的Android edittext失去了對調用notifydatachanged的關注的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!