English 中文(简体)
Sw work work work
原标题:Swiping don t work properly in Horizontal pager with scrollable tab row in compose

My horizontal pager doesn t work properly when adding some content to any screen. Here, my first screen contains a simple card with four PNG logos and some text headers. But it has a lag and a noticeable delay when scrolling by swiping or when a tab is clicked. Another related problem is that when swiping to the next one, I have to make a full-screen-width swipe, or it will step back to the same position.  The remaining screens have only one centered text for testing, but all tabs still have the following problem: (When swiping from screen to screen, the current screen content indeed appears on the other screen for a moment with not-fetched data before the new content data is successfully fetched.).

目前,该仪器正在以释放方式运行,8号火箭能够使用。 拖延较少,但问题依然存在。

The used implementation("androidx.compose.foundation.pager").

//Edit:
val pagerState = rememberPagerState(initialPage = 0, pageCount = { screens.size })
val pagerPage = rememberSaveable { mutableIntStateOf(0) }
    
LaunchedEffect(key1 = pagerState.currentPage ) {
    pagerPage.intValue = pagerState.currentPage
}

@Composable
fun CustomScrollableTabRow(
    tabs: List<TournamentScreens>,
    selectedTabIndex: Int,
    state: PagerState,
    onTabClick: (Int) -> Unit
) {
    val density = LocalDensity.current
    val tabWidths = remember {
        val tabWidthStateList = mutableStateListOf<Dp>()
        repeat(tabs.size) {
            tabWidthStateList.add(0.dp)
        }
        tabWidthStateList
    }
    ScrollableTabRow(
        selectedTabIndex = selectedTabIndex,
        contentColor = Color.White,
        edgePadding = 0.dp,
        indicator = { tabPositions ->
            SecondaryIndicator(
                modifier = Modifier.customTabIndicatorOffset(
                    currentTabPosition = tabPositions[selectedTabIndex],
                    tabWidth = tabWidths[selectedTabIndex]
                ),
                color = MaterialTheme.colorScheme.primary
            )
        }
    ) {
        tabs.forEachIndexed { tabIndex, tab ->
            Tab(
                selected = selectedTabIndex == tabIndex,
                onClick = { onTabClick(tabIndex) },
                text = {
                    Text(
                        text = tab.title,
                        onTextLayout = { textLayoutResult ->
                            tabWidths[tabIndex] =
                                with(density) { textLayoutResult.size.width.toDp() }
                        }
                    )
                }
            )
        }
    }
    HorizontalPager(
        outOfBoundsPageCount = tabs.size,
        state = state,
    ) {
        tabs[state.currentPage].screen()
    }
}

fun Modifier.customTabIndicatorOffset(
    currentTabPosition: TabPosition,
    tabWidth: Dp
): Modifier = composed(
    inspectorInfo = debugInspectorInfo {
        name = "customTabIndicatorOffset"
        value = currentTabPosition
    }
) {
    val currentTabWidth by animateDpAsState(
        targetValue = tabWidth,
        animationSpec = tween(durationMillis = 250, easing = FastOutSlowInEasing), label = ""
    )
    val indicatorOffset by animateDpAsState(
        targetValue = ((currentTabPosition.left + currentTabPosition.right - tabWidth) / 2),
        animationSpec = tween(durationMillis = 250, easing = FastOutSlowInEasing), label = ""
    )
    fillMaxWidth()
        .wrapContentSize(Alignment.BottomStart)
        .offset(x = indicatorOffset)
        .width(currentTabWidth)
}

I hope I gave a good explanation.

问题回答

你的问题是,你正在使用一个国家。 根据这些文件,下一次屏幕的内容在减去现有屏幕后作了改动。 因此,你最好使用“横向Pager”本身向你们提供的指数。

https://i.stack.imgur.com/322Hh.png” rel=“nofollow noreferer”

https://i.stack.imgur.com/d0M6H.png” rel=“nofollow noreferer”>。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签