Android:2个不同的视图意味着2个不同的活动?
我当前的应用程序有一个 Activity,主要的一个 Activity 扩展了 ListActivity(当然是 listview)。 主 Activity 还包含大部分应用程序功能。
我想添加一个选项,供用户在两个视图之间进行选择:当前的 listView 和 gridView。 我还想重用我的代码 - 大多数函数都可以用于两个视图。
在我提出实际问题之前,请注意,我的问题不是如何添加 gridView - 它已经添加并且效果很好(当注释掉所有 Listview 行时)。
我的问题是 - 我应该怎么做?
2 个不同的视图意味着我必须有 2 个不同的活动?一个扩展“ListActivity”,一个扩展“Activity”?
我是否应该创建 4 个类(在本例中为 3 个活动和函数类),其中主活动将根据用户选择的内容调用其他活动之一?
有没有办法为两个视图使用相同的主活动? (现在我扩展“ListActivity”以获取“getListView()”,这会导致 gridView 在 Activity 查找 ID 为“list”的 ListView 时强制关闭)。
还有其他办法吗?
谢谢你!
My current application has one Activity, the main one which extends ListActivity (listview of course).
The main Activity also holds most of the app functions.
I want to add an option for the user to select between 2 views: the current listView and a gridView.
I also want to reuse my code - most functions can be used for both Views.
Before I ask the actual question, note that my question is NOT how to add a gridView - it's already added and works great (when commenting out all Listview lines).
My question is - how should I do it?
2 different Views means I have to have 2 different Activities? one extends "ListActivity" and one extends "Activity"?
Should I create 4 classes (3 Activities and functions class in this case) where the main Activity will call one of the other Activities according to what the user selected?
Is there a way using the same main Activity for both Views? (right now I extend "ListActivity" to get "getListView()" which causes the gridView to force close as the Activity looks for a ListView with the ID "list").
Any other way?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在不使用 ListActivity 的情况下使用 ListView。如果您的活动代码的其余部分对于列表和网格表单都是类似的,我会推荐这种方法。动态选择要显示的活动,并根据需要根据 switch 或 if 语句定制适配器定义来执行这两项操作,而不是使用不同的活动。
You can use ListView without using a ListActivity. I would recommend this approach if the rest of your Activity's code will be similiar for both the List and Grid forms. Choose dynamically which one you show and tailor your adapter definitions to do both based on a switch or if statement where needed, rather than using different Activities.