Android separate packages

对着背影说爱祢 提交于 2019-12-06 07:16:54

问题


I have a large UI (about 20-25 screens). How should I organize my code? Should I separate by functionality into different packages? Should I have one package for all UI classes and then create sub packages to organize? Or should I not create separate packages and organize into folders. Any help would be greatly appreciated.


回答1:


When you create a folder, it becomes a package. My favourite structure for large projects is as follows:

  • UI
  • Core / Logic
  • DAO / Connections
  • Utils
  • Models / TOs / VOs

Hope this helps.




回答2:


I've tried to separate my classes in logical packages as much as possible.

  • activities
  • adapters (adapters for listviews mostly)
  • asynctasks (communication with the server for the most part)
  • core (contains a constants class and the base activity that holds the global vairables, as well as the sliding menu and action bar)
  • fragments
  • interfaces (mostly listener/observer pattern interfaces, used to notify for success/failure of an asynctask or database operation)
  • localsqlitedb (contains the sqlqueries class, the database helper class and other db-related classes)
  • objects - contains plain old java objects (pojos) representing different entities from my app (a user, a user's item, a category)
  • utilities - images handling, email senders, custom toast makers and so on

I think its easy to maintain and reuse code organized this way

I also use naming conventions so I know what a class or a file is about at a glance

in the layouts folder:

activity_login.xml
activity_register.xml
dialog_delete_profile_confirmation.xml

in the drawable folder:

icon_feed.xml
icon_search.xml
images_loading_splash_screen.png
images_default_avatar.png

in the asynctasks package

GetUserDataFromServerAsync.class
RegisterUserAsync.class

(so that when I refer these in the code I know its not the internal handler class, but the class that communicates with the server)



来源:https://stackoverflow.com/questions/5783276/android-separate-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!