简体   繁体   English

在嵌套片段中打开键盘时,BottomSheet 中出现超级奇怪的故障

[英]Super Weird Glitch in BottomSheet when open Keyboard in nested Fragment

I'm having this super weird glitch in my app.我的应用程序中出现了这个超级奇怪的故障。

小故障 Gif

When I open the keyboard and then I inflate another fragment, the bottomsheet fragment will scroll till reach the top of the recyclerview hiding the top of the bottomsheet view.当我打开键盘然后我给另一个片段充气时, bottomsheet片段将滚动直到到达隐藏bottomsheet视图顶部的recyclerview视图的顶部。

I have a MainFragment with a two FragmentContainerView .我有一个带有两个FragmentContainerView的 MainFragment 。 The first one is a container where I switch between two fragments ( NestedFragment_0 and NestedFragment_1 ) and the second one I use it to inlfate a fragment that has a BottomSheet.第一个是我在两个片段( NestedFragment_0NestedFragment_1 )之间切换的容器,第二个是我用它来膨胀一个具有BottomSheet的片段。 The button switch between two fragments.按钮在两个片段之间切换。

It happens just if the EditText is inside of one of the nested fragment and if there is a RecyclerView inside of the BottomSheet.仅当EditText位于嵌套片段之一内并且 BottomSheet 内有RecyclerView才会发生这种情况。

I've uploaded the project here if someone wants to download it如果有人想下载它,我已经在这里上传了项目

Main Fragment Layout:主要片段布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.MainFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="Switch Nested Fragment"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/bottom_sheet_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainFragment Code:主片段代码:

class MainFragment : Fragment() {

    companion object {
        fun newInstance() = MainFragment()
    }

    private lateinit var binding: MainFragmentBinding

    private var activeSubFragment: Fragment = Nested0Fragment()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = MainFragmentBinding.inflate(inflater, container, false)

        binding.button.setOnClickListener {
            activeSubFragment = if (activeSubFragment is Nested0Fragment) {
                Nested1Fragment()
            } else {
                Nested0Fragment()
            }
            childFragmentManager.beginTransaction()
                .replace(R.id.content_container, activeSubFragment).commit()
        }

        childFragmentManager.beginTransaction().replace(R.id.content_container, activeSubFragment)
            .commit()

        childFragmentManager.beginTransaction()
            .replace(R.id.bottom_sheet_container, BottomSheetFragment.newInstance()).commit()

        return binding.root
    }
}

BottomSheet Layout:底片布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_peekHeight="100dp"
        android:background="@color/teal_200"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        android:fillViewport="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".ui.main.BottomSheetFragment">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:text="BottomSheet"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                app:layoutManager="LinearLayoutManager"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

The problem seems to be related with the EditText and how RecyclerView manages the focus, if I remove the focus of the EditText, the glitch won't occur.问题似乎与 EditText 以及 RecyclerView 如何管理焦点有关,如果我删除 EditText 的焦点,则不会发生故障。 Problem reported in Android issue tracker and it probably be fixed within Fragment 1.4.0-beta01 release.问题在Android 问题跟踪器中报告,它可能会在 Fragment 1.4.0-beta01 版本中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM