如何通过从任何地方滑动来打开抽屉布局?

How to open drawerlayout by sliding from anywhere?

提问人:KuS- 提问时间:10/9/2023 最后编辑:KuS- 更新时间:10/10/2023 访问量:100

问:

我想知道如何通过在布局视图中的任何位置滑动来从左侧打开抽屉布局。

我试过这个:https://stackoverflow.com/a/71389377/21290837,但它最终没有让我初始化我的抽屉布局。

编辑

XML格式:

<androidx.drawerlayout.widget.DrawerLayout 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/dLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <include
        layout="@layout/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </include>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header_main"
        app:menu="@menu/menu"/>
</androidx.drawerlayout.widget.DrawerLayout>

主活动:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.navView.setNavigationItemSelectedListener(this)

...

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.fish ->
                adapter?.updateAdapter(fillArrays(resources.getStringArray(R.array.fish_name),
                    (resources.getStringArray(R.array.content)),
                    getImageId(R.array.fish_image_array)))
            R.id.location ->
                adapter?.updateAdapter(fillArrays(resources.getStringArray(R.array.location_name),
                    (resources.getStringArray(R.array.location_content)),
                    getImageId(R.array.location_image_array)))
        }

        binding.dLayout.closeDrawer(GravityCompat.START)

        return true
    }

...

}
XML Kotlin Android-Studio 抽屉布局

评论


答: 暂无答案