用于处理UI事件的最佳实践事件、UI

2023-09-04 23:56:27 作者:这一枪便穿心

我已经把所有的约束code上的OnCreate UI事件()。它使我的OnCreate()巨大的。

I have put the all the binding code for UI events on OnCreate(). It has made my OnCreate() huge.

有没有解决Android中实现UI事件模式?我可以添加在查看XML文件的方法,然后我可以把所有的处理程序code别处。

Is there pattern around implementing UI events in android ? Can I add methods in View xml file and then I can put all the handler code somewhere else.

在简单地说,我觉得我是问我如何能实现MVVM模式与Android的应用程序code?

In a nutshell , I think I am asking how can I implement MVVM pattern with android app code ?

推荐答案

东西,我做的:

将所有的onClick功能的XML。避免了大量的混乱在Java code的。 在初始化事件侦听器作为活动类的成员,而不是让他们在一个函数。我不喜欢在我的code太多的花括号。混淆了地狱了我。 如果我的名单适配器受到太大我让他们在一个单独的类,而不是作为活动类的成员,然后将所有视图听众那里的适配器。 要避免创建太多的onClick功能,我有时会保持一个函数例如 onNavigatonClick ,然后使用 view.getId(),查看哪个按钮被点击。由于XML没有检查有效的函数调用,它会导致运行时错误,如果你的函数名是错误的。 如果一个特定视图需要大量的用户界面交互code,我创建一个GestureDetector处理UI交互自定义视图。 Keep all onClick functions in the XML. Avoids a lot of clutter in the Java code. Initialize event listeners as members of the activity class rather than keeping them in a function. I don't like too many curly braces in my code. Confuses the hell out of me. If my list adapters get too big I keep them in a separate class rather than as a member of the activity class and then keep all view listeners there in the adapter. To avoid creating too many onClick functions I sometimes keep one function like onNavigatonClick and then use view.getId() to see which button was clicked. As the XML is not checked for valid function calls, it leads to runtime errors if your function name is wrong. If a particular view needs a lot of UI interaction code, I create a custom view with a GestureDetector to handle UI interactions.

我想这仍然是相当基本的,因为我还没有与Java的很多经验呢。

I guess this is still quite basic as I haven't had much experience with Java yet.