使用Android构建动态表单的解决方案(Solution to build dynamic forms with Android)

编程入门 行业动态 更新时间:2024-10-06 22:24:29
使用Android构建动态表单的解决方案(Solution to build dynamic forms with Android)

我实际上正在开发一个Android应用程序,我应该在其上显示基于JSON文档中包含的元数据的动态表单。 基本上它的工作方式(没有细节)是JSON文档代表一个表单的结构:

{ "fields": [ { "name": "fieldA", "type": "STRING", "minCharacters": 10, "maxCharacters": 100 }, { "name": "fieldB", "type": "INTEGER", "min": 10, "max": 100 }, { "name": "fieldC", "type": "BOOLEAN_CHECKBOX", "defaultValue": true } ... ], "name": "Form A" }

当应用程序收到其中一个JSON文档时,我实际上正在做的是它遍历每个字段并将其解析到适当的视图(EditText,Checkbox,自定义视图等),向视图添加标记(将是能够轻松检索它并将视图添加到LinearLayout 。 这是一个伪代码,它实际上是如何工作的:

//Add form title linearLayout.addView(new TextView(form.name)); //Add form fields for(Field field: form.fields) { View view; switch(field.type){ case STRING: view = new EditText(); ... } view.setTag(field.id); linearLayout.addView(view); }

这个问题是,对于大型表单(如> 20个字段),它需要充斥大量视图,并且UI线程会受到很多影响。 另一点需要考虑的是,单个屏幕可能有多种形式(一个接一个地垂直排序)。

为了避免重载UI线程我想到了2个可能的解决方案:

使用RecyclerView通过Facebook使用Litho

但在考虑这两个解决方案时,我会遇到多个问题:

使用Litho是一个很好的用例吗? 或者使用RecyclerView就足够了? 我的观点如何? 如果我使用回收模式,我是否能够保持每个字段的状态(甚至是屏幕外的那些字段),从而能够在不丢失数据的情况下保存表单? 如果我使用回收模式显示一个表单,我将如何处理多个表单? 我们可以嵌套RecyclerView吗? 表格需要像在垂直房车内一个接一个地显示,但如果表格本身是RV,我应该如何处理?

这更像是一个“良好实践”问题,并提供正确的方法或正确的方法来实现我的目标,而不是需要具有代码示例的特定答案等。

在此先感谢您的时间。

I am actually developing an Android application on which I should display dynamic forms based on metadata contained inside JSON documents. Basically the way it works (without the details) is that a JSON document represent the structure of a form:

{ "fields": [ { "name": "fieldA", "type": "STRING", "minCharacters": 10, "maxCharacters": 100 }, { "name": "fieldB", "type": "INTEGER", "min": 10, "max": 100 }, { "name": "fieldC", "type": "BOOLEAN_CHECKBOX", "defaultValue": true } ... ], "name": "Form A" }

What I'm doing actually when the application receive one of those JSON documents is that it loop through each fields and parse it to the appropriate view (EditText, Checkbox, custom view, etc.), adding a tag to the view (to be able to retrieve it easily) and add the view to a LinearLayout. Here is a pseudo-code of how it is working actually:

//Add form title linearLayout.addView(new TextView(form.name)); //Add form fields for(Field field: form.fields) { View view; switch(field.type){ case STRING: view = new EditText(); ... } view.setTag(field.id); linearLayout.addView(view); }

The issue with this is that with large forms (like >20 fields), it need to inflate lot of views and the UI thread suffer a lot. Another point to take into account is that a single screen may have multiple forms (one after another vertically sorted).

To avoid overloading the UI thread I thought of 2 possible solutions:

Using a RecyclerView. Using Litho by Facebook.

But multiple questions comes to me when considering these 2 solutions:

Is it a good use case to use Litho? Or using a RecyclerView is enough? What about the state of my views? If I use a Recycling pattern, would I be able to keep the state of each of my fields (even those off-screen) and so being able to save the form without losing data? If I use a Recycling pattern to display one form, how would I handle multiple forms? Can we have nested RecyclerView? Forms need to be displayed one after another like inside a vertical RV but if forms themselves are RV, how should I handle this?

This is more a "good practice" question and giving the right way or one of the right way of achieving my goal than a need of a specific answer with code example, etc.

Thank's in advance for your time.

最满意答案

在构建移动应用程序时,我想解决以下问题:

使用Litho是一个很好的用例吗? 或者使用RecyclerView就足够了?

视图是否正确回收:对我们来说意味着什么,每个屏幕创建40-50个视图,当用户移出视图时,系统不应该标记GC的所有视图,而应该在某种存档列表中当我们再次要求它时,我们应该可以从它获取。

为什么我们需要这样做:GC是最昂贵的操作,会导致应用渲染抖动,我们尝试通过不清除视图来最小化要调用的GC

为此,我想使用光刻,理由是在这里,因为您的要求似乎有更多可变计数的viewtypes 参考

结论:Litho +1,RecyclerView +0

我的观点如何? 如果我使用回收模式,我是否能够保持每个字段的状态(甚至是屏幕外的那些字段),从而能够在不丢失数据的情况下保存表单?

在RecyclerView中保存EditText内容这是一个组件,但同样的逻辑应该应用于checkbox或radiobutton 。 或者像在石头的状态维护中一样

结论:Litho + 1,RecyclerView +1都有特定的API来实现状态维护

如果我使用回收模式显示一个表单,我将如何处理多个表单? 我们可以嵌套RecyclerView吗? 表格需要像在垂直房车内一个接一个地显示,但如果表格本身是RV,我应该如何处理?

这必须通过用户体验和技术能力来解决:根据用户行为恕我直言,我不鼓励嵌套的垂直滚动,但其他人能够实现它,你可以很容易地找到如何在SO中。 最佳解决方案是在Andriod's或litho的回收站视图中进行水平滚动

注意:如果您需要了解实施细节,请将其作为单独的问题提出 ,我很乐意为您提供帮助


更新#1:

这个问题是,对于大型表单(如> 20个字段),它需要充斥大量视图,并且UI线程会受到很多影响。

必须在后端执行UI创建/布局,只需在UI线程上添加视图即可。 而且光刻是内置的 。 然而,同样可以实现原生回收者视图,但您必须移出UI线程并定期发布到UI。


When architecting for the mobile application I would like to address the following questions:

Is it a good use case to use Litho? Or using a RecyclerView is enough?

Are the views are being recycled properly: What does it mean to us is consider, creating 40-50 view per screen and as user moves out of the view, system should not mark all views for GC rather it should be inside some kind archived list and as we require it again we should be able to fetch from it.

Why do we need to that: GC is the costliest operation which would cause app rendering to be jitter, we try to minimize the GC to called at this point by not clearing the views

For this I would like to go with litho, justification is here as your requirement seems to have more of variable count of viewtypesreference

Conclusion: Litho +1, RecyclerView +0

What about the state of my views? If I use a Recycling pattern, would I be able to keep the state of each of my fields (even those off-screen) and so being able to save the form without losing data?

Saving EditText content in RecyclerView This is one the component but same logic should be appliced to checkbox or radiobutton as well. or as in state-maintenance for litho is here

Conclusion: Litho +1, RecyclerView +1 both has specific API's to achieve state maintenance

If I use a Recycling pattern to display one form, how would I handle multiple forms? Can we have nested RecyclerView? Forms need to be displayed one after another like inside a vertical RV but if forms themselves are RV, how should I handle this?

This has to be addressed with user experience plus technical capability: As per user behaviour IMHO,I discourage the nested vertical scroll however others were able to achieve it you can easily find on how to in SO. Optimal solution would be to have horizontal scroll within either Andriod's or litho's recycler view as here

NOTE: If you need to know implementation details, please raise it as separate question, I would be happy to help there


UPDATE #1:

The issue with this is that with large forms (like >20 fields), it need to inflate lot of views and the UI thread suffer a lot.

UI creation/layout has to be performed at the backend only adding to the view has to be done on UI thread. And litho does it in-built. However same can be achieved native recycler view as well but you have to move off the UI thread and post periodically to UI.


更多推荐

本文发布于:2023-08-04 19:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   解决方案   动态   Android   forms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!