ListView 项目焦点行为 [英] ListView items focus behaviour

查看:17
本文介绍了ListView 项目焦点行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建 EditText 项目的 ListView(使用自定义 CursorAdapter),这样 EditTexts起初看起来不可编辑,长按后变为可编辑.然后用户将编辑 EditText 内容,EditText 将在失去焦点时将更改保存到 db.但是,我遇到了一种非常讨厌的行为,使我无法这样做.

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) 我在 XML 中将我的 EditTexts 设置为 android:focusable="false"android:focusableInTouchMode="false".

1) I've set my EditTexts to android:focusable="false" and android:focusableInTouchMode="false" in XML.

2) 我在 ListActivity 中创建了一个 OnItemLongClickListener,它执行以下操作:

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 true 调用 item1

hasFocus false 称为focused

如果我将行设置 focusable 删除为 false (et.setFocusableInTouchMode(false);),我会得到另一个 hasFocus true,称为 unfocused.显然事情是这样的:

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.

此行为可防止我在失去焦点或使其无法聚焦时禁用 EditText,直到下一次长单击通过,这是我想要的.谁能建议我可能缺少什么?或者解释这种行为?任何解决方案表示赞赏.

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 真正奇怪的焦点处理,直到我试验了为 ViewGroups 定义的 android:descendantFocusability 设置.在我将它设置为 ViewGroup.FOCUS_AFTER_DESCENDANTS 之后,它的行为变得更加可预测,至少对我来说是这样.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆