美文网首页
Jetpack-创建自定义navigation注解处理器

Jetpack-创建自定义navigation注解处理器

作者: Perston | 来源:发表于2020-05-14 18:53 被阅读0次

1.创建libnavannotation
2.创建ActivityDestination和FragmentDestination注解文件
3.创建libnavcompiler
4.主工程app目录内的build.gradle添加依赖
5.Fragment和Activity类上都可以添加注解
6.在assets中生成destination.json
7.编码报红,选GBK解决

1.创建libnavannotation

image.png
apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "8"
targetCompatibility = "8"

2.创建ActivityDestination和FragmentDestination注解文件

ActivityDestination

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
public @interface ActivityDestination {
    String pageUrl();

    boolean needLogin() default false;

    boolean asStarter() default false;
}

FragmentDestination

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
public @interface FragmentDestination {
    String pageUrl();

    boolean needLogin() default false;

    boolean asStarter() default false;
}

3.创建libnavcompiler

image.png
apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':libnavannotation')
    implementation 'com.alibaba:fastjson:1.2.59'
    implementation 'com.google.auto.service:auto-service:1.0-rc6'
    annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'
}

sourceCompatibility = "8"
targetCompatibility = "8"

4.主工程app目录内的build.gradle添加依赖

    implementation project(':libnavannotation')
    //如果你使用kotlin,这里需要使用 kapt project
    annotationProcessor project(":libnavcompiler")

5.Fragment和Activity类上都可以添加注解

pageUrl //地址
needLogin //是否需要登录 默认是false
asStarter //是否启动页 默认是false

为Fragment和Activity注解可生成destination.json

@FragmentDestination(pageUrl = "main/tabs/home", asStarter = true)
public class HomeFragment extends AbsListFragment<Feed, HomeViewModel> {
    private PageListPlayDetector playDetector;
    private String feedType;
    private boolean shouldPause = true;
@ActivityDestination(pageUrl = "main/tabs/publish", needLogin = true)
public class PublishActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityLayoutPublishBinding mBinding;
    private int width, height;
    private String filePath, coverFilePath;
    private boolean isVideo;

6.在assets中生成destination.json

image.png

7.编码报红,选GBK解决

image.png
image.png

相关文章

网友评论

      本文标题:Jetpack-创建自定义navigation注解处理器

      本文链接:https://www.haomeiwen.com/subject/qesxohtx.html