ViewPager 的 Android 事件侦听器数据绑定

Android Event Listener Data Binding for ViewPager(ViewPager 的 Android 事件侦听器数据绑定)
本文介绍了ViewPager 的 Android 事件侦听器数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Android 绑定功能将 setOnPageChangeListener 的处理程序绑定到 XML 文件中的 ViewPager?

Is it possible to bind a handler for setOnPageChangeListener to a ViewPager in XML file with the Android Binding functionality?

演示展示了 onClick 事件,但我很好奇我可以用它实现多少事件功能.任何有关数据绑定功能的链接也会很棒.谢谢.

The demos show onClick events but I am curious as to how much event functionality I can implement with it. Any links on the capabilities of Data Binding would be great as well. Thanks.

假设示例:

example_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
   <variable name="handlers" type="com.example.Handlers"/>
</data>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:onPageChangeListener="@{handlers.pageChanged}" />
</layout>

Handler.java

package com.example.viewmodels;

import android.view.View;

public class Handlers {
    public void pageChanged(View view){}
}

编译错误是:

错误:(62) 在包 'android' 中找不到属性 'onPageChangeListener' 的资源标识符

Error:(62) No resource identifier found for attribute 'onPageChangeListener' in package 'android'

推荐答案

这是可以做到的.您需要实现自定义绑定适配器,因为 Android 支持库中没有为 View 类预定义的 BindingAdapter 类.

It is possible to do this. You need to implement a custom binding adapter because there is no BindingAdapter classes predefined for View classes from Android support libraries.

关于如何实现自定义适配器,您可以阅读 this.

For how to implement the custom adapter you may read this.

代码应该如下所示,我还没有彻底测试过:

The code should be something like the below, I haven't tested them thoroughly:

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:onPageChangeListener="@{handlers}" />

您的 BindingAapter 代码:

Your BindingAapter code:

@BindingAdapter("onPageChangeListener")
public static void setOnPageChangeListener(ViewPager viewPager, ViewPager.OnPageChangeListener listener) {
    Log.i("setOnPageChangeListener");

    // clear listeners first avoid adding duplicate listener upon calling notify update related code
    viewPager.clearOnPageChangeListeners();
    viewPager.addOnPageChangeListener(listener);
}

附:您传递的处理程序类应该实现 ViewPager.OnPageChangeListener.

P.S. Your handler class being passed should implement ViewPager.OnPageChangeListener.

这篇关于ViewPager 的 Android 事件侦听器数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)