question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewPager2's FragmentStateAdapter binding mismatches inherited generic `OnBindViewHolder` method

See original GitHub issue

Xamarin.Android Version (eg: 6.0):

Xamarin.Android 10.1.4.0 (d16-4 e44d1ae)

Operating System & Version (eg: Mac OSX 10.11):

Windows 10 1909 (IDE: Visual Studio 2019 16.4.5)

Support Libraries Version (eg: 23.3.0):

Xamarin.AndroidX.ViewPager2 1.0.0

Describe your Issue:

The class FragmentStateAdapter which is used for the new ViewPager2 seems to have a Java binding issue regarding inheritance.

In Java, the FragmentStateAdapter class inherits the RecyclerView.Adapter class, which has the abstract onBindViewHolder method. This abstract method is implemented and sealed (marked final) by the FragmentStateAdapter class to get a chunk of its functionality working. In Java and Kotlin this means any implementation of FragmentStateAdapter does not require implementation of the onBindViewHolder method.

In the Xamarin binding there seems to be a mismatch in inheritance however. The signature of the of the OnBindViewHolder implementation in FragmentStateAdapter seems to mismatch with the OnBindViewHolder in the parent RecyclerView.Adapter class.

On the C# side of things, this causes the responsibility of implementing the abstract OnBindViewHolder in RecyclerView.Adapter to cascade down to the classes that inherit FragmentStateAdapter (since the implementation in FragmentStateAdapter mismatches it is now missing as far as the C# compiler is concerned)

If I try to implement the method in my FragmentStateAdapter subclasses to make sure the C# compiler shows no errors, the Java build error shown below appears instead:

JAVAC0000:  error: name clash: onBindViewHolder(ViewHolder,int) in MyTestActivity_MyPagerAdapter and onBindViewHolder(VH,int) in Adapter have the same erasure, yet neither overrides the other
	public void onBindViewHolder (androidx.recyclerview.widget.RecyclerView.ViewHolder p0, int p1)  where VH is a type-variable: VH extends ViewHolder declared in class Adapter

Possible cause

The issue seems to be caused by the following mismatch:

In Java, the generic signature onBindViewHolder(VH holder, int position) (where VH is a generic defined in the RecyclerView.Adapter class signature) is properly inherited by the method signature onBindViewHolder(FragmentViewHolder holder, int position).

In C#/Xamarin however, it seems the RecyclerView.Adapter doesn’t have this generic aspect bound. Instead using the ViewHolder base type in place of the generic. This likely causes the OnBindViewHolder(FragmentViewHolder holder, int position) method in the FragmentStateAdapter to mismatch with the now non-generic OnBindViewHolder(ViewHolder holder, int position) method in the RecyclerView.Adapter class

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

0reactions
MarLoecommented, Aug 25, 2022

Still present in ver. 1.0.0.16

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate from ViewPager to ViewPager2
Fragment-based adapter classes inheriting from FragmentPagerAdapter or FragmentStatePagerAdapter always accept a single FragmentManager ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found