简体   繁体   English

Android:我可以将参数传递给膨胀视图吗?

[英]Android: can I pass parameters to inflated views?

I'm writing a calendar app for Android. 我正在为Android编写日历应用程序。 This app has a day view, week view and month view, very similar to the standard calendar apps. 该应用具有日视图,周视图和月视图,与标准日历应用非常相似。

All three views need to know what dates they are showing before the calls to onMeasure, onLayout, onDraw. 这三个视图都需要在调用onMeasure,onLayout,onDraw之前知道它们显示的日期。

Now, I've been thinking whether I should create fully customized view classes, or use XML layouts as much as possible, and embed custom views inside the layouts only when I need to, but I hit a brick wall with the second option: again, since all layouts will have at least one sub-view that depends on the selected dates, and the only way I know to pass parameters to a view is by calling a custom constructor (as opposed to inflating where I can't pass params at all) - basically, I would HAVE to use completely custom views... 现在,我一直在考虑是应该创建完全自定义的视图类,还是尽可能使用XML布局,并且仅在需要时才将自定义视图嵌入到布局中,但是我遇到了第二种选择: ,因为所有布局都会有至少一个取决于所选日期的子视图,所以我知道将参数传递给视图的唯一方法是调用自定义构造函数(而不是在无法传递参数的地方夸大全部)-基本上,我将使用完全自定义的视图...

Am I correct about this? 我对此是否正确? Is there no way to pass parameters to inflated views? 有没有办法将参数传递给膨胀视图?

The method inflate() is for converting a layout.xml to a View instance. inflate()方法用于将layout.xml转换为View实例。 You can do that but much more work is needed, and passing dynamic data seems impossible. 您可以这样做,但是还需要做更多的工作,并且传递动态数据似乎是不可能的。

But I think your brick wall is not a really brick wall: 但是我认为您的砖墙并不是真正的砖墙:

since all layouts will have at least one sub-view that depends on the selected dates. 因为所有布局都将至少具有一个取决于所选日期的子视图。

I figure that you wanna set data before the view shows, so you can: 我认为您想在视图显示之前设置数据,因此您可以:

    View view = LayoutInflater.from({context}).inflate(R.layout.{XML_name}, null);
    TextView tv_year = (TextView)view.findViewById(R.id.tv_year);
    tv_year.setText("2015");
    ...
    {root_view}.addView(view);

{context} can be your Activity, {XML_name} is what you wanna use, and {root_view} is where you wanna add your date view. {context}可以是您的Activity,{XML_name}是您要使用的对象,{root_view}是您要添加日期视图的位置。

您可以编写一个容器视图以添加dayView,weekView和monthView,单击dayView或单击滚动时通知回调,以通知weekview和monthview扩大差异视图或刷新适配器

This is a good practice: separating views from functional code makes it more readable. 这是一个好习惯:将视图与功能代码分开会使代码更具可读性。 You can definetely inflate custom classes from your custom_layout.xml into your code. 可以将custom_layout.xml中的自定义类定义膨胀到代码中。 To do so, set your custom class (extended from some view) as the name of a tag: 为此,请将自定义类(从某些视图扩展)设置为标记的名称:

<com.my_site.MyView>
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/my_view"
</com.my_site.MyView>

If your custom view class constructor accepts extra parameters, you will also need custom LayoutInflator factory , where you can set these parameters. 如果您的自定义视图类构造函数接受其他参数,则还需要自定义LayoutInflator factory ,您可以在其中设置这些参数。 To make this approach common for different cases you can always pass a single MyParameters class instance that will accept parameter values of different types and give them back in target constructor. 为了使这种方法在不同情况下通用,您始终可以传递一个MyParameters类实例,该实例将接受不同类型的参数值并将其返回给目标构造函数。

In my case I found it easier to pass all customization away from constructor to separate methods to call them after MyView is instantiated. 以我为例,我发现在实例化MyView之后,将所有自定义项从构造函数传递到单独的方法来调用它们更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM