在配置fragment跳转到的时候需要在xml进行编写id、name、跳转目的地等等,单Activity多Fragment,那得写多少,感觉有点费劲,那可不可以不写xml?我们可以通过注解的方式来实现
在NavController(NavController manages app navigation within a {@link NavHost}.)有这么一个方法
@CallSuper
public void setGraph(@NavigationRes int graphResId, @Nullable Bundle startDestinationArgs) {
setGraph(getNavInflater().inflate(graphResId), startDestinationArgs);
}
inflate方法里面就是对xml的解析
Resources res = mContext.getResources();
XmlResourceParser parser = res.getXml(graphResId);
省略N行代码.............
如果把这里不用它自己去解析,我们自己去生成对象(调用setGraph方法),这么不就省略了xml配置了吗。
方案
在编写Fragment的时候,写上url。。。
@FragmentDestination(pageUrl = "main/tabs/home", asStarter = true)
public class HomeFragment extends Fragment {
重点是注解解析器
@AutoService(Processor.class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes({"com.remember.libnavannotation.FragmentDestination", "com.remember.libnavannotation.ActivityDestination"})
public class NavProcessor extends AbstractProcessor {
private Messager messager;
private Filer filer;
private static final String OUTPUT_FILE_NAME = "destination.json";
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
//日志打印,在java环境下不能使用android.util.log.e()
messager = processingEnv.getMessager();
//文件处理工具
filer = processingEnv.getFiler();
}
@Override
public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
//通过处理器环境上下文roundEnv分别获取 项目中标记的FragmentDestination.class 和ActivityDestination.class注解。
//此目的就是为了收集项目中哪些类 被注解标记了
Set extends Element> fragmentElements = roundEnv.getElementsAnnotatedWith(FragmentDestination.class);
Set extends Element> activityElements = roundEnv.getElementsAnnotatedWith(ActivityDestination.class);
if (!fragmentElements.isEmpty() || !activityElements.isEmpty()) {
HashMap destMap = new HashMap<>();
//分别 处理FragmentDestination 和 ActivityDestination 注解类型
//并收集到destMap 这个map中。以此就能记录下所有的页面信息了
handleDestination(fragmentElements, FragmentDestination.class, destMap);
handleDestination(activityElements, ActivityDestination.class, destMap);
//app/src/main/assets
FileOutputStream fos = null;
OutputStreamWriter writer = null;
try {
//filer.createResource()意思是创建源文件
//我们可以指定为class文件输出的地方,
//StandardLocation.CLASS_OUTPUT:java文件生成class文件的位置,/app/build/intermediates/javac/debug/classes/目录下
//StandardLocation.SOURCE_OUTPUT:java文件的位置,一般在/ppjoke/app/build/generated/source/apt/目录下
//StandardLocation.CLASS_PATH 和 StandardLocation.SOURCE_PATH用的不多,指的了这个参数,就要指定生成文件的pkg包名了
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", OUTPUT_FILE_NAME);
String resourcePath = resource.toUri().getPath();
messager.printMessage(Diagnostic.Kind.NOTE, "resourcePath:" + resourcePath);
//由于我们想要把json文件生成在app/src/main/assets/目录下,所以这里可以对字符串做一个截取,
//以此便能准确获取项目在每个电脑上的 /app/src/main/assets/的路径
String appPath = resourcePath.substring(0, resourcePath.indexOf("app") + 4);
String assetsPath = appPath + "src/main/assets/";
File file = new File(assetsPath);
if (!file.exists()) {
file.mkdirs();
}
//此处就是稳健的写入了
File outPutFile = new File(file, OUTPUT_FILE_NAME);
if (outPutFile.exists()) {
outPutFile.delete();
}
outPutFile.createNewFile();
//利用fastjson把收集到的所有的页面信息 转换成JSON格式的。并输出到文件中
String content = JSON.toJSONString(destMap);
fos = new FileOutputStream(outPutFile);
writer = new OutputStreamWriter(fos, "UTF-8");
writer.write(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return true;
}
private void handleDestination(Set extends Element> elements, Class extends Annotation> annotationClaz, HashMap destMap) {
for (Element element : elements) {
//TypeElement是Element的一种。
//如果我们的注解标记在了类名上。所以可以直接强转一下。使用它得到全类名
TypeElement typeElement = (TypeElement) element;
//全类名com.mooc.ppjoke.home
String clazName = typeElement.getQualifiedName().toString();
//页面的id.此处不能重复,使用页面的类名做hascode即可
int id = Math.abs(clazName.hashCode());
//页面的pageUrl相当于隐士跳转意图中的host://schem/path格式
String pageUrl = null;
//是否需要登录
boolean needLogin = false;
//是否作为首页的第一个展示的页面
boolean asStarter = false;
//标记该页面是fragment 还是activity类型的
boolean isFragment = false;
Annotation annotation = element.getAnnotation(annotationClaz);
if (annotation instanceof FragmentDestination) {
FragmentDestination dest = (FragmentDestination) annotation;
pageUrl = dest.pageUrl();
asStarter = dest.asStarter();
needLogin = dest.needLogin();
isFragment = true;
} else if (annotation instanceof ActivityDestination) {
ActivityDestination dest = (ActivityDestination) annotation;
pageUrl = dest.pageUrl();
asStarter = dest.asStarter();
needLogin = dest.needLogin();
isFragment = false;
}
if (destMap.containsKey(pageUrl)) {
messager.printMessage(Diagnostic.Kind.ERROR, "不同的页面不允许使用相同的pageUrl:" + clazName);
} else {
JSONObject object = new JSONObject();
object.put("id", id);
object.put("needLogin", needLogin);
object.put("asStarter", asStarter);
object.put("pageUrl", pageUrl);
object.put("className", clazName);
object.put("isFragment", isFragment);
destMap.put(pageUrl, object);
}
}
}
}
在assert文件夹下就会生成destination.json文件
{
"main/tabs/capture": {
"isFragment": false,
"asStarter": false,
"needLogin": false,
"pageUrl": "main/tabs/capture",
"className": "com.remember.jetpackstudy.ui.publish.CaptureActivity",
"id": 1433086463
},
"main/tabs/dash": {
"isFragment": true,
"asStarter": false,
"needLogin": false,
"pageUrl": "main/tabs/dash",
"className": "com.remember.jetpackstudy.ui.dashboard.DashboardFragment",
"id": 525679635
},
}
我们对它进行处理,生成我们想要的Destination
NavigatorProvider provider = controller.getNavigatorProvider();
NavGraph navGraph = new NavGraph(new NavGraphNavigator(provider));
//FragmentNavigator fragmentNavigator = provider.getNavigator(FragmentNavigator.class);
FixFragmentNavigator fragmentNavigator = new FixFragmentNavigator(activity, activity.getSupportFragmentManager(), containerId);
provider.addNavigator(fragmentNavigator);
ActivityNavigator activityNavigator = provider.getNavigator(ActivityNavigator.class);
HashMap destConfig = AppConfig.getDestConfig();
Iterator iterator = destConfig.values().iterator();
while (iterator.hasNext()) {
Destination node = iterator.next();
if (node.isFragment) {
FragmentNavigator.Destination destination = fragmentNavigator.createDestination();
destination.setId(node.id);
destination.setClassName(node.className);
destination.addDeepLink(node.pageUrl);
navGraph.addDestination(destination);
} else {
ActivityNavigator.Destination destination = activityNavigator.createDestination();
destination.setId(node.id);
destination.setComponentName(new ComponentName(AppGlobals.getApplication().getPackageName(), node.className));
destination.addDeepLink(node.pageUrl);
navGraph.addDestination(destination);
}
if (node.asStarter) {
navGraph.setStartDestination(node.id);
}
}
controller.setGraph(navGraph);
}
思路:注解、注解解析器、生成文件、处理、赋值