Galera eu tenho essa função abaixo rodando perfeitamente em minha activity, porém quando eu coloco o mesmo dentro de um fragment ele para de funcionar. Sou novo no mundo android e gostaria de saber como faço pra fazer essa migraçao e o que eu preciso entender.
Aparentemente o mesmo não está funcionado devido ao "supportFragmentManager" o que estou fazendo de errado?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tabLayout: TabLayout = findViewById(R.id.tabs)
val viewpager: ViewPager = findViewById(R.id.viewpager)
val viewPagerAdapter = ViewPagerAdapter(supportFragmentManager)
viewPagerAdapter.addFragment(Fragment1(),"home")
viewPagerAdapter.addFragment(Fragment2(),"chat")
viewPagerAdapter.addFragment(Fragment3(),"setting")
viewpager.adapter = viewPagerAdapter
tabLayout.setupWithViewPager(viewpager)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
return super.onCreateOptionsMenu(menu)
}
internal class ViewPagerAdapter(fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager)
{
private val fragments: ArrayList<Fragment>
private val titles: ArrayList<String>
init {
fragments = ArrayList<Fragment>()
titles = ArrayList<String>()
}
override fun getCount(): Int {
return fragments.size
}
override fun getItem(position: Int): Fragment {
return fragments[position]
}
fun addFragment(fragment: Fragment,title: String)
{
fragments.add(fragment)
titles.add(title)
}
override fun getPageTitle(position: Int): CharSequence? {
return titles[position]
}
}
XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<com.rd.PageIndicatorView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
app:piv_radius="4dp"
app:piv_unselectedColor="@color/black"
app:piv_viewPager="@id/viewpager" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabBackground="@color/black"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="@color/purple_500"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>