Android - Activity 与 ListActivity - 我的活动类应该扩展哪一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2032417/
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 04:16:51  来源:igfitidea点击:

Android - Activity vs. ListActivity - Which one should my activity class extend?

androidandroid-activitylistactivity

提问by Bara

I've been learning to develop in Android and had more of a general question: If I have a layout that uses a list and some other views, should I be using Activity or ListActivity for my activity class?

我一直在学习在 Android 中进行开发,并且有更多的一般性问题:如果我有一个使用列表和其他一些视图的布局,我应该将 Activity 还是 ListActivity 用于我的活动类?

I know that ListActivity will probably make things easier to override list-specific events, but is there any other advantage to using a ListActivity over Activity? What if I wanted to change the layout in the future to a GridView? Would it be any more/less of a pain to change the class' code?

我知道 ListActivity 可能会使覆盖特定于列表的事件变得更容易,但是与 Activity 相比,使用 ListActivity 是否还有其他优势?如果我以后想将布局更改为 GridView 怎么办?更改类的代码会更痛苦/更少痛苦吗?

I was just curious about "best practices" in this regard and what the benefits entail, so any answer would be helpful :)

我只是对这方面的“最佳实践”以及好处感到好奇,所以任何答案都会有所帮助:)

Bara

巴拉

回答by Dave Webb

I'd use a ListActivitysince it gives you a lot of shortcut methods to make things easier and keep your code more readable.

我会使用 a ,ListActivity因为它为您提供了许多快捷方法,使事情变得更容易并使您的代码更具可读性。

For example, you get the onListItemClick() methodwhich is called whenever you click a item which saves you from creating a separate listener.

例如,您会在onListItemClick() method单击某个项目时调用 which,从而避免创建单独的侦听器。

If you want to change the layout of a ListActivityyou still can with setContentView()method from Activity. As long as there is a ListViewcalled @android:id/listsomewhere in your Viewthe ListActivitywill still work.

如果你想改变的布局ListActivity可以与你仍然setContentView()方法从Activity。只要在你的某个地方有一个ListView电话@android:id/listViewListActivity仍然可以工作。

If you're still not sure, you could always look at the source code for ListActivityand see that it doesn't do all that much other than make your life a little easier.

如果您仍然不确定,您可以随时查看源代码ListActivity,看看它除了让您的生活更轻松之外并没有做太多事情。

回答by Alex Volovoy

I'd go with Activity. Don't really see the reason to use ListActivity, unless you want to do something trivial and you know it's gonna be with the List.

我会去活动。不要真的看到使用 ListActivity 的原因,除非你想做一些微不足道的事情并且你知道它会与 List 一起使用。

回答by Tanya Agrawal

You should use Activity rather than ListActivity because ListActivity codes are non-reusable and you need to write them again and again.

您应该使用 Activity 而不是 ListActivity 因为 ListActivity 代码是不可重用的,您需要一次又一次地编写它们。