Xamarin.Android ViewPager2 Requires match_parent

Nathan Sokalski 4,106 Reputation points
2024-10-15T21:53:23.4933333+00:00

I have a Xamarin.Android app in which I am trying to use ViewPager2. The ViewPager2 is defined in my layout as follows:

<androidx.viewpager2.widget.ViewPager2 android:id="@+id/vpStartScore" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="fill" android:background="@color/DimGray"/>

And the ViewHolder(s) are created from the following:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="1" android:rowCount="2" android:paddingTop="2dp" tools:background="@color/Red" tools:ignore="ContentDescription,HardcodedSize,MissingDimension">
	<FrameLayout android:id="@+id/frmBar" android:layout_column="0" android:layout_row="0" android:layout_width="match_parent" android:layout_height="4dp" android:layout_rowWeight="1" android:layout_gravity="center_vertical" android:background="@color/Black"/>
	<ImageView android:id="@+id/imgTick" android:layout_column="0" android:layout_row="0" android:layout_width="2dp" android:layout_height="@dimen/TickSizeLarge" android:layout_rowWeight="1" android:layout_gravity="center" android:layout_margin="0dp" android:padding="0dp" android:background="@drawable/tickdrawablelarge"/>
	<TextView android:id="@+id/txtValue" android:layout_column="0" android:layout_row="1" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/InfoMessageTextViewStyle" android:background="@color/Transparent" android:visibility="visible" android:layout_gravity="center_vertical|fill_horizontal" android:layout_margin="0dp" android:padding="0dp" android:textAlignment="center" android:textColor="@color/Black" tools:text="0"/>
</GridLayout>

However, when trying to run the app, I receive the following Exception:

Java.Lang.IllegalStateException: 'Pages must fill the whole ViewPager2 (use match_parent)'

As you can see, the layout_width & layout_height of both my ViewPager2 & root GridLayout are match_parent. I don't have a lot of experience with ViewPager2 (or ViewPager), but I have used RecyclerView many times in the past. What is the problem? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,355 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,872 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,949 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Graham McKechnie 416 Reputation points
    2024-10-16T21:44:29.47+00:00

    There is a ViewPager2 example on my Github. https://github.com/gmck/NavigationGraph8Net8.

    The files you would be interested in are LeaderboardPagerFragment.cs, LeaderboardViewPagerStateAdapter.cs and the three SampleFragments. In Settings, check "Use ViewPager transitions" to see transitions when you swipe between pages. From memory, I think GridLayout was contained in the v7 support libs. You don't want to use something as old as that in modern Android development.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.