3

Here is the image

It works on component activity, but not on composeview() from fragment that hosted by appcompatactivity()

I’m working with Jetpack Compose and have a LazyColumn where the last item contains clickable fields. When I click clickable item like text or icon button on the screen clickable fields clicked at first time, the column scrolls slightly upwards, causing the item to move out of view. This behavior is problematic as it affects user experience, particularly when interacting with the last item in the list.

Details:

I use LazyColumn to display a list of items. Each item in the list has clickable fields or checkboxes. When the clickable fields clicked at first time, it causes the LazyColumn to scroll slightly up, which is undesirable as it hides the item from view.

Used LazyListState to manage the scroll position and attempted to programmatically scroll to the last item using animateScrollToItem(). Despite these efforts, the issue of the LazyColumn scrolling up slightly when the clickable fields clicked at first time is clicked persists.

here is the code,

@Composable
fun DummyScreen() {

    val state= rememberLazyListState()


    AppTheme {

        Scaffold { contentPadding ->

            LazyColumn(
              state = state,
                modifier = Modifier.padding(contentPadding)
            ) {

                items(3) { index ->
                    Card(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(320.dp)
                            .padding(8.dp)
                            .background(Color.Red)

                    ) {

                        Text(text = "item ${index}",

                            modifier = Modifier.clickable {

                            //TO DO SOME THING
                            }
                        )
                    }
                }

            }
        }
    }

}

2
  • This shouldn't happen. Maybe your UI is recomposed due to the operation you are performing on-click of item and that basically resets scroll position. Try defining lazyListState in LazyColumn to maintain state
    – Megh Lath
    Commented Sep 18 at 11:51
  • I tried your way already but not working, I use LazyColumn to display a list of items. Each item in the list has clickable fields or checkboxes. When the clickable fields clicked at first time, it causes the LazyColumn to scroll slightly up, which is undesirable as it hides the item from view. Commented Sep 18 at 12:28

2 Answers 2

1

First of all don't apply Modifier.padding(innerPadding) directly to the LazyColumn, which can sometimes result in unintended padding behaviour specially when fixed heights is used.

@Composable
fun DummyScreen() {
    val state = rememberLazyListState()
    AppTheme {
        Scaffold { contentPadding ->
            LazyColumn(
                state = state,
                modifier = Modifier.padding(bottom = contentPadding.calculateBottomPadding())
            ) {
                items(3) { index ->
                    Card(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(320.dp)
                            .padding(8.dp)
                            .background(Color.Red)
                    ) {
                        Text(
                            text = "Item $index",
                            modifier = Modifier
                                .fillMaxSize()
                                .clickable(
                                    // Optional: Customize the clickable behavior
                                    interactionSource = remember { MutableInteractionSource() },
                                    indication = null
                                ) {
                                    // TODO: Handle click actions here
                                }
                                .padding(16.dp) // Add padding inside the clickable area if needed
                        )
                    }
                }
            }
        }
    }
}


for bottom padding use 
Modifier.padding(bottom = contentPadding.calculateBottomPadding()), which apply padding to the bottom, rest other padding of top, start , end is your choice to put it here as per your requirement.



Clickable modifier enhancements:
for preventing default ripple effects and focus changes during scrolling use interactionSource and setting indication to null.

0

This issue you're facing with Jetpack compose is likely due to how the system reacts to focusable or clickable items within a LazyColumn.

Use keyboardScrollPadding

Change your clickable text as per below:

Text(
text = "item $index",
modifier = Modifier.clickable(
    indication = null, // Remove ripple effect to avoid focus request
    interactionSource = remember { MutableInteractionSource() }
) {
    
})
1
  • But the ripple effect is what makes the item tappable and gives the user feedback that it has been tapped.
    – Ana
    Commented Oct 16 at 0:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.