i am trying to implement viewpager and tablyout in android studio using kotlin i have tried fragmentpageradapter but it is not longer used
this is the adapter
package com.example.pgm
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
class ViewPagerAdapterS(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
FragmentStateAdapter(fragmentManager, lifecycle) {
fun getItem(position: Int): Fragment {
var fragment: Fragment? = null
when(position)
{
0 -> {fragment = VSFragment()}
1 -> {fragment = IVSFragment()}
}
return fragment!!
}
fun getPAgeTitle(position: Int): CharSequence? {
var title: String? = null
when(position)
{
0 -> {title = "Active Subscription"}
1 -> {title = "Inactive Subscription"}
}
return title!!
}
override fun getItemCount(): Int {
return 2
}
override fun createFragment(position: Int): Fragment {
TODO("Not yet implemented")
}
}
and this is the activity
package com.example.pgm
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.viewpager.widget.ViewPager
class ViewSubscriptionsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_view_subscriptions)
val viewPager = findViewById<ViewPager>(R.id.viewpagers)
val adapter = ViewPagerAdapterS(supportFragmentManager, lifecycle)
viewPager.adapter = adapter
}
}
and this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
tools:context="ViewSubscriptionsActivity"
>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<com.google.android.material.tabs.TabLayout
android:id="@ id/tabss"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@ id/viewpagers"
app:layout_anchor="@id/tabss"
app:layout_anchorGravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
i get the error Type mismatch: inferred type is ViewPagerAdapterS but PagerAdapter? was expected
So, what can i do? thanks in advance
CodePudding user response:
You have mixed up the classes of Viewpager (1) and Viewpager2 these are two totally independent implementations of a View pager.
You don't show your xml of activity_view_subscriptions
but the import import androidx.viewpager.widget.ViewPager
is for the original Viewpager
Your adaptor import androidx.viewpager2.adapter.FragmentStateAdapter
is from Viewpager2
Change your xml to use Viewpager2
class so that your val viewPager = findViewById<ViewPager>(R.id.viewpagers)
is the of the right type.