odoo 自定义视图_通过使用合并和自定义视图组重构您的活动或片段

odoo 自定义视图

In the Android application, Activity or Fragment would be the base component for the single page. You can put other UI controls into a single layout xml and inflate it in anActivity or a Fragment . One of the problems is that if the UI is complicated, the Activity or Fragment might also include complicated UI logic. In general case, a single page is a composite of many basic UI controls like TextView, Button, ImageView , etc. You need to implement the presentation logic in theActivity or the Fragment even in the Controller, Presenter or ViewModel.

在Android应用程序中,“ Activity或“ Fragment将是单个页面的基本组件。 您可以将其他UI控件放入单个布局xml中,然后将其填充到ActivityFragment 。 问题之一是,如果UI复杂,则ActivityFragment也可能包含复杂的UI逻辑。 通常,单个页面是由许多基本UI控件(如TextViewButtonImageView等)组成的。您需要在ActivityFragment甚至在ControllerPresenterViewModel中实现表示逻辑。

一个简单的例子 (A simple example)

Assume that we want to implement a simple digest UI like this

假设我们要实现一个简单的摘要UI,例如

This widget includes 3 TextViews in a ViewGroup. So we can declare the UI layout in xml like this.

此小部件在ViewGroup中包含3个TextView。 因此,我们可以像这样在xml中声明UI布局。

Image for post
Layout xml for digest widget
摘要小部件的布局xml

then we can add the UI setup function in Fragment

然后我们可以在Fragment添加UI设置功能

Image for post

and thanks new ViewBinding, we don’t need to use findViewById by ourselves. And then we want to add the right arrow icon to the right of Detail TextView and assign the same color and adjust the dimension. So we add one more function to do it.

还要感谢new ViewBinding ,我们不需要自己使用findViewById 。 然后,我们想在Detail TextView的右侧添加右箭头图标,并分配相同的颜色并调整尺寸。 因此,我们增加了一个功能。

Image for post
set the right drawable to a TextView
将正确的可绘制对象设置为TextView

As you can see, this is not unusual in the Android development. If the UI includes many widgets, the fragment will become a fatter because it includes lots of presentation logics. Besides, if we want to reuse the UI widget in another fragment, it would be difficult.

如您所见,这在Android开发中并不罕见。 如果UI包含许多小部件,则该片段将变得更加胖,因为它包含许多表示逻辑。 此外,如果我们想在另一个片段中重用UI小部件,将很困难。

Could we have a better solution? One of them is that we can create a custom view group and use the layout file with a<merge> tag. So we can move presentation logics into the custom view group.

我们能有更好的解决方案吗? 其中之一是,我们可以创建一个自定义视图组,并使用带有<merge>标签的布局文件。 因此,我们可以将表示逻辑移动到自定义视图组中。

<merge>技巧 (The <merge> tricks)

By the official document, the <merge> is usually used with <include> tag. The purpose of <merge> is to eliminate redundant view groups in the view hierarchy. Actually, we can also create a custom view group and inflate the layout xml with <merge>. Let’s see how to do it.

官方文档中<merge>通常与<include>标记一起使用。 <merge>的目的是消除视图层次结构中的冗余视图组。 实际上,我们还可以创建一个自定义视图组,并使用<merge>填充布局xml。 让我们来看看如何做。

First, We change the root tag of original xml to <merge> like below.

首先,我们将原始xml的根标签更改为<merge>如下所示。

Image for post
replace the ConstraintLayout to <merge>
将ConstraintLayout替换为<merge>

Then we create a class to extend ConstraintLayout

然后我们创建一个类来扩展ConstraintLayout

Image for post

The key point is that we inflate the xml and attach to the root ViewGroup.

关键是我们将xml膨胀并附加到根ViewGroup。

LayoutInflater
.from(context)
.inflate(R.layout.layout_digest, this, true)

and then we have a compound UI widget and we can also use it another xml layout directly!

然后我们有一个复合UI小部件,我们还可以直接使用它另一个XML布局!

<Digest 
android:id="@+id/digest"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Next, we move the presented functions into the new class and create 2 String properties

接下来,我们将呈现的函数移到新类中并创建2个String属性

Image for post

Finally, we can move all presented code into the custom ViewGroup and use it in the Fragment or Activity directly.

最后,我们可以将所有呈现的代码移到自定义ViewGroup中,然后直接在FragmentActivity使用它。

// In Fragment
viewBinding.digest.title = "This is title"
viewBinding.digest.content = "This is content"

利弊 (Pros and Cons)

By using <merge> in the xml can help us to extract the presented code from Fragment or Activity. We can more focus on business logic. Of course it is not a perfect solution. Here is the Pros and Cons as I know.

通过在xml中使用<merge>可以帮助我们从FragmentActivity提取呈现的代码。 我们可以更加关注业务逻辑。 当然,这不是一个完美的解决方案。 据我所知,这是优点和缺点。

优点 (Pros)

  • We can move the presentation logic which belongs a “View” to the appropriate place

    我们可以将属于“视图”的表示逻辑移动到适当的位置

  • Reduce the complexity of Fragment or Activity

    降低FragmentActivity的复杂性

  • The UI can be reused in another place.

    UI可以在其他地方重复使用。
  • If you don’t like to use Fragment, you can use this trick to replace Fragment.

    如果您不喜欢使用Fragment ,可以使用此技巧来替换Fragment

缺点 (Cons)

  • You may create many custom widgets

    您可以创建许多自定义小部件
  • Some properties assigned in xml may not work. It depends on how you initialize the ViewGroup now

    xml中分配的某些属性可能不起作用。 这取决于您现在如何初始化ViewGroup

That’s it! If you have any feedback, please don’t hesitate to leave your comments.

而已! 如果您有任何反馈意见,请随时发表评论。

翻译自: https://levelup.gitconnected.com/refactoring-your-activity-or-fragment-by-using-merge-and-custom-viewgroup-4c7de7c2be71

odoo 自定义视图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Odoo新建自定义视图,需要进行以下操作: 1. 在odoo的python代码中增加新视图类型与视图模式。这可以通过在ir_actions_act_window.py和ir_ui_view.py文件中添加相关代码来实现。例如,在ir_actions_act_window.py文件中,我们可以增加以下代码来定义新的视图模式: ``` # -*- coding: utf-8 -*- from odoo import fields, models class ActWindowView(models.Model): _inherit = 'ir.actions.act_window.view' view_mode = fields.Selection(selection_add=[('eview', 'echart views')]) ``` 在ir_ui_view.py文件中,我们可以添加以下代码来定义新的视图类型: ``` # -*- coding: utf-8 -*- from odoo import fields, models class View(models.Model): _inherit = 'ir.ui.view' type = fields.Selection(selection_add=[('eview', 'echart views')]) ``` 这样,Odoo就可以识别我们新定义的视图类型。 2. 定义js相关文件和模板代码。通过编写相应的javascript文件和模板代码,我们可以实现对自定义视图的布局和行为的控制。这些文件需要与新定义的视图类型相关联,并与视图模板文件进行交互。具体的实现方法可以根据项目需求和技术要求来确定。 这样,在执行以上操作之后,就可以在Odoo中创建自定义视图了。自定义视图可以根据具体需求来设计和实现,包括自定义的布局、交互和样式等方面 。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Odoo自定义视图教程](https://blog.csdn.net/u012739578/article/details/121670077)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值