問題描述
到目前為止,我已經在這方面花費了大約 6 個小時,并且遇到的只是障礙.一般的前提是 ListView
中有一些行(無論它是由適配器生成的,還是作為標題視圖添加的)包含一個 EditText
小部件和一個 按鈕
.我想要做的就是能夠使用慢跑球/箭頭,將選擇器導航到像正常一樣的單個項目,但是當我到達特定行時——即使我必須明確標識該行——具有焦點孩子,我希望那個孩子獲得焦點,而不是用選擇器指示位置.
I've spent about 6 hours on this so far, and been hitting nothing but roadblocks. The general premise is that there is some row in a ListView
(whether it's generated by the adapter, or added as a header view) that contains an EditText
widget and a Button
. All I want to do is be able to use the jogball/arrows, to navigate the selector to individual items like normal, but when I get to a particular row -- even if I have to explicitly identify the row -- that has a focusable child, I want that child to take focus instead of indicating the position with the selector.
我嘗試了很多可能性,但到目前為止都沒有運氣.
I've tried many possibilities, and have so far had no luck.
布局:
<ListView
android:id="@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
標題視圖:
EditText view = new EditText(this);
listView.addHeaderView(view, null, true);
假設適配器中還有其他項目,使用箭頭鍵將在列表中向上/向下移動選擇,如預期的那樣;但是當到達標題行時,它也與選擇器一起顯示,并且無法使用慢跑球將焦點放在 EditText
中.注意:點擊 EditText
會將其聚焦在該點上,但這依賴于觸摸屏,這不是必需的.
Assuming there are other items in the adapter, using the arrow keys will move the selection up/down in the list, as expected; but when getting to the header row, it is also displayed with the selector, and no way to focus into the EditText
using the jogball. Note: tapping on the EditText
will focus it at that point, however that relies on a touchscreen, which should not be a requirement.
ListView
在這方面顯然有兩種模式:
1. setItemsCanFocus(true)
:選擇器從不顯示,但EditText
可以在使用箭頭時獲得焦點.焦點搜索算法難以預測,并且沒有視覺反饋(在任何行:是否有可聚焦的子項)選擇了哪個項目,這兩者都可以給用戶帶來意想不到的體驗.
2. setItemsCanFocus(false)
:選擇器總是在非觸摸模式下繪制,EditText
永遠無法獲得焦點——即使你點擊它.
ListView
apparently has two modes in this regard:
1. setItemsCanFocus(true)
: selector is never displayed, but the EditText
can get focus when using the arrows. Focus search algorithm is hard to predict, and no visual feedback (on any rows: having focusable children or not) on which item is selected, both of which can give the user an unexpected experience.
2. setItemsCanFocus(false)
: selector is always drawn in non-touch-mode, and EditText
can never get focus -- even if you tap on it.
更糟糕的是,調用 editTextView.requestFocus()
返回 true,但實際上并沒有給 EditText 焦點.
To make matters worse, calling editTextView.requestFocus()
returns true, but in fact does not give the EditText focus.
我所設想的基本上是 1 和 1 的混合體.2,如果 all 項目是否可聚焦,而不是列表設置,我想為列表中的 single 項目設置可聚焦性,以便選擇器從為不可聚焦的項選擇整行,并遍歷包含可聚焦子項的焦點樹.
What I'm envisioning is basically a hybrid of 1 & 2, where rather than the list setting if all items are focusable or not, I want to set focusability for a single item in the list, so that the selector seamlessly transitions from selecting the entire row for non-focusable items, and traversing the focus tree for items that contain focusable children.
有接受者嗎?
推薦答案
對不起,回答了我自己的問題.它可能不是最正確或最優(yōu)雅的解決方案,但它對我有用,并提供了非??煽康挠脩趔w驗.我查看了 ListView 的代碼以了解為什么這兩種行為如此不同,并從 ListView.java 中發(fā)現了這一點:
Sorry, answered my own question. It may not be the most correct or most elegant solution, but it works for me, and gives a pretty solid user experience. I looked into the code for ListView to see why the two behaviors are so different, and came across this from ListView.java:
public void setItemsCanFocus(boolean itemsCanFocus) {
mItemsCanFocus = itemsCanFocus;
if (!itemsCanFocus) {
setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
}
所以,當調用 setItemsCanFocus(false)
時,它也設置了后代的可聚焦性,使得沒有孩子可以得到焦點.這就解釋了為什么我不能只在 ListView 的 OnItemSelectedListener 中切換 mItemsCanFocus
- 因為 ListView 會阻止所有子級的焦點.
So, when calling setItemsCanFocus(false)
, it's also setting descendant focusability such that no child can get focus. This explains why I couldn't just toggle mItemsCanFocus
in the ListView's OnItemSelectedListener -- because the ListView was then blocking focus to all children.
我現在擁有的:
<ListView
android:id="@android:id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:descendantFocusability="beforeDescendants"
/>
我使用 beforeDescendants
是因為只有當 ListView 本身(不是子項)具有焦點時才會繪制選擇器,因此默認行為需要是 ListView 先獲得焦點并繪制選擇器.
I use beforeDescendants
because the selector will only be drawn when the ListView itself (not a child) has focus, so the default behavior needs to be that the ListView takes focus first and draws selectors.
然后在 OnItemSelectedListener 中,因為我知道要覆蓋選擇器的標題視圖(需要更多的工作來動態(tài)確定任何給定位置是否包含可聚焦視圖),我可以更改后代的可聚焦性,并將焦點設置在 EditText.當我離開該標題時,再次將其改回.
Then in the OnItemSelectedListener, since I know which header view I want to override the selector (would take more work to dynamically determine if any given position contains a focusable view), I can change descendant focusability, and set focus on the EditText. And when I navigate out of that header, change it back it again.
public void onItemSelected(AdapterView<?> listView, View view, int position, long id)
{
if (position == 1)
{
// listView.setItemsCanFocus(true);
// Use afterDescendants, because I don't want the ListView to steal focus
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
myEditText.requestFocus();
}
else
{
if (!listView.isFocused())
{
// listView.setItemsCanFocus(false);
// Use beforeDescendants so that the EditText doesn't re-take focus
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
listView.requestFocus();
}
}
}
public void onNothingSelected(AdapterView<?> listView)
{
// This happens when you start scrolling, so we need to prevent it from staying
// in the afterDescendants mode if the EditText was focused
listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
注意注釋掉的 setItemsCanFocus
調用.通過這些調用,我得到了正確的行為,但是 setItemsCanFocus(false)
導致焦點從 EditText 跳轉到 ListView 之外的另一個小部件,返回到 ListView 并在下一個選定的對象上顯示選擇器項目,而跳躍的焦點讓人分心.刪除 ItemsCanFocus 更改,只需切換后代可聚焦性就可以讓我獲得所需的行為.所有項目都像往常一樣繪制選擇器,但是當到達帶有 EditText 的行時,它會專注于文本字段.然后,當繼續(xù)退出該 EditText 時,它又開始繪制選擇器.
Note the commented-out setItemsCanFocus
calls. With those calls, I got the correct behavior, but setItemsCanFocus(false)
caused focus to jump from the EditText, to another widget outside of the ListView, back to the ListView and displayed the selector on the next selected item, and that jumping focus was distracting. Removing the ItemsCanFocus change, and just toggling descendant focusability got me the desired behavior. All items draw the selector as normal, but when getting to the row with the EditText, it focused on the text field instead. Then when continuing out of that EditText, it started drawing the selector again.
這篇關于ListView 內的可聚焦 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!