Data Binding onClick not working

时间:2018-07-19 09:01:17

标签: android data-binding onclick android-databinding

I am getting started for using DataBinding and something is wrong with my onClick.

GameViewModel.java

public void onClickItem(int row, int col){
    Log.d("click","row: "+row+" col: "+col);
}
@BindingAdapter("load_image")
public static void loadImage(ImageView view,int imageId) {
    view.setImageResource(getDrawable(imageId));
}

GameFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //View view=inflater.inflate(R.layout.fragment_game, container, false);
    FragmentGameBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_game, container, false);
    View view = binding.getRoot();
    ButterKnife.bind(this,view);
    binding.setGameViewModel(gameViewModel);
    gameViewModel= ViewModelProviders.of(getActivity()).get(GameViewModel.class);
    gameViewModel.init();
return view;
}

fragment_game.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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"

tools:context=".view.GameFragment">

<data>
     <import type="android.support.v4.app.Fragment"/>
    <import type="android.view.View"/>

    <variable
        name="gameViewModel"
        type="harkor.addus.viewmodel.GameViewModel" />
</data>


<android.support.constraint.ConstraintLayout
   (...)>

    <TextView
        (...)>

    <android.support.constraint.ConstraintLayout
        (...)>

        <ImageView
            android:id="@+id/image_puzzle11"
            android:src="@android:color/holo_green_dark"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="1dp"
            android:layout_marginRight="1dp"
            android:onClick="@{() -> gameViewModel.onClickItem(1,1)}"
            app:load_image="@{0}"
            app:layout_constraintBottom_toTopOf="@+id/image_puzzle21"
            app:layout_constraintDimensionRatio="w,1:1"
            app:layout_constraintEnd_toStartOf="@+id/image_puzzle12"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
(...)

load_image is working, but onClick do nothing... No error in compilation, no crash when button is clicking on device, no result in console...

2 个答案:

答案 0 :(得分:2)

请检查以下代码:

您已写为,以点击图片点击为:

 android:onClick="@{() -> gameViewModel.onClickItem(1,1)}"

尝试编写如下内容,然后再次检查:

android:onClick="@{(v) -> gameViewModel.onClickItem(1,1)}"

根据指导这不是实现我们可以按照MVVM体系结构进行以下工作的体系结构原理的方法: 1.创建一个界面 2.将界面定义为布局文件中的处理程序,如下所示:

<variable
        name="handler"
        type="com.cityguide.interfaces.MustVisitItemListener"></variable>

3。现在,我们使用此处理程序将onclick定义为:

android:onClick="@{(v) ->handler.onGalleryItemClick(v,currentPosition,photo)}"
  1. 在将处理程序与View绑定之前,请使用我们的java类或Activity类对Handler进行处理,
private MustVisitItemListener mItemListener;
     

mItemListener =新的MustVisitItemListener(){};

5。使用绑定对象设置接口处理程序,如下所示:

mbinding.setHandler(mItemListener);

答案 1 :(得分:0)

最简单的方法是设置视图模型,并在布局的View的onClick中调用适当的方法: 您的xml:

<data>
    <variable
        name="myHandlers"
        type="co.package.MyHandlersListener" />
</data>

<com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/my_id"
                        android:layout_width="@dimen/full_width"
                        android:layout_height="wrap_content"
                        android:onClick="@{() -> myHandlers.doSomething()}" />

但是,如果出于任何原因需要从片段或活动中调用方法,则最好的建议是创建一个接口来处理该方法,实现该方法并将其设置为如下所示的布局: 您的xml

/* My Handler Methods */

interface MyHandlersListener {
    fun doSomething()
}

然后在您的Fragment或Activity中创建接口,然后实现它: 您的活动/片段:

private val myHandlersListener: MyHandlersListener = object : MyHandlersListener {
    override fun doSomething() {
        something()
    }
}

然后,在考虑到方法 某物 已在您的活动/片段类中定义和实现的情况下,实现监听器:

myBinding.myHandlers = myHandlersListener

使用数据绑定,您可以将处理程序设置到布局类中(这可以在 onCreate onCreateView 方法中完成,具体取决于您使用的是Activity还是fragment分别):

conda install

通过这种方式,它可以完美运行,并且您遵循Android团队提供的指南: https://developer.android.com/topic/libraries/data-binding/expressions#method_references