ListView的项目主要集中行为 [英] ListView items focus behaviour
问题描述
我试图创建一个的ListView
(自定义的CursorAdapter
)的EditText的
的项目,使得 EditTexts
出现不可编辑的第一,并成为可编辑后,长按。然后,用户将编辑的EditText
内容和的EditText
将更改保存在失去焦点为DB。但是我碰到了$ P $从这样pvents我真是可恶行为。
I'm trying to create a ListView
(with custom CursorAdapter
) of EditText
items such that EditTexts
appear uneditable at first and become editable upon long click. Then the user would edit EditText
contents and EditText
would save changes to db upon losing focus. However I've run into a really nasty behaviour that prevents me from doing this.
1)我已经把我的 EditTexts
到机器人:可调焦=假
和安卓:focusableInTouchMode = XML中的假
。
1) I've set my EditTexts
to android:focusable="false"
and android:focusableInTouchMode="false"
in XML.
2)我已经创建了一个 OnItemLongClickListener
我的 ListActivity
,做了以下内容:
2) I've created an OnItemLongClickListener
in my ListActivity
that does the following:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d("NLAc", "longClick called");
EditText et = (EditText) view.findViewById(R.id.etFolderName);
et.setFocusable(true);
et.setFocusableInTouchMode(true);
return true;
}
3)当我创建视图在我的适配器附上以下焦点变化监听器:
3) And when I create views in my adapter I attach the following focus change listener:
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
EditText et = (EditText)v;
Log.d(TAG, "hasFocus true called " + et.getText());
et.setText("focused");
et.setSelection(et.length());
}
else
{
EditText et = (EditText)v;
Log.d(TAG, "hasFocus false called " + et.getText());
et.setText("unfocused");
et.setFocusableInTouchMode(false);
//TODO Save to DB
}
}
会发生什么事是,当我长的点击,我得到的日志在下面的第一项:
What happens is that when I long click the very first item I get the following in the log:
longClick名为
hasFocus真名为ITEM1
hasFocus假称为集中
如果我删除行设置可聚焦到假( et.setFocusableInTouchMode(假);
)我得到另一个 hasFocus真所谓聚焦
。
显然,事情是这样的:
If I remove the line setting focusable to false (et.setFocusableInTouchMode(false);
) I get another hasFocus true called unfocused
.
Apparently things go like this:
1)的EditText
获得焦点时,设置可聚焦
1) EditText
gets focus when set focusable
2) LinearLayour
包含我的的ListView
失去焦点,并调用内部 unFocus()
在所有它的孩子,包括我的的EditText
2) LinearLayour
containing my ListView
loses focus and calls internal unFocus()
on all it's children including my EditText
3)的EditText
失去焦点
4)的EditText
得到重视 - 。现在不管是什么原因
4) EditText
gets focus - for whatever reason now.
此行为prevents我从禁用的EditText
在失去焦点或使其unfocusable,直到下一个长按来通过这正是我想要的。
任何人都可以提出什么,我可能会丢失?或者解释这种现象?任何解决方案AP preciated。
This behaviour prevents me from disabling EditText
upon losing focus or making it unfocusable until the next long click comes through which is what I want.
Can anyone suggest what I may be missing? Or explain this behaviour? Any solutions appreciated.
推荐答案
我能够用在彼此的前两名意见,取得了一些进展。我使用一个TextView为重点不突出显示和我隐藏它,并显示一个EditText而不是在长期点击。但是,这并不能真正帮助先是用ListView的很奇怪重点处理,直到我尝试用安卓descendantFocusability
设置定义ViewGroups。之后,我已经将它设置为 ViewGroup.FOCUS_AFTER_DESCENDANTS
它的行为变得更加predictable,至少对我来说。
I was able to make some progress by using two views on top of each other. I'm using a TextView for unfocused display, and I hide it and show an EditText instead on long clicked. But that didn't really help at first with ListView's really weird focus handling until I've experimented with android:descendantFocusability
setting defined for ViewGroups. After I've set it to ViewGroup.FOCUS_AFTER_DESCENDANTS
it's behaviour became much more predictable, at least for me.
这篇关于ListView的项目主要集中行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!