如何在android jetpack compose ui中实现水平滚动?
我正在尝试使用jetpack compose实现水平滚动视图,如下所示: 但我找不到任何解决方案来设置单元格的宽度,以获取16dp边距的屏幕宽度,这就是我得到的: 这是我的代码:如何在android jetpack compose ui中实现水平滚动?,android,user-interface,kotlin,android-jetpack,android-jetpack-compose,Android,User Interface,Kotlin,Android Jetpack,Android Jetpack Compose,我正在尝试使用jetpack compose实现水平滚动视图,如下所示: 但我找不到任何解决方案来设置单元格的宽度,以获取16dp边距的屏幕宽度,这就是我得到的: 这是我的代码: private val imageList : Array<Effect<Image>> =arrayOf( imageResource(R.drawable.maldive), imageResource(R.drawable.maldiveone),
private val imageList : Array<Effect<Image>> =arrayOf(
imageResource(R.drawable.maldive),
imageResource(R.drawable.maldiveone),
imageResource(R.drawable.maldivetwo))
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
createList()
}
}
@Composable
fun createList(){
MaterialTheme() {
HorizontalScroller(){
Row(crossAxisSize = LayoutSize.Expand) {
(0..3).forEachIndexed { _, i ->
populateListItem(i)
}
}
}
}
}
@Composable
fun populateListItem(index: Int){
Column(crossAxisSize = LayoutSize.Wrap, modifier = Spacing(16.dp)) {
Card(elevation = 0.dp, shape = RoundedCornerShape(8.dp, 8.dp, 8.dp, 8.dp)) {
val im: Image = +imageList[index.rem(3)]
Container(expanded = true,height = 180.dp)
{
DrawImage(image = im)
}
}
HeightSpacer(height = 16.dp)
Text("Maldive $index",
style = +themeTextStyle { h6 })
Text("Enjoy Our $index Resort!",
style = +themeTextStyle { body2 })
}
}
private val imageList:Array=arrayOf(
图像资源(R.drawable.Maldeve),
图像资源(R.drawable.maldiveone),
图像资源(R.drawable.maldivetwo))
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
设置内容{
createList()
}
}
@组合的
fun createList(){
材料血红素(){
水平滚动条(){
行(crossAxisSize=LayoutSize.Expand){
(0..3).ForAchinedIndexed{{,i->
人口统计项目(一)
}
}
}
}
}
@组合的
趣味流行列表(索引:Int){
列(crossAxisSize=LayoutSize.Wrap,修饰符=间距(16.dp)){
卡片(标高=0.dp,形状=RoundedCornerShape(8.dp,8.dp,8.dp,8.dp)){
valim:Image=+imageList[index.rem(3)]
容器(展开=真,高度=180.dp)
{
DrawImage(图像=im)
}
}
高度垫片(高度=16.dp)
文本(“马尔代夫$index”,
style=+themeTextStyle{h6})
文本(“享受我们的$index度假酒店!”,
style=+themeTextStyle{body2})
}
}
关键是resources.displayMetrics.widthPixels,这将发挥神奇的作用。用下面的函数替换populateListItem函数,它将正常工作
@Composable
fun populateListItem(index: Int) {
val padding = 16
val dm = resources.displayMetrics
val cardWidth = dm.widthPixels/dm.density - 16 * 2 // 2 is multiplied for left and right padding
Column(crossAxisSize = LayoutSize.Wrap, modifier = Spacing(padding.dp)) {
Card(elevation = 0.dp, shape = RoundedCornerShape(8.dp, 8.dp, 8.dp, 8.dp)) {
val im: Image = +imageList[index.rem(3)]
Container(width = cardWidth.dp, height = 180.dp)
{
DrawImage(image = im)
}
}
HeightSpacer(height = 16.dp)
Text("Maldive $index",
style = +themeTextStyle { h6 })
Text("Enjoy Our $index Resort!",
style = +themeTextStyle { body2 })
}
}
从JetPack Compose 1.0.0-beta01使用
BoxWithConstraints
获得屏幕的最大宽度和高度,并根据需要将其传递给@Composable
带注释的函数和修改器
@Composable
fun WithConstraintsComposable() {
BoxWithConstraints {
Text("My minHeight is $minHeight while my maxWidth is $maxWidth)
}
}
文档:是否希望子项的宽度与父项的宽度匹配?。您应该使用google提供的
com.google.伴奏者:伴奏者寻呼机:0.8.1
。它将看起来像水平的viewpager
链接:只是在这里添加我的解决方案
@Composable
趣味水平滚动屏幕(){
//替换为您的项目。。。
val items=(1..10).map{“Item$it”}
//填充整个屏幕的包装器
框(修饰符=修饰符.fillMaxSize()){
//BowWithConstraints将提供下面使用的最大宽度
BoxWithConstraints(修饰符=modifier.fillMaxWidth()){
//LazyRow可水平显示您的项目
懒汉(
modifier=modifier.fillMaxWidth(),
state=rememberLazyListState()
) {
项目索引(项目){索引,项目->
卡片(
修饰语=修饰语
.高度(100.dp)
.width(maxWidth)//这里是诀窍
.填充(16.dp)
) {
Text(item)//卡片内容
}
}
}
}
}
}
此实现可以工作,但您不会有捕捉行为。LazyRow
不支持这种开箱即用的行为(有一个特性请求),但是您可以尝试使用flingBehavior
参数来实现这种行为
或者现在,您可以使用伴奏寻呼机库。