Код: Выделить всё
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
BaseTheme {
ExceptionsSpreadSheet(exceptions = generateData())
}
}
}
}
Код: Выделить всё
@OptIn(ExperimentalFoundationApi::class)
@Preview(showBackground = true, widthDp = 960, heightDp = 540)
@Composable
fun ExceptionSpreadSheetHeader(
exception: DropBoxException = DropBoxException(),
state: LazyListState = rememberLazyListState()
) {
LazyRow(
modifier = Modifier.background(color = Color.DarkGray),
state = state
) {
stickyHeader {
ExceptionSpreadSheetCell(text = "id")
}
items(1) {
ExceptionSpreadSheetCell(text = "packageName")
ExceptionSpreadSheetCell(text = "processName")
ExceptionSpreadSheetCell(text = "packageVersionCode")
ExceptionSpreadSheetCell(text = "packageVersionName")
ExceptionSpreadSheetCell(text = "activity")
ExceptionSpreadSheetCell(text = "romVersion")
ExceptionSpreadSheetCell(text = "platformId")
ExceptionSpreadSheetCell(text = "productName")
ExceptionSpreadSheetCell(text = "modelName")
ExceptionSpreadSheetCell(text = "exceptionSummary")
ExceptionSpreadSheetCell(text = "exceptionType")
ExceptionSpreadSheetCell(text = "exceptionDigest")
ExceptionSpreadSheetCell(text = "androidId")
ExceptionSpreadSheetCell(text = "occurTime")
ExceptionSpreadSheetCell(text = "callStack")
}
}
}
@OptIn(ExperimentalFoundationApi::class)
@Preview(showBackground = true, widthDp = 960, heightDp = 540)
@Composable
fun ExceptionSpreadSheetRow(
exception: DropBoxException = DropBoxException(),
state: LazyListState = rememberLazyListState()
) {
LazyRow(state = state) {
stickyHeader {
ExceptionSpreadSheetCell(
text = exception.id.toString(),
modifier = Modifier.background(color = Color.DarkGray)
)
}
items(1) {
ExceptionSpreadSheetCell(text = exception.packageName)
ExceptionSpreadSheetCell(text = exception.processName)
ExceptionSpreadSheetCell(text = exception.packageVersionCode)
ExceptionSpreadSheetCell(text = exception.packageVersionName)
ExceptionSpreadSheetCell(text = exception.activity)
ExceptionSpreadSheetCell(text = exception.romVersion)
ExceptionSpreadSheetCell(text = exception.platformId.toString())
ExceptionSpreadSheetCell(text = exception.productName)
ExceptionSpreadSheetCell(text = exception.modelName)
ExceptionSpreadSheetCell(text = exception.exceptionSummary)
ExceptionSpreadSheetCell(text = exception.exceptionType)
ExceptionSpreadSheetCell(text = exception.exceptionDigest)
ExceptionSpreadSheetCell(text = exception.androidId)
ExceptionSpreadSheetCell(text = exception.occurTime.toString())
ExceptionSpreadSheetCell(text = exception.callStack)
}
}
}
Затем я объединил эти два компонента вместе как ``
Код: Выделить всё
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ExceptionsSpreadSheet(exceptions: List) {
val sharedScrollState = rememberLazyListState()
if (sharedScrollState.isScrollInProgress) {
sharedScrollState.firstVisibleItemIndex
sharedScrollState.firstVisibleItemScrollOffset
}
LazyColumn {
stickyHeader {
ExceptionSpreadSheetHeader(exception = exceptions.first())
ExceptionSpreadSheetHeader(exception = exceptions.first(), state = sharedScrollState)
ExceptionSpreadSheetHeader(state = sharedScrollState)
ExceptionSpreadSheetRow(exception = exceptions.first(), state = sharedScrollState)
ExceptionSpreadSheetRow(state = sharedScrollState)
ExceptionSpreadSheetHeaderTest(state = sharedScrollState)
ExceptionSpreadSheetHeaderTest(exception = exceptions.first(), state = sharedScrollState)
}
items(exceptions) {
ExceptionSpreadSheetRow(exception = it, state = sharedScrollState)
}
}
}
>
- в разделе элементов общее состояние прокрутки сначала не работает, только когда я добавил if (sharedScrollState.isScrollInProgress) часть, строки в items будет синхронизироваться, в противном случае это будет выглядеть как items_not_sync. Мне бы хотелось знать, почему это работает после того, как я добавил часть if, или почему оно не работает раньше
- в StickyHeaders ExceptionsSpreadSheet я добавил много строк для тестирования, и результат выглядит как header_not_sync. Первые 6 строк — это добавленные мной заголовки, включая 2 красные как ExceptionSpreadSheetHeaderTest:
Код: Выделить всё
@OptIn(ExperimentalFoundationApi::class)
@Preview(showBackground = true, widthDp = 960, heightDp = 540)
@Composable
fun ExceptionSpreadSheetHeaderTest(
exception: DropBoxException = DropBoxException(),
state: LazyListState = rememberLazyListState()
) {
LazyRow(
modifier = Modifier.background(color = Color.Red),
state = state
) {
stickyHeader {
ExceptionSpreadSheetCell(text = "id")
}
items(1) {
ExceptionSpreadSheetCell(text = "packageName")
ExceptionSpreadSheetCell(text = "processName")
ExceptionSpreadSheetCell(text = "packageVersionCode")
ExceptionSpreadSheetCell(text = "packageVersionName")
ExceptionSpreadSheetCell(text = "activity")
ExceptionSpreadSheetCell(text = "romVersion")
ExceptionSpreadSheetCell(text = "platformId")
ExceptionSpreadSheetCell(text = "productName")
ExceptionSpreadSheetCell(text = "modelName")
ExceptionSpreadSheetCell(text = "exceptionSummary")
ExceptionSpreadSheetCell(text = "exceptionType")
ExceptionSpreadSheetCell(text = "exceptionDigest")
ExceptionSpreadSheetCell(text = "androidId")
ExceptionSpreadSheetCell(text = exception.occurTime.toString())
ExceptionSpreadSheetCell(text = "callStack")
}
}
}
- в коде второй заголовок () и седьмой заголовок (
Код: Выделить всё
ExceptionSpreadSheetHeader(exception = exceptions.first(), state = sharedScrollState)
) не имеет ничего другого, но между предпоследним ExceptionSpreadSheetCell в разделе элементов кажется, что мне нужно каким-то образом использовать переданное исключение, тогда синхронизация будет работать, что мне кажется очень странным.Код: Выделить всё
ExceptionSpreadSheetHeaderTest(exception = exceptions.first(), state = sharedScrollState)
- 6-й и 7-й заголовок различаются параметрами конструктора, 6-й заголовок использует значение по умолчанию, 7-й заголовок использует внешнее присвоенное значение, но они приводят к разному поведению синхронизации.
ли>
Код: Выделить всё
data class DropBoxException(
var id: Int = 0,
var packageName: String = "",
var processName: String = "",
var packageVersionCode: String = "",
var packageVersionName: String = "",
var activity: String = "",
var romVersion: String = "",
var platformId: Int = -1,
var productName: String = "",
var modelName: String = "",
var exceptionSummary: String = "",
var exceptionType: String = "",
var exceptionDigest: String = "",
var androidId: String = "",
var occurTime: Int = 0,
var callStack: String = "",
)
Код: Выделить всё
fun generateData(): List = (1..300).map {
DropBoxException(
id = it,
packageName = "$it packageName",
processName = "$it processName"
)
}
Подробнее здесь: https://stackoverflow.com/questions/712 ... e-behavior