Eu sou iniciante em programação Mobile e quero adicionar em minha aplicação um TabLayout com ViewPager2, estou tendo o seguinte problema:
Eu tenho uma MainActivity e um MainFragment em kotlin:
MainActivity.kt
package com.gugaesmaltes.ellacosmeticos.view
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.gugaesmaltes.ellacosmeticos.R
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.add(R.id.container, MainFragment())
.commitNow()
}
}
}
MainFragment.kt
package com.gugaesmaltes.ellacosmeticos.view
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import com.gugaesmaltes.ellacosmeticos.R
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
//val test = arrayListOf("A","B","C","D","E","F","G")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
TabLayoutMediator(view.tab_layout, view.frag_view_pager){
}
}
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?><com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@android:color/darker_gray" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/frag_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
O problema se encontra no MainFragment.kt, no TabLayoutMediator:
TabLayoutMediator(view.tab_layout, view.frag_view_pager){
}
Supostamente eu deveria conseguir chamar os IDs apenas com view.
*Eu já tentei seguir tutoriais da internet e não tive sucesso.
*Já tentei passar variáveis desta forma:
val tabLayout = view.findViewById(R.id.tab_layout)
val viewPager= view.findViewById(R.id.frag_view_pager)
TabLayoutMediator(tabLayout,viewPager){
}
Desde já agradeço