【问题标题】:Android data binding view.onTouchListenerAndroid数据绑定view.onTouchListener
【发布时间】:2017-08-15 12:05:52
【问题描述】:

Android在数据绑定中有

<Button android:onClick="@{handler.someButtonClick()}"/>

在它的Handler class 中,它的监听器会有点像:

public View.OnClickListener someButtonClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        };
    }

我想为Button 实现OnTouchListener,我可以知道按钮何时被按下以及何时被释放

喜欢:

// Check if the button is PRESSED
if (event.getAction() == MotionEvent.ACTION_DOWN){
     //do some thing          
}// Check if the button is RELEASED
else if (event.getAction() == MotionEvent.ACTION_UP) {
    //do some thing                     
}

有没有可能的方法来完成这个任务。

【问题讨论】:

  • 试试android:onClick="@{ (view, event) -&gt; (handler::handleAction(event)) }",未经测试。但是,正如文档所说,您应该只通过数据绑定来绑定简单的侦听器,因为以后可能难以阅读和维护。 developer.android.com/topic/libraries/data-binding/…
  • 能否提供示例代码

标签: android data-binding ontouchlistener


【解决方案1】:

这是一个解决方法,您可以使用它来执行此操作。

@BindingAdapter("touchListener")
public void setTouchListener(View self,boolean value){
    self.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            // Check if the button is PRESSED
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                //do some thing
            }// Check if the button is RELEASED
            else if (event.getAction() == MotionEvent.ACTION_UP) {
                //do some thing
            }
            return false;
        }
    });
}

然后在xml中

<Button  app:touchListener="@{true}"/>

【讨论】:

  • 这个setTouchListener(..) 将在 ViewModel 中,对吧?
  • @Kavita_p ,不,您应该将其设为抽象方法。
  • onTouch 方法应该返回 true
【解决方案2】:

视图模型

public class RecyclerViewModel   {
      public View.OnTouchListener onTouchListener;

      public void setOnTouchListener( View.OnTouchListener onTouchListener) {
          this.onTouchListener = onTouchListener;
      }
}

数据绑定

@BindingAdapter("onTouchListener")
public static void setOnTouchListener(View view, View.OnTouchListener onTouchListener) {
    if (onTouchListener != null)
        view.setOnTouchListener(onTouchListener);
}

在xml中使用

<Button app:onTouchListener="@{viewModel.onTouchListener}"/>

【讨论】:

    【解决方案3】:

    视图模型

    private var tapTime: Long = 0
    
    fun onTouchListener(view:View, event: MotionEvent): Boolean {
       when (event.action) {
          MotionEvent.ACTION_DOWN -> {
              tapTime = Date().time
              // more commands for touch down event
          } 
          MotionEvent.ACTION_UP -> {
             if (Date().time - tapDate < 300) {
                view.performClick()
               // more commands if click event
             } else {
               // any commands if holded toch on screen
             } 
         }
      }
      return true
    }
    

    不需要数据绑定适配器。它已经存在。

    XML

    <Button
       ...
       app:onTouchListener="@{viewModel.onTouchListener}"
       ...
      />
    

    警告 您需要在 ViewModel 中使用正确的签名:

    fun name(view:View, event: MotionEvent): Boolean
    

    以及XML中属性的正确名称:

    app:onTouchListener
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 2016-04-10
      • 2016-03-06
      • 1970-01-01
      • 2017-07-28
      • 2021-05-24
      • 2020-11-12
      • 2016-06-19
      相关资源
      最近更新 更多