是否可以onClick使用数据绑定库将自定义参数传递给方法?我有我的布局xml文件,我需要使用onClickListener:
<?xml version="1.0" encoding="utf-8"?>
<layout ...>
<data>
<variable
name="viewModel"
type="com.productivity.tiktak.ui.tracker.viewModel.CategoryViewModel"/>
<variable
name="callback"
type="com.productivity.tiktak.ui.tracker.TrackerAdapter"/>
</data>
<android.support.v7.widget.CardView
android:onClick="@{callback.onCategoryClick(viewModel)}"
...
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- ... Some stuff -->
</android.support.v7.widget.CardView>
</layout>
Run Code Online (Sandbox Code Playgroud)
我在这里有我的点击处理程序代码:
public void onCategoryClick(View view, CategoryViewModel categoryViewModel)
{
//handler code...
}
Run Code Online (Sandbox Code Playgroud)
是否可以将我的CategoryViewModel对象从xml传递到单击处理程序?
我在使用数据绑定和包含标签时遇到以下错误:
Error:Execution failed for task ':app:dataBindingProcessLayoutsBetaDebug'.>data binding error msg:Only one layout element and one data element are allowed. [path to file] has 3file:[path to file]****\ data binding error ****
Run Code Online (Sandbox Code Playgroud)
这是我的布局文件:
[...]
<LinearLayout
android:id="@+id/activity_description_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="@+id/activity_description_header_bottom"
layout="@layout/activity_description_header_bottom" />
<include
android:id="@+id/activity_description_contact_info"
layout="@layout/activity_description_contact_info" />
<include
android:id="@+id/activity_description_other_info_box"
layout="@layout/activity_description_other_info_box" />
<include
android:id="@+id/activity_description_bottom_buttons"
layout="@layout/activity_description_bottom_buttons" />
</LinearLayout>
[...]
</layout>
Run Code Online (Sandbox Code Playgroud)
在每个包含的布局中,我有这样的东西:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
[...]
</layout>
Run Code Online (Sandbox Code Playgroud)
从这个回复:Android数据绑定使用包含标签我想我的代码是正确的,为什么数据仓认为我在文件中使用多个单个标签?
我想在同一个视图中多次使用我的一个布局include.假设我有custom.xml一些包含TextViews.
custom.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我已将此布局多次包含在parent.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/custom"
android:id="@+id/layout1"/>
<include layout="@layout/custom"
android:id="@+id/layout2"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
现在我想将我的数据模型绑定到这个布局,但问题是我不知道如何绑定两个不同的数据模型layout1,layout2因为它们都是从一个布局中引用的custom.xml.据我所知,我可以在我的xml布局中添加此标记:
<data>
<variable name="user" type="com.myproject.model.User"/>
</data>
Run Code Online (Sandbox Code Playgroud)
但我需要绑定两个不同的数据模型custom.xml.
我的问题是如何在一个视图中多次包含布局并使用数据绑定将不同的数据传递给它们?比如将数据传递给布局但不是将模型静态绑定到xml.
我也发现这个问题确实存在同样的问题但是由于数据绑定是在较新版本的android中发布的,我正在寻找一种方法来解决使用数据绑定的相同问题.以下是我引用澄清的问题的一部分:
例如,我有一个精心设计的布局,我希望在我的视图中显示三次.每个实例都需要不同的值.由于
include基本上是XML并将其粘贴到此处,因此我需要更强大的功能.
我有content_main布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>Run Code Online (Sandbox Code Playgroud)
和activity_main
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"
android:id="@+id/content"
app:foo="@{1}">
</include>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
</layout>Run Code Online (Sandbox Code Playgroud)
现在在Java中我有
ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)
现在我想访问content_main中的textview作为
binding.content.textView
Run Code Online (Sandbox Code Playgroud)
我尝试在布局标记中使用content_main但它不起作用.我也按照这个链接但它没有用
我怎样才能做到这一点?
我有一个数据绑定布局,其中包含一个框架布局,其中包含其他布局:
<FrameLayout
android:id="@+id/global_actions_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.075"
android:background="@color/colorToolBar">
<include
android:id="@+id/included"
layout="@layout/global_actions">
</include>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
布局中有以下格式的图像按钮:
<ImageButton
android:id="@+id/settingsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_settings_black_36dp"
android:layout_gravity="end"
android:background="@color/colorToolBar"
android:layout_margin="4dp"
android:layout_marginLeft="20dp"
android:onClick="@{listener::onClickState}"
android:alpha="0.4"/>
Run Code Online (Sandbox Code Playgroud)
我在它们周围添加了标签,并添加了活动名称的数据
<data>
<import type="android.view.View"/>
<variable name="listener" type="MyActivity"/>
</data>
Run Code Online (Sandbox Code Playgroud)
在MyActivity中,我调用了一个函数来监听单击图像按钮:
public void onClickState(View view){
int id = view.getId();
}
Run Code Online (Sandbox Code Playgroud)
但是由于某种原因,当我点击时我也没有达到这个功能
android:onClick="@{listener.onClickState}"
Run Code Online (Sandbox Code Playgroud)
但没有任何帮助..任何想法?谢谢 !!