Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize bindView function through self-defined processor in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve bindView function through custom processor in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve bindView function through custom processor in Android"!

I. brief introduction

At this stage of Android development, annotations are becoming more and more popular, such as ButterKnife,Retrofit,Dragger,EventBus and so on choose to use annotations to configure. According to the processing period, annotations are divided into two types, one is run-time annotations, the other is compile-time annotations, run-time annotations have been criticized by some people because of performance problems. The core of compile-time annotations depends on the implementation of APT (Annotation Processing Tools). The principle is to add annotations to certain code elements (such as types, functions, fields, etc.). At compile time, the compiler examines the subclasses of AbstractProcessor, calls the process function of that type, and then passes all the annotated elements to the process function, so that developers can process them in the compiler, for example, generating new Java classes based on annotations. This is the basic principle of open source libraries such as EventBus,Retrofit,Dragger.

Java API already provides a framework for scanning source code and parsing annotations, and you can inherit the AbstractProcessor class to provide your own parsing annotation logic. Next we will learn how to generate java files in Android Studio through compile-time annotations.

II. Concept

The annotation processor is a tool in javac for scanning and processing annotations at compile time. You can register your own annotation processor for specific comments.

The annotation processor can generate Java code, which forms .java files, but cannot modify existing Java classes (that is, you cannot add methods to existing classes). These generated Java files are compiled by javac along with other ordinary handwritten Java source code.

AbstractProcessor is located under the javax.annotation.processing package, and we need to inherit it when we write processor:

Public class LProcessor extends AbstractProcessor {@ Override public synchronized void init (ProcessingEnvironment processingEnvironment) {super.init (processingEnvironment);} @ Override public boolean process (Set

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report