[英]What is the best way to organize android development code?
I know that every situation will be different, but I just wanted see if there was a general recommendation. 我知道每种情况都会有所不同,但我只是想看看是否有一般建议。
Currently, I have my activities (screens) dynamically creating custom button objects and custom edit text objects. 目前,我的活动(屏幕)是动态创建自定义按钮对象和自定义编辑文本对象的。 Each of these objects have listeners to see if their state has changed.
这些对象中的每个对象都有侦听器,以查看其状态是否已更改。 These object classes have all the logic for the screen.
这些对象类具有屏幕的所有逻辑。 The activity's only job is to assign objects to the widgets I created in XML.
该活动的唯一工作是将对象分配给我用XML创建的小部件。
Part of me thinks it should be opposite, where the activity contains all the logic for all the widgets on the screen and simply waits for the objects to notify it when the listeners go off. 我的一部分认为应该相反,该活动包含屏幕上所有小部件的所有逻辑,并且仅在侦听器关闭时等待对象通知它。
Which way is more "standard" ? 哪种方式更“标准”?
I use the following way. 我使用以下方式。 I have a common EventHandler sub class in every activity or fragment and I add a single instance belonging to activity to each UI item.
我在每个活动或片段中都有一个公共的EventHandler子类,并且向每个UI项添加了一个属于活动的实例。 EventHandler implements OnClickListener, OnChanged.., and so on.
EventHandler实现OnClickListener,OnChanged ..等。
I would also recommend looking at this library, if you are familiar with DI concept: 如果您熟悉DI概念,我也建议您查看此库:
https://github.com/roboguice/roboguice/ https://github.com/roboguice/roboguice/
Here is an example of code of mine: 这是我的代码示例:
package com.x.y;
public class DashboardActivity extends FragmentActivity {
private EventHandler eventHandler = new EventHandler();
@SomeAnnotationForInit(R.id.some_id)
private Button feedButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
initGui();
}
private void initGui() {
feedButton.setOnClickListener(eventHandler);
}
private class EventHandler implements View.OnClickListener {
@Override
public void onClick(View view) {
if(view.equals(...)) {
//TODO:
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.