GithubHelp home page GithubHelp logo

attributesdispatcher's Introduction

AttributesDispatcher

It is a lightweight library which helps to create a custom view.

The library is 100% reflection-free.

Usage

AttributesDispatcher introduces a few annotations:

NOTE: Annotated methods and fields must not be private.

Annotation Required Description
@CustomView โœ“ Register Custom View. Annotation @CustomView require package name of application
@Attribute Annotate a method which performs the action that requires one or more attributes
@BooleanAttr Annotate fields or parameters with @BooleanAttr and a custom boolean format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default false.
@ColorAttr Annotate fields or parameters with @ColorAttr and a custom color format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default 0.
@DimenAttr Annotate fields or parameters with @DimenAttr and a custom dimension format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default 0f.
@FloatAttr Annotate fields or parameters with @FloatAttr and a custom float format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default 0f.
@IntAttr Annotate fields or parameters with @IntAttr and a custom integer format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default -1.
@ReferenceAttr Annotate fields or parameters with @ReferenceAttr and a custom reference format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default -1.
@StringAttr Annotate fields or parameters with @StringAttr and a custom string format attribute for Attribute to find and automatically cast the corresponding attribute in your view. By default ""

Example

1. Create custom declare-styleable

<resources>
    <declare-styleable name="MyCustomView">
        <attr name="custom_text" format="string" />
        <attr name="custom_color" format="color" />
    </declare-styleable>
</resources>

2. In your layout

    <com.ebr163.android.attributesdispatcher.view.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:custom_text="Text1"
        app:custom_color="@android:color/holo_blue_light" />

3. Attach annotations

@CustomView("com.ebr163.android.attributesdispatcher")
public class MyCustomView extends EditText {

    @StringAttr("custom_text")
    protected String text;
    
    @ColorAttr(value = "custom_color", defaultValue = Color.GREEN)
    public int color;

    public MyCustomView(Context context) {
        super(context);
    }
    
    @Attribute
    protected void setCustomAttr(@ColorAttr("custom_color") int color) {
        this.setTextColor(color);
    }
}

4. Delegate to generated class

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        MyCustomViewAttribute.init(this, attrs);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        MyCustomViewAttribute.init(this, attrs);
    }

Note

  • The name of the custom view and the name of the declare-styleable should be the same
  • The parameters of the method with @Attribute must be annotated with

Installation

To add it to your project, include the following in your build.gradle file:

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

And on your app module build.gradle:

  compile 'com.github.pavel163.AttributesDispatcher:library:1.1.0'
  annotationProcessor 'com.github.pavel163.AttributesDispatcher:compiler:1.1.0'

Licence

Copyright 2016 Ergashev Bakht.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

attributesdispatcher's People

Contributors

pavel163 avatar

Watchers

James Cloos avatar suifengpiaobo avatar

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.