Jetpack compose how to make two lazy columns scroll together - android

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?

Related

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.

Resistance with scroll in HorizontalPager in Jetpack Compose

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.

How can i overlap list item on each other in android jetpack compose?

I want to implement this ui.
How can i overlap post list item on each other in android jetpack compose?
If you're using a Column or a LazyList to display the items, you can use the verticalArrangement parameters with a negative spacedBy space.
LazyColumn(
verticalArrangement = Arrangement.spacedBy((-32).dp)
) {
// Put the items to overlap here
}
spacedBy() doc
Use Box to put one element on top of another.
Also, read official documentation here
You can use Box
sample :
Box(modifier = Modifier.fillMaxSize()) {
Image(modifier = Modifier.fillMaxSize()) {} //Image1
Image(modifier = Modifier.fillMaxSize()) {} //Image2
}
In the above example, Image2 will cover the Box's maxSize. Image1 will be beneath Image2.
Adding to Bagadeshkumar R's answer, you can place Spacer with height that is spacer(modifier = Modifier.height(8.dp)), attribute between image 1 and 2, for image 1 to be partially visible.

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