Coder Social home page Coder Social logo

Comments (4)

Dev-iL avatar Dev-iL commented on May 23, 2024 1

@curioustechizen I think the right course of action consists of including the following class in the codebase, the presence of which should be sufficient for the binding to work correctly.

However, the version of gradle has to be updated (is that a bad thing...?) from 1.0.0 to at least 1.5.0-alpha1 as mentioned at the top of the Data Binding guide. The latest version of the android-gradle plugin at the time of writing is 2.0.0-beta2. I was trying to do this locally but ran into some difficulties. (Possibly because one can't have data-binding inside a apply plugin: 'com.android.library' -type module.)

BTW, not only the gradle plugin, but also the build tools and the target sdk should be updated (to API-23).

As per the example class in this link, a class like this should appear in the project:

package com.your.package;

import android.databinding.BindingAdapter;
import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import com.github.curioustechizen.ago.RelativeTimeTextView;

@BindingMethods({
        @BindingMethod(type = RelativeTimeTextView.class, attribute = "rttv:relative_time_prefix", method = "setPrefix"),
        @BindingMethod(type = RelativeTimeTextView.class, attribute = "rttv:relative_time_suffix", method = "setSuffix"),
})
public class RelativeTimeTextViewBindingAdapter {
    @BindingAdapter("rttv:reference_time")
    public static void setReferenceTime(RelativeTimeTextView view, long time) {
        view.setReferenceTime(time);
    }
}

(Note the custom namespace rttv:, which is an optional modification that makes sense to include). The XML is then:

<com.github.curioustechizen.ago.RelativeTimeTextView
    android:id="@+id/time_n_sender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    rttv:relative_time_prefix='@{"by " + message.senderName + ", "}'
    rttv:reference_time='@{message.timestamp}'
    tools:textColor="@color/gray"
    tools:text="by Mike, 30/02/2016 22:22:10"/>

Note to users:
Adding the above anywhere in a project which uses this library should make it work with data-binding.

from android-ago.

curioustechizen avatar curioustechizen commented on May 23, 2024

The problem as you rightly pointed out is the way the custom attribute and its setter method have been named. The naming is incompatible with data binding.

I am considering deprecating the prefix and suffix attributes in favor of a more general mechanism (like a format specifier with positional parameters). See #15

Hence I don't see myself updating the library to include this functionality. However, if you take the time to implement it (simply adding a setRelativeTimePrefix method that just calls setPrefix) then I would be glad to accept pull requests.

For your question about the date format - see #19

from android-ago.

Dev-iL avatar Dev-iL commented on May 23, 2024

A complete example of RelativeTimeTextView with data-binding can be found here:
Dev-iL@37ab447

from android-ago.

curioustechizen avatar curioustechizen commented on May 23, 2024

@Dev-iL This is very helpful. Thanks!

from android-ago.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.