user4054208
user4054208

Reputation:

FragmentGridPagerAdapter i and i2 values

I have a question about the FragmentGridPagerAdapter. When I override the public Fragment getFragment(int i, int i2) function and log the i and i2 values, i get these strange results:

When i swipe right i2 get's increased, so it goes 0, 1, 2 etc. That makes sense.

But let's say I swiped 6 times to the right and then 1 below I would expect i2 to be 6 and i to be 1. However, i2 get's reset to the first position immediately to the first position. Does anyone know what causes this or what's the logic behind this is?

Upvotes: 0

Views: 421

Answers (2)

Maciej Ciemięga
Maciej Ciemięga

Reputation: 10713

Just to make it more readable: the i should be named row and i2 should be called column.

But to the point:

The GridViewPager is implemented to help you reproduce the Android Wear Home stream behavior. As you know, there are notification cards in the first columns and actions to the right hand side. If you have scrolled to the right to second or third action page for particular notification it would be very confusing to the user if (after swiping up or down) he would land in the middle of action pages for completely different notification. He would see the some action but it would be hard to guess what notification it refers to. So they implement it that way so it resets the current column and returning always to the column with index 0.

This behavior is presented in a video about Building Advanced UIs for Android Wear.
About 2:50 this topic is started (the actual explanation is near 3:25 if you want to skip), but I recommend to watch the entire video.

To change this behavior you simply need to override getCurrentColumnForRow(int row, int currentColumn) method in your FragmentGridPagerAdapter (or just GridViewAdapter).

Here is the documentation for that method:

Returns the column to arrive at when navigating vertically to the specified row.

The default implementation simply returns 0.

Parameters

row               the row in question
currentColumn     the column which is currently centered

Returns

the column to arrive at

To achieve what you want you just need to return currentColumn, or maybe do some check before returning it to see if this position is valid for the new row.

Upvotes: 1

eshayne
eshayne

Reputation: 935

This isn't an issue with the getFragment API, this is the actual behavior of the GridViewPager element. It doesn't really behave like a grid - swiping up or down always starts at the first column.

Presumably this was implemented to enforce Google's design guidelines for a 2D picker, which says "Navigation should be vertical-then-horizontal, not horizontal-then-vertical."

Upvotes: 1

Related Questions