gpt4 book ai didi

android-studio - 在Android Studio中使用导航框架时,如何知道保留的类名或关键字?

转载 作者:行者123 更新时间:2023-12-03 16:52:04 34 4
gpt4 key购买 nike

以下代码来自于 https://github.com/mycwcgr/camera/tree/master/CameraXBasic 的项目

该项目使用了最新的导航框架,我发现有一些保留的类名,如CameraFragmentDirections、GalleryFragmentArgs。

系统没有这些类名的提示信息,这些关键字必须自己记住吗?

代码

 /** Method used to re-draw the camera UI controls, called every time configuration changes */
@SuppressLint("RestrictedApi")
private fun updateCameraUi() {

// Listener for button used to view last photo
controls.findViewById<ImageButton>(R.id.photo_view_button).setOnClickListener {
Navigation.findNavController(requireActivity(), R.id.fragment_container).navigate(
CameraFragmentDirections.actionCameraToGallery(outputDirectory.absolutePath))
}
}


/** Fragment used to present the user with a gallery of photos taken */
class GalleryFragment internal constructor() : Fragment() {

/** AndroidX navigation arguments */
private val args: GalleryFragmentArgs by navArgs()

}

最佳答案

不,如果你知道一个技巧,你不需要自己记住这些东西。

例如,如果您不记得“关键字”Directions ,但你知道你想做一些与 CameraFragment 相关的事情,您可以开始输入例如CameraFragm在 Android Studio 中。然后它会建议 CameraFragmentCameraFragmentDirections为你。这样你就可以找到CameraFragmentDirections即使您不记得关键字 Directions 也很容易.

虽然没有那么多关键字需要担心。使用 Navigation 框架一段时间后,您会记住它们。

如果您好奇,可以在构建后在这里找到生成的类:

./app/build/generated/source/navigation-args/...

例如调试版本后:
./app/build/generated/source/navigation-args/debug/com/android/example/cameraxbasic/fragments/CameraFragmentDirections.java

如果你更好奇,生成这些类的代码在这里:
https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/java/JavaNavWriter.kt

例如,您可以在那里找到此代码:
internal fun Destination.toClassName(): ClassName {
val destName = name ?: throw IllegalStateException("Destination with actions must have name")
return ClassName.get(destName.packageName(), "${destName.simpleName()}Directions")
}

这是决定什么名字的代码 CameraFragmentDirections得到。 (注 "${destName.simpleName()}Directions" 最后。)

关于android-studio - 在Android Studio中使用导航框架时,如何知道保留的类名或关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57983665/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com