数据绑定onClick无法正常工作 [英] Data Binding onClick not working

查看:108
本文介绍了数据绑定onClick无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始使用DataBinding,而onClick出了点问题.

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工作正常,但是onClick却无济于事... 编译没有错误,按钮在设备上单击时没有崩溃,在控制台中没有结果...

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

推荐答案

请检查以下代码:

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

You have written as to call on Click of image as :

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

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

Try to write as below and check again :

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

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

As per the Guidance This is not the way to achieve the Architecture Principles we can work as below as per the MVVM Architecture: 1. Create an Interface 2. Define Interface as handler inside the Layout File as :

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

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

3.Now we are using this handler to define onclick as :

android:onClick="@{(v) ->handler.onGalleryItemClick(v,currentPosition,photo)}"

  1. 在将处理程序与View绑定在一起之前,请使用我们的java类或Activity类对Handler进行处理,

private MustVisitItemListener mItemListener;

mItemListener =新的MustVisitItemListener(){};

mItemListener = new MustVisitItemListener() { };

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

5.Set the Interface handler with bind object as below:

mbinding.setHandler(mItemListener);

这篇关于数据绑定onClick无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆