New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Pager] Re-write Pager on top of LazyRow|Column #678
Merged
Merged
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
0fd2416
Experimenting with changing Pager to use LazyRow
chrisbanes bd38e05
Attempt to migrate some state over
chrisbanes b29d5d0
Add support for center/start/end alignment
chrisbanes 0fea85a
Layout page + offsets calculations are working
chrisbanes 1d0392b
Vaguely working scrolls/flings
chrisbanes d1e5f99
Try generalising the snapping fling
chrisbanes ee4e6b6
Allow snapOffset to be configurable
chrisbanes 131f907
Extract snapping fling behavior
chrisbanes 0dc2aa3
Add broken spring animation
chrisbanes 1ba6721
Working snapping fling
chrisbanes e4c7489
Provide LazyItemScope to Pager content
chrisbanes bfe7a08
First attempt at getting the tests to pass
chrisbanes 39b39ad
Update state after an animation
chrisbanes 42077ea
Re-enable Robolectric Pager tests
chrisbanes b2fddcd
Lots of fixes
chrisbanes cdf4972
Add reverseLayout support
chrisbanes 18514ec
Fixing some more tests
chrisbanes 889f4d1
Remove inifite looping
chrisbanes 245ddce
Use fillParentMax* in samples
chrisbanes 0baf50c
Move away from using contentPadding
chrisbanes fe77a9f
Tidy up paging offsets
chrisbanes 07b6c16
Finally all tests are passing!
chrisbanes 829181e
Fix scrollToPage
chrisbanes 6da48a4
Add a semi-working animateScrollToPage
chrisbanes 2a2a043
Update API files
chrisbanes dec9f87
Remove dragEnabled parameter
chrisbanes 758d97f
Fix nested scrolls getting to Pager
chrisbanes f64263f
Fix device tests
chrisbanes bd7df36
Add scrollOffset back to scrollToPage
chrisbanes f8bd852
Expose key parameter
chrisbanes ee606ab
Expose contentPadding parameter
chrisbanes 2cbe157
Update docs
chrisbanes 01f4892
More tidy-ups
chrisbanes 56a202c
Remove LazyItemScope from PagerScope
chrisbanes 2671e61
Add comment for use of ConsumeFlingNestedScrollConnection
chrisbanes 7ffcf33
Tweak comment
chrisbanes ee2ec3a
Add comment about usage of BoxWithConstraints
chrisbanes 1d22c48
Remove unnecessary call to onScrollFinished()
chrisbanes d9ff81e
Move count parameter
chrisbanes d4ec005
Fix HorizontalPager
chrisbanes 56f2f18
Play around with using contentPadding to provide alignment
chrisbanes 54c2c6d
Fix snapping point
chrisbanes 6a16814
Update horizontal layout assertion
chrisbanes 758ce3c
Update to Compose SNAPSHOT 7655479
chrisbanes 19a97f6
Merge branch 'cb/snapshot-7655479' into cb/pager-lazy-contentpadding
chrisbanes b6dc4ed
Fix scrollToPage test
chrisbanes 7a79250
Fix snapOffset used in page offset calculation
chrisbanes 9474dcb
Fix calculateHorizontalPadding()
chrisbanes e10bf32
Fix swipeToEndAndBack test
chrisbanes 0870e3f
Update Pager samples
chrisbanes bf297b2
Fix derivedStates reading themselves
chrisbanes cf39c70
Run spotless on tests
chrisbanes 1867739
Some vertical test fixes
chrisbanes a6a2618
Remove BoxWithConstraints
chrisbanes da9f75f
Merge branch 'main' into cb/pager-lazy
chrisbanes f5ee829
Update vertical pager samples
chrisbanes fc4ad3e
Most tests are now passing!
chrisbanes 9fd2489
Update API files
chrisbanes 2a13c50
Fix compile error
chrisbanes 91872dc
Merge branch 'main' into cb/pager-lazy
chrisbanes 3764dd8
Add some clarifying comments
chrisbanes 62c2574
Update our scrolling tests to use Dp based velocities
chrisbanes ce8a6c6
Hide SnappingFligBehavior for now
chrisbanes a2bf597
Add rememberPagerFlingConfig back (deprecated)
chrisbanes 6d8e8ea
Add looping sample
chrisbanes 76dfecc
Update docs and guide
chrisbanes d5a6e61
Remove unused code
chrisbanes 1e9ddfb
Add some deprecated functions back in
chrisbanes 4e45979
Add pageOffset back to animateScrollToPage
chrisbanes 355d14e
List more doc differences
chrisbanes 290d83a
Update requireCurrentPage
chrisbanes f7f0052
Merge branch 'main' into cb/pager-lazy
chrisbanes File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
sample/src/main/java/com/google/accompanist/sample/pager/HorizontalPagerLoopingSample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2021 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.accompanist.sample.pager | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.aspectRatio | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import com.google.accompanist.pager.ExperimentalPagerApi | ||
import com.google.accompanist.pager.HorizontalPager | ||
import com.google.accompanist.pager.rememberPagerState | ||
import com.google.accompanist.sample.AccompanistSampleTheme | ||
import com.google.accompanist.sample.R | ||
|
||
class HorizontalPagerLoopingSample : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContent { | ||
AccompanistSampleTheme { | ||
Surface { | ||
Sample() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalPagerApi::class) | ||
@Composable | ||
private fun Sample() { | ||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
title = { Text(stringResource(R.string.horiz_pager_title_looping)) }, | ||
backgroundColor = MaterialTheme.colors.surface, | ||
) | ||
}, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
Column(Modifier.fillMaxSize()) { | ||
// Display 10 items | ||
val pageCount = 10 | ||
|
||
// We start the pager in the middle of the raw number of pages | ||
val startIndex = Int.MAX_VALUE / 2 | ||
val pagerState = rememberPagerState(initialPage = startIndex) | ||
|
||
HorizontalPager( | ||
// Set the raw page count to a really large number | ||
count = Int.MAX_VALUE, | ||
state = pagerState, | ||
// Add 32.dp horizontal padding to 'center' the pages | ||
contentPadding = PaddingValues(horizontal = 32.dp), | ||
// Add some horizontal spacing between items | ||
itemSpacing = 4.dp, | ||
modifier = Modifier | ||
.weight(1f) | ||
.fillMaxWidth() | ||
) { index -> | ||
// We calculate the page from the given index | ||
val page = (index - startIndex).floorMod(pageCount) | ||
PagerSampleItem( | ||
page = page, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.aspectRatio(1f) | ||
) | ||
} | ||
|
||
ActionsRow( | ||
pagerState = pagerState, | ||
modifier = Modifier.align(Alignment.CenterHorizontally) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun Int.floorMod(other: Int): Int = when (other) { | ||
0 -> this | ||
else -> this - floorDiv(other) * other | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be
to start from the first index