安卓工作室 3.6
我想将 ViewPager2 与片段(滑动元素)一起使用
应用程序/build.gradle:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') { transitive = true; }
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-beta01'
implementation 'org.altbeacon:android-beacon-library:2.16.3'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta05'
在我的自定义适配器中,我尝试这样做:
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.myproject.fragment.UserFragment
class UserAdapter(private val userList: ArrayList<String>, fragmentManger: FragmentManager) :
FragmentStateAdapter(fragmentManger) {
override fun getItem(position: Int): Fragment {
return UserFragment.getInstance(userList[position])
}
override fun getItemCount(): Int {
return userList.size
}
}
但我得到两个错误:
Class 'UserAdapter' is not abstract and does not implement abstract base class member @NonNull public abstract fun createFragment(p0: Int): Fragment defined in androidx.viewpager2.adapter.FragmentStateAdapter
None of the following functions can be called with the arguments supplied:
public constructor FragmentStateAdapter(@NonNull p0: Fragment) defined in androidx.viewpager2.adapter.FragmentStateAdapter
public constructor FragmentStateAdapter(@NonNull p0: FragmentActivity) defined in androidx.viewpager2.adapter.FragmentStateAdapter
第二个错误:
'getItem' overrides nothing
我的代码有什么问题?