Joe*_*Joe 2 android android-fragments android-navigation android-transitions android-jetpack
我正在使用喷气背包导航从片段过渡到细节片段。
我需要帮助在显示它的片段上呈现细节片段。
请参阅下面的照片以了解我想要实现的目标:
我还在导航图中的动画上使用基本的淡入/淡出作为输入退出。
这是我得到的结果:
如何设置过渡以便细节片段显示在呈现视图上?
这是我所有的进展:
底部导航菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fragment_tab1"
android:title="Tab 1"/>
<item
android:id="@+id/fragment_tab2"
android:title="Tab 2"/>
<item
android:id="@+id/fragment_tab3"
android:title="Tab 3"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
导航图
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph"
app:startDestination="@id/fragment_tab1">
<fragment android:id="@+id/fragment_tab1" android:name="com.example.navgraphfragmentmodal.Tab1" android:label="fragment_tab1"
tools:layout="@layout/fragment_tab1">
<action android:id="@+id/toModal" app:destination="@id/modalFragment"/>
</fragment>
<fragment android:id="@+id/fragment_tab2" android:name="com.example.navgraphfragmentmodal.Tab2" android:label="fragment_tab2"
tools:layout="@layout/fragment_tab2"/>
<fragment android:id="@+id/fragment_tab3" android:name="com.example.navgraphfragmentmodal.Tab3"
android:label="fragment_tab3" tools:layout="@layout/fragment_tab3"/>
<fragment android:id="@+id/modalFragment" android:name="com.example.navgraphfragmentmodal.ModalFragment"
android:label="fragment_modal" tools:layout="@layout/fragment_modal"/>
</navigation>
Run Code Online (Sandbox Code Playgroud)
主活动.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.navigation_host_fragment) as NavHostFragment?
NavigationUI.setupWithNavController(bottom_navigation_view, navHostFragment!!.navController)
supportActionBar?.setDisplayShowHomeEnabled(true)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
supportActionBar?.setDisplayShowHomeEnabled(false)
if (item.itemId === android.R.id.home) {
//Title bar back press triggers onBackPressed()
onBackPressed()
return true
}
return super.onOptionsItemSelected(item)
}
//Both navigation bar back press and title bar back press will trigger this method
override fun onBackPressed() {
supportActionBar?.setDisplayShowHomeEnabled(false)
if (supportFragmentManager.backStackEntryCount > 0) {
supportFragmentManager.popBackStack()
} else {
super.onBackPressed()
}
}
}
Run Code Online (Sandbox Code Playgroud)
Tab1 片段(这是带有埃菲尔铁塔图像和按钮的片段)
class Tab1 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
learnMoreButton.setOnClickListener {
NavHostFragment.findNavController(this).navigate(R.id.modalFragment)
}
}
}
Run Code Online (Sandbox Code Playgroud)
赏金编辑:
我希望在导航图中的片段上显示片段,如埃菲尔铁塔图像所示。在 iOS 中,这类似于演示文稿:全屏显示。我不希望使用 DialogFragment,因为我可以更好地控制普通 Fragment 中的视图动画。
您正在尝试将模态对话框作为正常目的地添加到导航图中。NavController 总是一次只显示一个目的地,并且它们不会堆叠。当 NavController 显示另一个片段时,会从视图中删除一个片段。它并不打算按照您希望的方式运行。
您需要使用您的 UI创建 aAlertDialog或 an DialogFragment。在上面显示的情况下,aAlertDialog已经足够了。您可以将自定义视图添加到AlertDialog这样的:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.your_layout)
.show()
dialog.findViewById<View>(R.id.button).setOnClickListener {
// Do somehting
dialog.dismiss()
}
Run Code Online (Sandbox Code Playgroud)
这允许您使用自定义视图创建对话框。它会像人们希望的那样工作:单击按钮关闭对话框并按下后退按钮。背景会自动变暗。
该对话框将具有默认形状,但您可以按照此答案创建问题中显示的对话框的圆角。
您可以向AlertDialog窗口动画和自定义样式添加动画,请参阅此答案。
对于需要生命周期的更复杂的视图,DialogFragment应该使用(例如在对话框中显示地图时)。从导航 2.1.0-alpha03<dialog>目标开始支持。不过,这仍处于 alpha/beta 阶段。
编辑:
如果你想对动画进行极其精细的控制,你可以像这样在你的布局中添加一个视图:
<FrameLayout>
<!-- Remove the defaultNavHost from this! -->
<fragment android:id="@+id/navHost" android:name="...NavHost" />
<FrameLayout android:id="@+id/dialog" android:background="#44000000" android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout android:id="@+id/dialogContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center">
<!-- Dialog contents -->
</FramyLayout>
</FrameLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
因为该项目在布局中的导航主机之后,所以它会在它上面呈现。请注意,具有该elevation属性设置的按钮或其他元素可能会出现在其顶部。
最后,覆盖您onBackPressed()的活动以恢复导航行为:
override fun onBackPressed() {
val dialog = findViewById<View>(R.id.dialog)
val navController = (supportFragmentManager.findFragmentById(R.id.navHost) as NavHostFragment).navController
if (dialog.visibility == View.VISIBLE) {
hideDialog()
} else if(navController.popBackStack()) {
Log.i("MainActivity", "NavController handled back press")
} else {
super.onBackPressed()
}
}
private fun showDialog() {
findViewById<View>(R.id.dialog).visibility = View.VISIBLE
// Intro animation
}
private fun hideDialog() {
// Outro animation. Call the next line after the animation is done.
findViewById<View>(R.id.dialog).visibility = View.GONE
}
Run Code Online (Sandbox Code Playgroud)
这可能是你在找什么,但是我不推荐它和工作AlertDialog或DialogFragment代替。
您还可以为对话框使用具有透明背景的新活动,并禁用此活动的动画,以便您可以手动为活动中的视图设置动画。活动可以是 100% 透明的。
| 归档时间: |
|
| 查看次数: |
1924 次 |
| 最近记录: |