Resistance with scroll in HorizontalPager in Jetpack Compose - android

I have implemented HorizontalPager with TabRow in my project. My HorizontalPager uses default fling behaviour from PagerDefaults.flingBehavior(state). I have not overridden the fling behavior.
This is how the code looks like:
val pagerState = rememberPagerState()
HorizontalPager(
count = profileState.tabs.size,
state = pagerState
) {..grid item..}
I have to scroll very hard to scroll between the pager items. I want the pager to scroll to next/previous screen on even slight scroll.
After debugging and logging, I found that initialVelocity in performFling method is coming out to be -0.0 (same for left and right fling) all the time for some reason.

You can use (with Accompanist version: accompanist-pager:0.24.8-beta):
HorizontalPager(
count = 3,
state = pagerState,
verticalAlignment = Alignment.Top,
modifier = Modifier.fillMaxSize()
)

In my case, there was a horizontal scrollable Row in the pager item. Make it unscrollable.

Related

LazyColumn cuts off items when using with BottomNavigationView and Toolbar

LazyColumn in Jetpack Compose works fine with Scaffold and its inner padding according to this link. but when I try to use it inside a Fragment, it's items cut off by Toolbar and BottomNavigationView.
should I calculate inner padding manually depending on Toolbar and BottomNavigationViewsize or is there a better solution?
here's the structure. items of lazyColumn are colorful.
as you can see blue item is cut off.
update:
I realized that this behavior is because root of activity is a CoordinatorLayout and my container has appbar_scrolling_view_behavior. I tried this, but it didn't work.
I'm not sure if this would help, but you can try adding the height of the Bottom Navigation view as a bottom content padding to the LazyColumn or to a transparent widget inside another item{…} scope.
as a content padding
LazyColumn(
modifier = Modifier
.fillMaxSize(),
contentPadding = PaddingValues(bottom = <Bottom navigation height>)
) {…}
or as another item scope
LazyColumn(
modifier = Modifier
.fillMaxSize()
) {
items (..) {…}
item {
Box(
modifier = Modifier.height(<Bottom navigation view height>)
)
}
}

Jetpack compose how to make two lazy columns scroll together

I want to have two columns of card flow in the screen with paging data from network. I have tried using two lazy columns with launch effects. (ref: Scroll Two Lazy Scrollers Together), however, the height of card is different so I cannot use the firstVisibleItemScrollOffset and firstVisibleItemIndex directly. If I use lazyGrid, the height of the card cannot be different. How to implement a two-column card flow page like this?
target data flow page
how to make two lazy column scroll together or combine swipe refresh layout with column when using jetpack compose
Just use a LazyVerticalStaggeredGrid.
Something like:
val state = rememberLazyStaggeredGridState()
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Fixed(2),
modifier = Modifier.fillMaxSize(),
state = state,
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp),
content = {
items(count) {
//item content
}
}
)
Have you tried to pass as an argument the same LazyListState to both LazyColumns?

How to scroll Jetpack Compose screen that overflows its contents?

I have an Add Shopping List item Jetpack Compose screen which contains several TextField inputs and an image container at the bottom. The screen overflows at the bottom and is cut off. How can I scroll the screen?
Add Modifier.scrollable(..) to the container that you wish to make scrollable.
Code would be something like this:
val scrollState = rememberScrollState()
...
Box( // or whatever your parent composable is
modifier = Modifier
.scrollable(state = scrollState, orientation = Orientation.Vertical)
) {
...
}
Of course, there are other Modifier methods for making composables scrollable that might fit better for your case.

Is there way to customize LazyRow drag or scroll behavior in android Jetpack Compose?

Below Image explains specification I want to implements
I want to make Lazy Row scroll item if user drag previous (or next) item to some X_SCROLL_THRESHOLD.
I considered to try Pager in Jetpack compose Sample app Jet Caster, but I want to show quite many items (1788 items) so, I'm not sure Pager approach is proper than using LazyRow.
Is there way to customize LazyRow drag or scroll behavior in android Jetpack Compose?
You can try out Pager Layouts. It's an experimental API currently. Hope Google Devs make it part of the general compose library soon.
Use rememberPagerState to manage initialOffscreenLimit(the no. of pages to retain on either side of the current page).
https://google.github.io/accompanist/pager/
Google Github Samples link - https://github.com/google/accompanist/tree/main/sample/src/main/java/com/google/accompanist/sample/pager
You would need to include this dependency in your app gradle
//Pager
implementation "com.google.accompanist:accompanist-pager:0.18.0"
A simple example
// Display 10 items
val pagerState = rememberPagerState(
pageCount = 10,
initialOffscreenLimit = 2
)
HorizontalPager(state = pagerState) { page ->
// Our page content
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
Text(
text = "Item: $page",
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
Text(
text = "Something Else",
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
//Your other composable
}
}
Layout Inspector showcasing the offscreenLimit of 2 items on either side of current page

Compose Column content becomes unreachable if the size of the TextField increases

I have a Composable which has 2 TextFields and a Button in a Column, when the content of the TextFields increases, the Button at the bottom becomes unreachable.
Is there a way I can make the Column scrollable?
To create and automatically remember ScrollState with default parameters use rememberScrollState.
val scroll = rememberScrollState(0)
You can add the verticalScroll modifier to enable scrolling within the Column.
Column(
modifier = Modifier.verticalScroll(scroll)
) {
...
For more read and this

Categories

Resources