本文介绍了Cusom事件属性在android绑定应用程序:onMyEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用绑定库连接自定义中激活绑定 code>或 Fragment 并设置一个 ClickHandler 的实例,并为其添加一个变量 xml 为 ClickHandler 。假设你已经知道了,我会继续:

Of course, you'll need to activate the Binding in your Activity or Fragment and set an instance of the ClickHandler to it, and have a variable for it in your xml for the ClickHandler. Assuming that you already know that, I'll continue:

魔术的一部分是使用 app:addOnItemTouchListener 对于 RecyclerView :

One part of the magic is using app:addOnItemTouchListener for the RecyclerView:

<android.support.v7.widget.RecyclerView
    android:id="@+id/rec_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:addOnItemTouchListener="@{clickHandler.touchListener}"/>

另一部分是 ClickHandler.class :

public class ClickHandler {

    public RecyclerView.OnItemTouchListener touchListener;

    public ClickHandler(){
        //initialize the instance of your touchListener in the constructor
        touchListener = new RecyclerView.SimpleOnItemTouchListener(){
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e)     {
                //allow clicks
                return true;
            }

            @Override
                public void onTouchEvent(RecyclerView rv, MotionEvent e) {
                //check if it is working / check if we get the touch event:
                Log.d("onTouchEvent", "RecView: " + rv.getId() + "\nMotionEvent: "+ e.getAction());
            }
        };
    }

    /* last but not least: a method which returns the touchlistener.
       You can rename the method, but don't forget to rename the attribute in the xml, too. */
    public RecyclerView.OnItemTouchListener touchListener(){
        return touchListener;
    }
}

这篇关于Cusom事件属性在android绑定应用程序:onMyEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:52
查看更多