片段容器视图松散导航图

问题描述 投票:0回答:1

我有一个功能可以在我的主要活动中创建一个新的底部工作表对话框,其中包含一个片段容器视图来显示导航图。

    private fun showBottomSheet() {
        // Create the BottomSheetDialog
        val bottomSheetDialog = BottomSheetDialog(this)

        // Inflate the layout for the Bottom Sheet (this is the correct layout)
        val bottomSheetView = LayoutInflater.from(this).inflate(R.layout.bottomsheet_add_address, null)

        // Set the content view for the BottomSheetDialog
        bottomSheetDialog.setContentView(bottomSheetView)
        bottomSheetDialog.show()
    }

第一次渲染屏幕时一切正常,但我确实注意到我的片段容器在我折叠并重新打开对话框后丢失了导航图,对话框仍然有它的片段容器,但导航图消失了。

我试图找到活动和图表生命周期的第一个片段的问题,发现该片段确实调用了 onResume,但只调用了一次(我第二次打开底部表单对话框),但之后仍然没有显示任何内容onResume 再也没有被调用过。请注意,即使我折叠底部工作表,片段 onStop 或 onDestroy 也从未被调用

我通常使用底部工作表片段,所以我真的不知道问题是什么,如果有人可以帮助我解决这个问题,那就太好了。

如果你们想仔细看看的话,这是我的 git 项目:项目的存储库

提前谢谢你们。

android kotlin android-fragments navgraph
1个回答
0
投票

感谢您的建议,Mohammadreza Khahani

所以这是我到目前为止建议的一个解决方案,从底部工作表对话框 xml 中删除片段容器视图并将其添加到活动中,并让活动处理片段容器状态而不是对话框。

所以基本上我的可见性消失了,或者在 onCreate 方法中删除片段容器,然后将片段容器视图添加到添加地址对话框中。这当然不是解决这个问题的好方法,但却是一个很好的快速解决方法。

这是我的MainActivity.kt

   class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var fab: FloatingActionButton
    private lateinit var rcv: RecyclerView
    private val viewModel: AddressViewModel by viewModels()
    private lateinit var repo: AddressRepo
    private lateinit var adapter: HouseAdapter
    // create a global variable container the container view
    private lateinit var fragmentContainerView: FragmentContainerView
    private val TAG: String = "Activity Main log"


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

        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        fab = binding.homeFab
        rcv = binding.homeRcv

    // bind and remove the view in onCreate
        fragmentContainerView = binding.homeFragmentContainerView
        (fragmentContainerView.parent as ViewGroup).removeView(fragmentContainerView)

        val db = DatabaseInstance.getDatabase(this@MainActivity)
        repo = AddressRepo(db.addressDao())

        adapter = HouseAdapter(emptyList())
        rcv.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)
        val spacingInPixels = resources.getDimensionPixelSize(R.dimen.item_spacing)
        rcv.addItemDecoration(ItemDecoration(2, spacingInPixels, true))
        rcv.adapter = adapter

        // Load data asynchronously and update the adapter
        lifecycleScope.launch(Dispatchers.IO) {
            val addresses = repo.getAllHouse()
            Log.d(TAG, "onCreate: House Data = " + addresses.toString())
            launch(Dispatchers.Main) {
                adapter.updateData(addresses)
            }
        }

        viewModel.address.observe(this) {address ->
            Log.d(TAG, "onCreate: INPUT = $address")
        }

        fab.setOnClickListener {
            showBottomSheet()
        }
    }

    private fun showBottomSheet() {
        val bottomSheetDialog = BottomSheetDialog(this)

        val bottomSheetView = LayoutInflater.from(this)
            .inflate(R.layout.bottomsheet_add_address, null)

    // add container into bottom sheet dialog if it parent is null
        if (fragmentContainerView.parent != null) {
            (fragmentContainerView.parent as ViewGroup).removeView(fragmentContainerView)
        }
        fragmentContainerView.visibility = View.VISIBLE
        val bottomSheetLinearLayout = bottomSheetView.findViewById<LinearLayout>(R.id.bottom_sheet_placeholder_container)
        bottomSheetLinearLayout.addView(fragmentContainerView)

        bottomSheetDialog.setContentView(bottomSheetView)
        bottomSheetDialog.show()
    }

    }

我的活动布局

    <?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".Screen.MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/home_rcv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="1dp"
        android:paddingTop="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/home_fragmentContainerView"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
        android:background="@color/lightGrey"
        app:navGraph="@navigation/add_address"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/home_fab"
        android:layout_width="wrap_content"
        android:layout_height="56dp"
        android:backgroundTint="@color/blue"
        android:clickable="true"
        android:contentDescription="@string/home_fab_description"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="25dp"
        android:layout_marginBottom="20dp"
        app:srcCompat="@drawable/add"
        app:tint="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我的底部工作表添加地址布局

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <com.google.android.material.bottomsheet.BottomSheetDragHandleView
        android:id="@+id/bottomSheetDragHandleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/bottom_sheet_placeholder_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.example.customviews.StepBar
            android:id="@+id/add_address_bottom_sheet_progressBar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginHorizontal="25dp"
            app:barColor="@color/lightBlue"
            app:canGoUpTo="3"
            app:currentStep="3"
            app:inactiveBarColor="@color/lightGrey"
            app:inactiveMockColor="@color/grey"
            app:mockColor="@color/blue"
            app:stepCount="5" />

    </LinearLayout>

    </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.