所以我有两个片段:DashboardFragment.kt
和HomeFragment.kt
。
DashboardFragment.kt
是带有底部导航和抽屉布局的片段。
这是DashboardFragment.kt
的布局:
fragment_dashboard.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/content"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="@+id/bottomNavigationView"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/buttonColor"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:itemTextColor="@drawable/bottom_navigation_selector"
app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/buyItemButtonColor"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
HomeFragment.kt
是通过DashboardFragment的访问的五个片段之一
BottomNavigationView
这是HomeFragment.kt
的布局:
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="32dp">
<RelativeLayout
android:id="@+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingHorizontal="24dp"
android:layout_marginTop="32dp">
<ImageView
android:id="@+id/navigationDrawerTrigger"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_menu_24" />
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/home_search_bar_background"
android:layout_marginHorizontal="16dp"
android:paddingVertical="7dp"
android:hint="Cari Produk"
android:paddingHorizontal="16dp"
android:layout_toStartOf="@id/notificationTrigger"
android:layout_toEndOf="@id/navigationDrawerTrigger" />
<ImageView
android:id="@+id/notificationTrigger"
android:layout_width="28dp"
android:layout_centerVertical="true"
android:layout_height="28dp"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_notifications_none_24" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/carouselView">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/promotedItemListLayout">
...
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</layout>
我想要的是,当我单击navigationDrawerTrigger
中ID为fragment_home.xml
的Imageview时,它将打开放置在fragment_dashboard.xml
中的NavigationDrawer。
这是HomeFragment.kt
的代码段:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
...
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.navigationDrawerTrigger.setOnClickListener {
drawerLayout?.openDrawer(GravityCompat.START)
}
}
上面drawerLayout
行中的drawerLayout?.openDrawer(GravityCompat.START)
是我试图访问的fragment_dashboard.xml
中的抽屉布局。
这是DashboardFragment.kt
中处理bottomNavigationView的代码片段:
private val onNavigateItemListener = BottomNavigationView.OnNavigationItemSelectedListener{ item ->
when(item.itemId){
R.id.navigationItem1 -> {
val fragment = HomeFragment.newInstance(memberId, username)
addFragment(fragment)
currentSelectedMenu = R.id.navigationItem1
...
return@OnNavigationItemSelectedListener true
}
R.id.navigationItem2 -> {
val fragment = BasketFragment.newInstance(memberId)
addFragment(fragment)
currentSelectedMenu = R.id.navigationItem2
...
return@OnNavigationItemSelectedListener true
}
R.id.navigationItem3 ->{
val fragment = ContactFragment()
addFragment(fragment)
currentSelectedMenu = R.id.navigationItem3
...
return@OnNavigationItemSelectedListener true
}
R.id.navigationItem4 ->{
val fragment = ProfileFragment.newInstance(memberId)
addFragment(fragment)
currentSelectedMenu = R.id.navigationItem4
...
return@OnNavigationItemSelectedListener true
}
}
false
}
private fun addFragment(fragment: Fragment) {
fragmentManager
?.beginTransaction()
?.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
?.replace(R.id.content, fragment, fragment.javaClass.simpleName)
?.commit()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_dashboard, container, false
)
val args = arguments?.let {
DashboardFragmentArgs.fromBundle(
it
)
}
if (args != null) {
memberId = args.memberId
username = args.username
...
binding.navView.setNavigationItemSelectedListener(this)
}
binding.bottomNavigationView.setOnNavigationItemSelectedListener(onNavigateItemListener)
val fragment = HomeFragment.newInstance(memberId, username)
addFragment(fragment)
// Inflate the layout for this fragment
return binding.root
}
如何从DrawerLayout
访问fragment_dashboard.xml
中的HomeFragment.kt
?如果有任何我想指出的细节,请告诉我。