android - 在具有透明背景的 fragment 上呈现 fragment

标签 android android-fragments android-jetpack android-transitions android-navigation

我正在使用 Jetpack 导航从 fragment 过渡到细节 fragment 。

我需要帮助在显示它的 fragment 上呈现细节 fragment 。

请看下面的照片以了解我想要实现的目标:

Eiffel Tower

我还在导航图中的动画上使用基本的 fade_in/fade_out 作为 Enter Exit。

这是我得到的结果:

result of presenting view not showing up in the detail view

如何设置过渡,以便细节 fragment 显示在呈现 View 之上?

这是我所有的进步:

底部导航菜单

<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>

导航图

<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>

MainActivity.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()
        }
    }
}

Tab1 fragment (这是带有埃菲尔铁塔图像和按钮的 fragment )

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)
        }
    }

}

赏金编辑:

我希望在导航图中显示一个 fragment over fragment ,如埃菲尔铁塔图像所示。 在 iOS 中,这类似于演示:全屏显示。希望使用 DialogFragment,因为我对普通 fragment 中的 View 动画有更多的控制权.

最佳答案

您正在尝试将模态对话框添加到导航图中作为正常目的地。 NavController 将始终一次只显示一个目的地,并且它们不会堆叠。当 NavController 显示另一个 fragment 时,一个 fragment 将从 View 中移除。它不会按照您希望的方式运行。

您需要创建一个 AlertDialogDialogFragment与您的用户界面。在上面显示的情况下 AlertDialog已经足够了。您可以将自定义 View 添加到 AlertDialog像这样:

val dialog = AlertDialog.Builder(context)
    .setView(R.layout.your_layout)
    .show()

dialog.findViewById<View>(R.id.button).setOnClickListener {
   // Do somehting
   dialog.dismiss()
}

这允许您使用自定义 View 创建对话框。它会像人们希望的那样工作:单击按钮关闭对话框并按下后退按钮。背景会自动变暗。

对话框将具有默认形状,但您可以按照 this 操作回答以创建问题中显示的对话框的圆角。

您可以将动画添加到 AlertDialog使用窗口动画和自定义样式,请参阅 this answer .

对于需要生命周期的更复杂的 View DialogFragment应该使用(例如,在对话框中显示 map 时)。从导航 2.1.0-alpha03 开始​​ <dialog>支持目的地。不过,这仍处于 alpha/beta 阶段。

编辑:

如果你想非常精细地控制动画,你可以像这样向你的布局添加一个 View :

<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>

因为该项目在布局中位于导航主机之后,所以它将呈现在它之上。请注意,带有 elevation 的按钮或其他元素属性集可能出现在它上面。

最后,覆盖你的 onBackPressed()在您的 Activity 中恢复导航行为:

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
}

这可能是您正在寻找的,但我不推荐它并使用 AlertDialogDialogFragment相反。

您还可以为对话框使用具有透明背景的新 Activity 并禁用此 Activity 的动画,以便您可以手动为 Activity 中的 View 设置动画。 Activity 可以 100% 透明。

关于android - 在具有透明背景的 fragment 上呈现 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56566411/

相关文章:

java - NullPointerException 和编码

安卓|主屏幕上的书签图标

android - 用 ViewPager 中的另一个 fragment 替换 fragment

android - Kotlin Android Jetpack 在后台 fragment 之间导航

android - 如何从jetpack compose中的惰性列中删除项目

android - 将 Jetpack-Compose 添加到项目使用 API below 21

Android onLocationChanged location.getTime() 返回错误的时间

Android:从DialogFragment回调 fragment ?

android - 实现 MVP 时,在 Android 中保留 Presenter 的最佳做法是什么?

android - map 的房间类型转换器