Organizing code files/XML files for Android SDK

核能气质少年 提交于 2019-12-01 05:04:33

Say I have a bunch of activities; is it good to place them all into a separate package, while putting other classes (such as custom Adapters) in another package to separate "logic" better?

I'm not sure what best practices are, but here's how I organize my applications: I tend to put my activities in com.foo.appname.activity, content providers in com.foo.appname.content, services in com.foo.appname.service, and generic utilities in com.foo.appname.utils.

For helper classes like Adapters that are only used by one activity, I typically make them static inner classes. If they're used in multiple activities, I'd give them package level visibility in the activity package.

I don't want to just throw them all into res/layout

I don't think that the res directories are allowed to have subdirectories, so the best you can do is to come up with a good naming scheme. I typically prefix the layout file with the type: activity_foo.xml, fragment_foo.xml, etc.

All these suggestions, of course are your choice. But when I develop something, I use to separate logical layer from "visible" layers and classes. I mean that I use different packages for

a) Activites
b) Classes or Objects
c) Interface classes
d) Database classes
e) Interaction with Database

I also create different packages for all of them, so you can organize them better. But this is always your choice.

And with your layout... I don't know if you can organize better the layout. When you generate your project, if you see, in your gen folder there's the R.java class. That class auto-detects folders such as layout, drawable, raw... But I'm not sure if you can make subfolders inside it.

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