Premise

在实现从ListView到其他分片的屏幕过渡过程中,会出现重叠ListView和分片的错误.

  1. 我 Select 了底部导航视图活动模板.

  2. 已将ListView添加到仪表板

Issue

两个元素覆盖在同一个屏幕上,如下面的屏幕截图所示.

过渡后 After Transition

Source Code

MainActivity.kt

package com.example.guitercodeapp3

//omit 'import'

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    internal fun openDetailFragment(selectedItem: String,lyrics: String){
        val fragment = Detail1Fragment.newInstance(selectedItem ,lyrics)
        supportFragmentManager.beginTransaction().replace(androidx.navigation.fragment.R.id.nav_host_fragment_container, fragment).addToBackStack(null).commit()
    }
}

DashboardFragment.kt

package com.example.guitercodeapp3.ui.dashboard

//omit 'import'

class DashboardFragment : Fragment() {

    private var _binding: FragmentDashboardBinding? = null

    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentDashboardBinding.inflate(inflater, container, false)
        val rootView: View = _binding!!.root

        val listView: ListView = binding.listView
        val items = resources.getStringArray(R.array.list_song)
        val adapter = ArrayAdapter(requireContext(),android.R.layout.simple_list_item_1,items)
        listView.adapter = adapter

        val dashboardViewModel =
            ViewModelProvider(this).get(DashboardViewModel::class.java)

        //val rootView = inflater.inflate(R.layout.fragment_dashboard,container,false)


        listView.setOnItemClickListener { parent, view, position, id ->
            val selectedItem = items[position]
            val lyrics = resources.getStringArray(R.array.list_lyrics)[position]
            val activity = requireActivity() as MainActivity
            activity.openDetailFragment(selectedItem, lyrics)
            val detail1Fragment = Detail1Fragment.newInstance(selectedItem, lyrics)

            requireActivity().supportFragmentManager.beginTransaction().replace(R.id.nav_host_fragment_container,detail1Fragment).addToBackStack(null).commit()
            Toast.makeText(requireContext(), "$selectedItem が選択されました", Toast.LENGTH_SHORT)
                .show()
        }



        val textView: TextView = binding.textDashboard
        dashboardViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        }
        return rootView
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

Detail1Fragment.kt

package com.example.guitercodeapp3.ui.dashboard

//omit 'import'

class Detail1Fragment : Fragment() {

    companion object {
        private const val ARG_SELECTED_ITEM = "selected_item"
        private const val ARG_LYRICS = "lyrics"

        fun newInstance(selectedItem: String, lyrics: String):Detail1Fragment{
            val fragment = Detail1Fragment()
            val args = Bundle()
            args.putString(ARG_SELECTED_ITEM, selectedItem)
            args.putString(ARG_LYRICS,lyrics)
            fragment.arguments = args
            return fragment
        }
    }


    @SuppressLint("MissingInflatedId")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val rootView = inflater.inflate(R.layout.fragment_detail1,container,false)
        val textViewDetail = rootView.findViewById<TextView>(R.id.textViewDetail)


        arguments?.let{
            val selectedItem = it.getString(ARG_SELECTED_ITEM)
            val lyrics = it.getString(ARG_LYRICS)
            textViewDetail.text = "$selectedItem\n\n$lyrics"
        }


        return rootView
    }

}

Fragment_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.dashboard.DashboardFragment">

    <TextView
        android:id="@+id/text_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.6" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:entries="@array/list_song"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <FrameLayout
        android:id="@+id/nav_host_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="androidx.navigation.fragment.NavHostFragment"

        app:navGraph="@navigation/mobile_navigation"
        app:layout_constraintTop_toBottomOf="@+id/text_dashboard"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:defaultNavHost="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment_Detail1.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_host_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.dashboard.Detail1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/textViewDetail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

Strings.xml

<resources>
    <string name="app_name">GuiterCodeApp3</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>
    <string name="list_animal" />

    <string-array name="list_song">
        <item>Imagine</item>
        <item>Shape of you</item>
        <item>Whatever</item>
    </string-array>

    <string-array name="list_lyrics">
        <item>Lyrics of Imagine</item>
        <item>Lyrics of Shape of you</item>
        <item>Lyrics of Whatever</item>
    </string-array>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>

</resources>

Supplementary information

Android Studio 长颈鹿|2022.3.1

What I tried

我确保在XML文件中包含id、名称、条目和导航图形.

我try 更改DashboardFragment.kt的listView.setOnItemClickListener部分中的ID, 但没有取得任何进展.

推荐答案

一段时间以前,我曾面对过这个问题.虽然我不确定是什么原因造成的,但这是因为透明的碎片背景.因此,将背景 colored颜色 添加到所有片段的根视图中.

android:background="#fff"添加到根视图中,如下所示.你可以用任何适合你设计的 colored颜色 .

将第二个片段的根视图的clickable属性设置为真.则不会将该事件传递给第一个片段.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_host_fragment_container"
    android:layout_width="match_parent"
    android:background="#fff"
    android:clickable="true"     
    android:layout_height="match_parent"
    tools:context=".ui.dashboard.Detail1Fragment">

</FrameLayout>

Java相关问答推荐

使用JdkClientHttpRequestFactory通过Spring RestClient和Wiemock读取时达到EOF

给定Java枚举类,通过值查找枚举

如何创建同一类的另一个对象,该对象位于变量中?

SpringBoot+Java 17@Valid未验证POJO

如何让DTO接受空字符串字段,但如果它们不为空,则应用JPA验证?

GSON期间的Java类型擦除

将java.util.Date转换为OffsetDateTime

格式中的特定回录键-值对

如何从Keyloak映射Hibernate实体中的用户

如何在Application.yaml中连接字符串?

使SLF4J在Android中登录到Logcat,在测试中登录到控制台(Gradle依赖问题)

如何在一行中使用Dijkstra中的Java Stream

如何使这两种方法合二为一?

Spring Validator批注不起作用

协同 routine 似乎并不比JVM线程占用更少的资源

无法在Java中获取ElastiCache的AWS CloudWatch指标

如何在Spring Boot中为不同的部署环境管理多个.properties文件?

整数->;双取消框,但双->;int不';t开箱.为什么?

如何使用 Java 替换位于特定标记内的 XML 标记的 CDATA 内的值

在java中使用SevenZip.openArchive方法后无法删除文件