【问题标题】:How to add a click and a long click listener on the same view using databinding?如何使用数据绑定在同一视图上添加单击和长单击侦听器?
【发布时间】:2020-06-12 14:02:59
【问题描述】:

我有这个布局文件:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data class="ItemDataBinding">
        <variable
            name="item"
            type="com.example.data.Item" />

        <variable
            name="onItemClickListener"
            type="com.example.OnItemClickListener" />

        <variable
            name="onLongShoppingListClickListener"
            type="com.example.OnLongItemClickListener" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="@{(v) -> onItemClickListener.onItemClick(item)}">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_text_view"
            android:text="@{item.getName()}"/>
    </RelativeLayout>
</layout>

使用单击侦听器,它可以正常工作。我试过了:

android:onClick="@{(v) -> onItemClickListener.onItemClick(item), onLongItemClickListener.onLongItemClick(item)}">

但它不起作用。如何在同一个视图上添加两个监听器?

【问题讨论】:

    标签: android data-binding onclicklistener android-databinding onlongclicklistener


    【解决方案1】:

    您必须创建自己的ClickHander 并在XML 中使用它,如下所示。

        <data>
    
             <variable
                    name="item"
                    type="com.example.data.Item" />
    
            <variable
                name="handler"
                type="embitel.com.databindingexample.helper.ClickHander" />
    
        </data>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onLongClick="@{(v) -> handler.onLongClickOnHeading(v, item)}"
            android:onClick="@{(v)->handler.onItemClicked(v,item)}">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/item_text_view"
                android:text="@{item.getName()}"/>
        </RelativeLayout>
    

    你的ClickHandler 班级

        public class ClickHander {
    
        public void onItemClicked(View v, Item item) {
            Context context = v.getContext();
            // Your code 
        }
    
        // For long click
        public void onLongClickOnHeading(View v, Item item) {
            Context context = v.getContext();
            // Your code 
        }
    }
    

    从您的ActivityFragment 设置绑定

    binding.setHandler(new ClickHander());
    

    【讨论】:

    • 感谢您的回答,但我仍然不明白如何处理这两个事件,单击和长按。你只是改变了处理事件的方式。我希望在同一视图上处理这两个事件。
    • 让我试着回复你。投票赞成。
    • 真的有必要在onItemClicked()中添加View v作为参数吗?
    • 是的,这是必需的。
    猜你喜欢
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多