在具有透明背景的片段上方显示一个片段

时间:2019-06-12 16:09:36

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

我正在使用jetpack导航从片段过渡到详细片段。

我需要帮助来显示详细片段 ,显示该片段。

请参见下面的照片,以查看我要实现的目标:

Eiffel Tower

我还在导航图的“动画”中的Enter Exit(输入退出)中使用了基本的fade_in / fade_out。

这是我得到的结果:

result of presenting view not showing up in the detail view

如何设置过渡,以使详细信息片段显示在当前视图上?

这是我的全部进度:

bottom_nav_menu

<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片段(这是带有埃菲尔铁塔图片和按钮的片段)

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

}

赏金编辑:

我正在寻找在导航图中的片段上方显示片段的视图,如埃菲尔铁塔图片所示。 在iOS中,这类似于演示文稿:在全屏模式下。不希望使用DialogFragment,因为我对常规Fragment中的视图动画设置了更多控制权

1 个答案:

答案 0 :(得分:2)

您试图将模式对话框添加到导航图中作为常规目标。 NavController一次始终只会显示一个目的地,并且不会堆叠在一起。 NavController显示另一个片段时,将从视图中删除一个片段。它并非旨在按照您希望的方式运行。

您需要使用用户界面创建AlertDialogDialogFragment。在上面显示的情况下,AlertDialog就足够了。您可以像这样向AlertDialog添加自定义视图:

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

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

这允许您使用自定义视图创建对话框。它将像人们希望的那样工作:单击按钮关闭对话框,然后按返回按钮。背景将自动变暗。

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

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

对于需要生命周期的更复杂的视图,应使用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>

由于该项目位于布局中的导航主机之后,因此它将在其顶部呈现。请注意,设置了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
}

这可能是您正在寻找的东西,但我不建议您使用它,而应使用AlertDialogDialogFragment

您还可以为对话框使用带有透明背景的新“活动”,并禁用该活动的动画,以便您可以为活动中的视图手动设置动画。活动可以是100%透明的。

相关问题