Android ListView 中的焦点控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/572053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:17:54  来源:igfitidea点击:

Focus control in a ListView

androidlistviewfocus

提问by yanchenko

The context: I want to have a ListView that wouldn't receive focus (e.g. won't highlight row when user touches it). Yet each row widget has it's own OnClickListener. Here's what I specify in layout xml:

上下文:我想要一个不会获得焦点的 ListView(例如,当用户触摸它时不会突出显示行)。然而,每一行小部件都有它自己的 OnClickListener。这是我在布局 xml 中指定的内容:

android:choiceMode="none" 
android:focusableInTouchMode="false"
android:focusable="false"

The ListView still behaves exactly the same. Could someone please explain

ListView 的行为仍然完全相同。有人可以解释一下吗

  1. The interrelation between the three
  2. What's the right way to create a ListView that doesn't receive focus?
  1. 三者之间的关系
  2. 创建未获得焦点的 ListView 的正确方法是什么?

TIA.

TIA。

采纳答案by Will

The list receiving focus is different than the selected row not highlighting. The list gets focus whenever a user is in it. The best a ListView can do is report an intfor whatever the user has selected. I'm not sure how each row widget has it's own ClickListener. There are no row widgets that I am aware of. The onListItemClickbelongs to the ListView.

接收焦点的列表不同于未突出显示的选定行。只要用户在列表中,列表就会获得焦点。ListView 可以做的最好的事情就是报告int用户选择的任何内容。我不确定每个行小部件如何拥有自己的 ClickListener。没有我所知道的行小部件。在onListItemClick属于ListView控件。

I havent been able to figure it out yet, but between android:listSelectorand android:backgroundand adjusting the alpha channels I figure there would be a way to make a selection look just like a non-selected row.

我还没有弄清楚,但是在alpha 通道和调整 alpha 通道之间android:listSelectorandroid:background我认为有一种方法可以使选择看起来像一个未选择的行。

android:listSelector="#8fff"makes it so just the foreground changes on selection.

android:listSelector="#8fff"使得它只是选择时的前景变化。

It seems like android chooses the non-selected foreground on its own, which is making this hard. I hope this helps.

似乎 android 自己选择了未选择的前景,这使得这很难。我希望这有帮助。

回答by Quintin Robinson

Although you specified in the xml you could try specifying in code as well. Although i'm not sure you can set a list to not focusable and still have the list be scrollable and it's clickable.

尽管您在 xml 中指定,但您也可以尝试在代码中指定。尽管我不确定您是否可以将列表设置为不可聚焦,并且仍然可以使列表可滚动且可点击。

after your setContentView...

在您 setContentView 之后...

myListView.setFocusableInTouchMode(false);

You could try and inherit from the view as well and then add a little debugging code in the interim to help you find when the list actually has focus.

您也可以尝试从视图继承,然后在此期间添加一些调试代码以帮助您找到列表何时真正具有焦点。

    myListView.setOnFocusChangeListener(new OnFocusChangeListener(){

        public void onFocusChange(View v, boolean hasFocus)
        {
            v.setBackgroundColor(hasFocus ? Color.GRAY : Color.BLACK);
        }
    });

    myListView.setClickable(true);

回答by SSG

Cant you do with a RadioButton thats selected when user touches a Row on the List?

当用户触摸列表上的一行时,您不能使用选中的 RadioButton 吗?

Following is a excerpt from my own code.

以下是我自己代码的摘录。

// Create an adapter with a simple list item with single choice.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, categories);

// Set the adapter to your list
listView.setAdapter(adapter);

Cheers!

干杯!