Android Project: How best to organize the files

时间:2022-09-11 20:40:21

I'm building my first android app, and it's gotten a little messy already. I'm using List/detail patterns because they're what fit the circumstances, but because I'm developing for both mobile and tablet it's getting a bit out of hand. For every screen (List/detail being one screen), four files are created. ListActivity, ListFragment, DetailActivity, DetailFragment. Having four screens so far, and literally just starting the project, I have 12 files, plus three helper files for one database table.

我正在构建我的第一个Android应用程序,它已经变得有点混乱了。我正在使用List / detail模式,因为它们适合环境,但因为我正在开发移动设备和平板电脑,所以它有点失控。对于每个屏幕(列表/细节是一个屏幕),创建四个文件。 ListActivity,ListFragment,DetailActivity,DetailFragment。到目前为止有四个屏幕,并且字面上刚刚启动项目,我有12个文件,以及一个数据库表的三个帮助文件。

What I'm asking, is what's the best way to organize this? I'm using Android Studio, and it seems I can't sort the files into folders without putting them in separate packages. So do I do something like com.domain.app.screen1.(Fragments|Activities), com.domain.app.screen2.(Fragments|Activities) and so on? Or do I just put up with it? Or is there a better way of doing this?

我问的是,组织这个的最好方法是什么?我正在使用Android Studio,似乎我无法将文件排序到文件夹中而不将它们放在单独的包中。所以我会做一些像com.domain.app.screen1。(片段|活动),com.domain.app.screen2。(片段|活动)等等?或者我忍受了吗?或者有更好的方法吗?

If I'm being unclear, just let me know and I'll try to clear it up

如果我不清楚,请告诉我,我会尽力清除它

2 个解决方案

#1


26  

Good Explain by @Eric Oestrich:

@Eric Oestrich的好解释:

Writing a medium to large Android app requires having code structure. In creating our latest Android development project, I came across a structure that has helped me out.

编写中型到大型Android应用程序需要具有代码结构。在创建我们最新的Android开发项目时,我遇到了一个帮助我的结构。

Java Code :

Java代码:

  • com.example

    • activities

      Contains all the activities. Classes are all named with Activity at the end. That way, you can immediately know what it is when reading Java code that doesn't have its full package name.

      包含所有活动。最后,所有类都以Activity命名。这样,在阅读没有完整包名的Java代码时,您可以立即知道它是什么。

    • adapters

    Contains all the adapters.

    包含所有适配器。

    • authenticator

    Contains any class related to signing a user in. I create a local account and having all related classes together is very handy.

    包含与签署用户相关的任何类。我创建一个本地帐户并将所有相关的类放在一起非常方便。

    • data

    Contains all classes related to data management such as ContentProvider and SQLiteHelper.

    包含与数据管理相关的所有类,如ContentProvider和SQLiteHelper。

    • data.migrations

    Contains all of my SQLite migrations.

    包含我的所有SQLite迁移。

    • fragments

    Contains all fragments.

    包含所有片段。

    • helpers

    Contains helper classes. A helper class is a place to put code that is used in more than one place. I have a DateHelper for instance. Most of the methods are static.

    包含辅助类。帮助程序类是放置在多个位置使用的代码的地方。我有一个DateHelper。大多数方法都是静态的。

    • interfaces

    Contains all interfaces.

    包含所有接口。

    • models

    Contains all local models. When syncing from an HTTP API I parse the JSON into these Java objects using Jackson. I also pull Cursor rows into these models as well.

    包含所有本地模型。从HTTP API同步时,我使用Jackson将JSON解析为这些Java对象。我也将Cursor行拉入这些模型中。

    • preferences

    Contains all classes for custom preferences. When creating the preferences I required a custom PreferenceDialog as well as a custom PreferenceCategory. They live here.

    包含自定义首选项的所有类。创建首选项时,我需要自定义PreferenceDialog以及自定义PreferenceCategory。他们住在这里。

    • sync

    Contains all classes related to syncing. I use a SyncAdapter to pull data from an HTTP API. In addition to the SyncAdapter a SyncService is required, so I created a package.

    包含与同步相关的所有类。我使用SyncAdapter从HTTP API中提取数据。除了SyncAdapter之外,还需要SyncService,因此我创建了一个包。

Layouts :

  • Activity Layout name start with activity_
  • 活动布局名称以activity_开头

  • Adapter Layout row name start with row_
  • 适配器布局行名以row_开头

  • Fragment Layout name start with fragment_
  • 片段布局名称以fragment_开头

#2


11  

As far as i know, there is no convention, but here is an example of how you can put your files in packages :

据我所知,没有约定,但这里有一个如何将文件放入包中的示例:

  • mainPackage
    • LauncherFragment
    • LauncherActivity
    • MyApplication
  • mainPackage LauncherFragment LauncherActivity MyApplication

  • uiPackage
    • DetailsFragment
    • DetailsActivity
    • OtherTabletFragment
  • uiPackage DetailsFragment DetailsActivity OtherTabletFragment

  • viewPackage
    • custom views
  • viewPackage自定义视图

  • databasePackage
    • MainContentProvider
    • MainDBHelper
    • SecondContentProvider
    • SecondDBHelper
  • databasePackage MainContentProvider MainDBHelper SecondContentProvider SecondDBHelper

  • dataPackage
    • CustomAdapter
  • utilsPackage
    • xmlUtils
    • textUtils
  • utilsPackage xmlUtils textUtils

And many others. You can search for android projects on GitHub and have a look.

还有很多其他人。你可以在GitHub上搜索android项目并查看。

#1


26  

Good Explain by @Eric Oestrich:

@Eric Oestrich的好解释:

Writing a medium to large Android app requires having code structure. In creating our latest Android development project, I came across a structure that has helped me out.

编写中型到大型Android应用程序需要具有代码结构。在创建我们最新的Android开发项目时,我遇到了一个帮助我的结构。

Java Code :

Java代码:

  • com.example

    • activities

      Contains all the activities. Classes are all named with Activity at the end. That way, you can immediately know what it is when reading Java code that doesn't have its full package name.

      包含所有活动。最后,所有类都以Activity命名。这样,在阅读没有完整包名的Java代码时,您可以立即知道它是什么。

    • adapters

    Contains all the adapters.

    包含所有适配器。

    • authenticator

    Contains any class related to signing a user in. I create a local account and having all related classes together is very handy.

    包含与签署用户相关的任何类。我创建一个本地帐户并将所有相关的类放在一起非常方便。

    • data

    Contains all classes related to data management such as ContentProvider and SQLiteHelper.

    包含与数据管理相关的所有类,如ContentProvider和SQLiteHelper。

    • data.migrations

    Contains all of my SQLite migrations.

    包含我的所有SQLite迁移。

    • fragments

    Contains all fragments.

    包含所有片段。

    • helpers

    Contains helper classes. A helper class is a place to put code that is used in more than one place. I have a DateHelper for instance. Most of the methods are static.

    包含辅助类。帮助程序类是放置在多个位置使用的代码的地方。我有一个DateHelper。大多数方法都是静态的。

    • interfaces

    Contains all interfaces.

    包含所有接口。

    • models

    Contains all local models. When syncing from an HTTP API I parse the JSON into these Java objects using Jackson. I also pull Cursor rows into these models as well.

    包含所有本地模型。从HTTP API同步时,我使用Jackson将JSON解析为这些Java对象。我也将Cursor行拉入这些模型中。

    • preferences

    Contains all classes for custom preferences. When creating the preferences I required a custom PreferenceDialog as well as a custom PreferenceCategory. They live here.

    包含自定义首选项的所有类。创建首选项时,我需要自定义PreferenceDialog以及自定义PreferenceCategory。他们住在这里。

    • sync

    Contains all classes related to syncing. I use a SyncAdapter to pull data from an HTTP API. In addition to the SyncAdapter a SyncService is required, so I created a package.

    包含与同步相关的所有类。我使用SyncAdapter从HTTP API中提取数据。除了SyncAdapter之外,还需要SyncService,因此我创建了一个包。

Layouts :

  • Activity Layout name start with activity_
  • 活动布局名称以activity_开头

  • Adapter Layout row name start with row_
  • 适配器布局行名以row_开头

  • Fragment Layout name start with fragment_
  • 片段布局名称以fragment_开头

#2


11  

As far as i know, there is no convention, but here is an example of how you can put your files in packages :

据我所知,没有约定,但这里有一个如何将文件放入包中的示例:

  • mainPackage
    • LauncherFragment
    • LauncherActivity
    • MyApplication
  • mainPackage LauncherFragment LauncherActivity MyApplication

  • uiPackage
    • DetailsFragment
    • DetailsActivity
    • OtherTabletFragment
  • uiPackage DetailsFragment DetailsActivity OtherTabletFragment

  • viewPackage
    • custom views
  • viewPackage自定义视图

  • databasePackage
    • MainContentProvider
    • MainDBHelper
    • SecondContentProvider
    • SecondDBHelper
  • databasePackage MainContentProvider MainDBHelper SecondContentProvider SecondDBHelper

  • dataPackage
    • CustomAdapter
  • utilsPackage
    • xmlUtils
    • textUtils
  • utilsPackage xmlUtils textUtils

And many others. You can search for android projects on GitHub and have a look.

还有很多其他人。你可以在GitHub上搜索android项目并查看。