爪哇 - 机器人 - 斯普利特活动/类成多个文件,更好地组织(解决方案) [英] Java - Android - Split Activity/Class into multiple files for better organization (solution)

查看:181
本文介绍了爪哇 - 机器人 - 斯普利特活动/类成多个文件,更好地组织(解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专业的Java,但是我发现越来越疯狂了Android的活动变得太大的自己。
经过code长了几页,我发现自己的永久来回滚动!
不仅OnClickhandlers可以增长巨大的,只是很多code趋于总结在一个更大的活动。
片段,对我无解。
在一般的Java不允许分割类分成几个文件,所以黑客攻击是必要的。

I'm not PRO in JAVA , but I found myself of getting crazy with Android activities getting too large.
After a few pages of code length I found myself of permanently scrolling back and forth!
Not only OnClickhandlers can grow huge, just a lot of code tends to sum up in a bigger activity.
Fragments were no solution for me.
In general Java does not allow to split up a class into several files so a hack is needed.

我通过大量的StackOverflow线程有关的,而不是一个有一个解决方案浏览。
我解决了也无妨,也许这可以帮助别人。

I browsed through a lot of stackoverflow threads regarding that, not one had a solution.
I solved it anyway, maybe this helps others.

这是一个有点Android的重点,因为我得到了anoyed我的巨大的onclick处理所有的菜单和按钮,但我想它会为其他一般的Java问题以及工作方式相同。

This is a bit Android focused as I got anoyed by my huge onClick Handlers for all the menus and buttons but I guess it will work for other general Java problems as well the same way.

推荐答案

这是我遵循的是移动查看关注到一个单独的类的简单方法(姑且称之为V​​iewManager),使活动/片段的工作只是作为一个控制器

A simple approach that I follow is to move the View concerns into a separate class (let's call it ViewManager) and make the Activity/Fragment work only as a controller.

一个ViewManager一般负责 -

A ViewManager is generally responsible for -

  • 充气布局
  • 获取对所有的意见
  • 在显示数据的看法
  • 在处理点击/触摸事件
  • 动画

该活动/片段只负责 -

The Activity/Fragment is only responsible for -

  • 在读取数据,并把它传递给ViewManager以显示它
  • 在处理导航
  • 发布数据到服务器/数据库

有关触发控制器负责操作的UI控件,比如推出新的活动单击按钮时,该ViewManager收到的点击,并呼吁在需要导航照顾控制器的方法。

For UI controls that trigger an action that the controller is responsible for, say launching a new activity when a button is clicked, the ViewManager receives the click and calls a method in the controller that takes care of the navigation.

如果您想进一步消除样板code(单击处理程序,findViewById()调用等),考虑使用图书馆像的 ButterKnife

If you want to further eliminate boilerplate code (click handlers, findViewById() calls, etc), consider using libraries like ButterKnife.

这篇关于爪哇 - 机器人 - 斯普利特活动/类成多个文件,更好地组织(解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆