Как объединить макет ящика с viewapger2 и Navhostfragment
Я пытаюсь совместить макет ящика с viewapger2 и Navhostfragment. Когда я щелкнул значок макета ящика, открывается NavigationView (включая мое меню и заголовок). всякий раз, когда я пытаюсь выбрать один элемент из макета ящика, я не могу выбрать, потому что макет ящика автоматически закрывается. Как я могу решить эту проблему.
это моеactivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/my_nav" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
fragment_view_pager.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"
android:background="@drawable/beach4"
android:id="@+id/cons"
tools:context=".view.ViewPagerFragment">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="75dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#00ffffff"
android:layout_marginTop="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/cons"
app:tabBackground="@drawable/tab_pager_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
app:tabPaddingEnd="@dimen/tab_padding_end"
app:tabPaddingStart="@dimen/tab_padding_start"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
NavHeader.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"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#B3F6FF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mustafa"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/firstItem"
android:title="First Item"/>
<item
android:id="@+id/secondItem"
android:title="Second Item"/>
<item
android:id="@+id/thirdItem"
android:title="Third Item"/>
MainActivity.kt
class MainActivity : AppCompatActivity() {
companion object {
var globalList = ArrayList<Float>()
var globalVar3=1
var globalVar = 1
var globalVar2 = 1
lateinit var sharedPreferences: SharedPreferences
lateinit var toggle: ActionBarDrawerToggle
lateinit var navVieww: NavigationView
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sharedPreferences =
this.getSharedPreferences("com.mustafa.horizontalrecycler", Context.MODE_PRIVATE)
toggle = ActionBarDrawerToggle(
this@MainActivity,
drawer_layout,
R.string.Open,
R.string.Close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val navView = findViewById<NavigationView>(R.id.navView)
navVieww = navView
navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.firstItem -> {
Toast.makeText(applicationContext, "First Item Selected", Toast.LENGTH_LONG)
.show()
println("First Item")
}
R.id.secondItem -> {
Toast.makeText(applicationContext, "Second Item Selected", Toast.LENGTH_LONG)
.show()
println("Second Item")
}
R.id.thirdItem -> {
Toast.makeText(applicationContext, "Third Item Selected", Toast.LENGTH_LONG)
.show()
}
}
true
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (toggle.onOptionsItemSelected(item)) {
globalVar3= globalVar3+1
return true
}
return super.onOptionsItemSelected(item)
}
}
viewPagerFragment.kt
class ViewPagerFragment : Fragment() {
private val args by navArgs<ViewPagerFragmentArgs>()
private lateinit var wordViewModel: WordViewModel
var number = 1
private lateinit var tabLayout: TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
println("VieewPagerOncreated")
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_view_pager, container, false)
wordViewModel = ViewModelProvider(this).get(WordViewModel::class.java)
wordViewModel.readAllData.observe(viewLifecycleOwner, Observer {
//println(it.size)
})
number = sharedPreferences.getInt("pageNumber", 1)
println(number)
val tabLayout = view.findViewById<TabLayout>(R.id.tab_layout)
val viewPager = view.findViewById<ViewPager2>(R.id.view_pager)
val fragments1: ArrayList<Fragment> = arrayListOf(
WeatherListFragment(),
Location1Fragment(),
Location2Fragment(),
Location3Fragment()
)
val fragments2: ArrayList<Fragment> = arrayListOf(
LocationAddFragment()
)
//(i in number..x) x sayısı locationfragment sayısından bir az olcak
//suan için istediğimiz gibi oldu
for (i in number..2) {
fragments1.removeLast()
}
//fragments1.add(fragments2.get(0))
val adapter =
ViewPagerAdapter(fragments1, requireActivity().supportFragmentManager, lifecycle)
viewPager.adapter = adapter
if (MainActivity.globalVar > 1) {
viewPager.setCurrentItem(args.currentPage, false)
}
TabLayoutMediator(tabLayout, viewPager)
{ tab, position -> }.attach()
// bu kod ile başlıgın ismini değiştirebiliriz
(activity as AppCompatActivity).supportActionBar?.title = "Ingredient Display"
(activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
(activity as AppCompatActivity).navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.firstItem -> {
println("viewpagerfragment")
Toast.makeText(requireContext(), "First Item Selected", Toast.LENGTH_LONG)
.show()
println("First Item")
}
R.id.secondItem -> {
Toast.makeText(requireContext(), "Second Item Selected", Toast.LENGTH_LONG)
.show()
println("Second Item")
}
R.id.thirdItem -> {
Toast.makeText(requireContext(), "Third Item Selected", Toast.LENGTH_LONG)
.show()
}
}
true
}
return view
}
}