Android:列表视图上的多个操作 - 焦点问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/500264/
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
Android : Multiple Actions on a List View - Focus Issue
提问by Chrispix
I would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).
我想实现一个 ListView,我可以用我的光标来完成它。现在,取决于您单击哪一行,它会将您带到基于该行上按下的信息的新活动(就像它应该的那样,正如预期的那样)。我想要一个按钮来删除该行,因此用户可以按下该行的任何部分来启动新活动,但是如果他们按下该行上的按钮,它会删除该行(或启动一个删除活动/功能)。
If you can look @ DroidRecord, they have a similar layout as I am looking to achive.
如果您可以查看@ DroidRecord,它们的布局与我希望实现的布局相似。
Thanks!
谢谢!
Chris.
克里斯。
采纳答案by Mariano Kamp
what is your question? How to add a button to a list row?
你的问题是什么?如何将按钮添加到列表行?
Very simple, just as you expect it will be added to the row's layout.
非常简单,正如您所期望的那样,它将被添加到行的布局中。
Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentationthat didn't work for me
不幸的是,尽管这也会使整行“不可触碰”。我问过的 Google 开发人员说这是设计使然(据我所知),您应该使用 TouchDelegate 来解决这个问题。因为没有示例,甚至在 android 源代码中也没有,只有非常薄的文档对我不起作用
Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?
无论如何,似乎没有多少应用程序使用列表行中的按钮。我只知道我的(newsrob,见文章列表)和闹钟。也许你可以使用上下文菜单?
Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.
否则,丑陋的解决方案是在 getView 方法的行视图上添加调用 setOnClickListener() 。
Cheers
干杯
回答by BoD
As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:
正如 Mariano Kamp 所说,将按钮添加到一行将使其“不可触碰”,但根据我的经验,如果您在按钮上设置这些属性,这个问题就会消失:
android:focusable="false"
android:focusableInTouchMode="false"
See also How to fire onListItemClick in Listactivity with buttons in list?
回答by Hatchmaster
Another possible workaround - you can use an ImageView instead of the button, and set the ImageView's onClickListener (For example, when you're inflating the cell view).
另一种可能的解决方法 - 您可以使用 ImageView 而不是按钮,并设置 ImageView 的 onClickListener(例如,当您膨胀单元格视图时)。
ImageView is not focusable so it doesn't prevent OnListItemClick() from being dispatched, and when you click on the image only the image's listener fires.
ImageView 不可聚焦,因此它不会阻止 OnListItemClick() 被调度,并且当您单击图像时,仅会触发图像的侦听器。
回答by pupeno
It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: How do you implement context menu in a ListActivity on Android?
这不是您问题的答案,但长按/标签通常是弹出上下文菜单并执行额外操作(例如删除)的地方。您可以在此处阅读如何操作:如何在 Android 上的 ListActivity 中实现上下文菜单?
回答by Darkendorf
I would like to thank BoDfor its hinton removing the focusable
state to the button(s), it saved my day.
我要感谢董事会对公司提示上取下focusable
状态的按钮(一个或多个),它救了我的一天。
But for information, since the button is no more focusable
- state_focused
on < selector > xml - , its design won't display anymore to the user.
但是对于信息,由于按钮不再focusable
-state_focused
在 < selector > xml - 上,它的设计将不再向用户显示。
Those buttons will still get the state pressed
though, but also when clicking anywhere else on the parent view (anywhere BUT another button) !
pressed
尽管如此,这些按钮仍然会获得状态,而且在单击父视图上的任何其他地方(除了另一个按钮之外的任何地方)时也会获得状态!
Keep that in mind, it could not be a good solution for your own case, but it does work well.
请记住,对于您自己的情况,这可能不是一个好的解决方案,但它确实运行良好。
回答by DannyY2K4StackOverflow
I tried this to be able to click on the buttons but it didn't work for me android:focusable="false" android:focusableInTouchMode="false"
我试过这样可以点击按钮,但它对我不起作用 android:focusable="false" android:focusableInTouchMode="false"
so what I did was to change the activity layout to scrollview and then add a linerLayout inside of it. after that you can add buttons to the layout and each button will be clickable.
所以我所做的是将活动布局更改为滚动视图,然后在其中添加一个 linerLayout 。之后,您可以向布局添加按钮,并且每个按钮都可以点击。