简体   繁体   English

如何在 jetpack compose 中使用 lazy column 和 lazyverticalgrid?

[英]How can I use lazy column and lazyverticalgrid in jetpack compose?

I wanted a layout similar to我想要一个类似于

`<Scrollview>
  <Relativelayout>
     <Recyclerview/>(Horizontal)
     <Recyclerview/>(Vertical)
  </Relativelayout>
</Scrollview>`

this is my compose code related to the issue这是我与该问题相关的撰写代码

import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composeappdemo.R
import com.example.composeappdemo.model.Feature
import com.example.composeappdemo.ui.theme.*

@ExperimentalFoundationApi
@Composable
fun HomeScreen(list: List<String>) {
    var features = listOf(
        Feature(
            title = "Summer Times",
            R.drawable.ic__search,
            BlueViolet1,
            BlueViolet1,
            BlueViolet1
        ),
        Feature(
            title = "Winter Vibes",
            R.drawable.ic__search,
            LightGreen1,
            BlueViolet1,
            BlueViolet1
        ),
        Feature(
            title = "Spring Times",
            R.drawable.ic__search,
            LightRed,
            BlueViolet1,
            BlueViolet1
        )
    )

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .background(DeepBlue)
    ) {
        LazyColumn() {
            item {
                GreetingScreen("Android")
            }
            item {
                ChipSection(list)
            }
            item {
                CurrentMeditation()
            }
            item {
                Text(
                    text = "Features",
                    color = Color.White,
                    style = MaterialTheme.typography.h6,
                    modifier = Modifier.padding(15.dp)
                )
            }
            items(features.windowed(2, 2, true)) { list ->
                Row(Modifier.fillMaxWidth()) {
                    list.forEach {
                        FeatureItem(it)
                    }
                }
            }
        }
    }
}

@Composable
fun GreetingScreen(name: String = "Android") {
    Row(
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .fillMaxWidth()
            .padding(15.dp)
    ) {
        Column(
            verticalArrangement = Arrangement.Center
        ) {
            Text(
                text = "Good Morning , $name",
                style = MaterialTheme.typography.h6,
                color = Color.White
            )
            Text(
                text = "We wish you a good day today",
                style = MaterialTheme.typography.body1,
                color = Color.White
            )
        }
        Icon(
            painter = painterResource(id = R.drawable.ic__search),
            contentDescription = "Search",
            tint = Color.White,
            modifier = Modifier.size(24.dp)
        )
    }
}

@Composable
fun ChipSection(
    chips: List<String>
) {
    var selectedChipIndex by remember {
        mutableStateOf(0)
    }
    LazyRow {
        items(chips.size) {
            Box(
                contentAlignment = Alignment.Center,
                modifier = Modifier
                    .padding(start = 15.dp, top = 15.dp, bottom = 15.dp)
                    .clickable {
                        selectedChipIndex = it
                    }
                    .clip(RoundedCornerShape(10.dp))
                    .background(
                        if (selectedChipIndex == it) ButtonBlue
                        else DarkerButtonBlue
                    )
                    .padding(15.dp)
            ) {
                Text(text = chips[it], color = Color.White)
            }
        }
    }
}

@Composable
fun CurrentMeditation(
    color: Color = LightRed
) {
    Box(
        modifier = Modifier
            .padding(15.dp)
            .clip(RoundedCornerShape(15.dp))
            .background(color)
            .fillMaxWidth()
    ) {
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .fillMaxWidth()
                .padding(15.dp)
        ) {
            Column(verticalArrangement = Arrangement.Center) {
                Text(
                    text = "Daily Thought",
                    color = TextWhite,
                    fontSize = 18.sp
                )
                Text(
                    text = "Meditation 3 - 10 mins",
                    color = TextWhite,
                    fontSize = 13.sp
                )
            }
            Icon(
                painter = painterResource(id = R.drawable.ic__play),
                contentDescription = "Play",
                tint = Color.White
            )
        }
    }
}

@ExperimentalFoundationApi
@Composable
fun FeatureSection(features: List<Feature>) {
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        item {
            Text(
                text = "Features",
                color = Color.White,
                style = MaterialTheme.typography.h6,
                modifier = Modifier.padding(15.dp)
            )
        }
        items(features.windowed(2, 2, true)) { list ->
            Row(Modifier.fillMaxWidth()) {
                list.forEach {
                    FeatureItem(feature = it)
                }
            }
        }
        /*LazyVerticalGrid(
            cells = GridCells.Fixed(2),
            contentPadding = PaddingValues(start = 5.dp, top = 5.dp, bottom = 5.dp),
            modifier = Modifier.fillMaxHeight()
        ) {
            items(features.size) {

            }
        }*/
    }
}

@Composable
fun FeatureItem(feature: Feature) {
    Box(
        modifier = Modifier
            .padding(15.dp)
            .fillMaxWidth(.3f)
            .clip(RoundedCornerShape(15.dp))
            .aspectRatio(1f)
            .background(color = feature.lightColor)
    ) {
        Text(
            text = feature.title,
            color = Color.White,
            style = MaterialTheme.typography.h6,
            lineHeight = 24.sp,
            modifier = Modifier.align(Alignment.TopStart)
        )
        Icon(
            painter = painterResource(id = feature.iconId),
            contentDescription = feature.title,
            tint = Color.White,
            modifier = Modifier.align(Alignment.BottomStart)
        )
        Text(
            text = "Start",
            color = TextWhite,
            fontSize = 14.sp,
            fontWeight = FontWeight.Bold,
            modifier = Modifier
                .clickable {

                }
                .align(
                    Alignment.BottomEnd
                )
                .clip(RoundedCornerShape(10.dp))
                .background(ButtonBlue)
                .padding(vertical = 6.dp, horizontal = 15.dp)
        )
    }
}

When I try to achieve it in jetpack compose.当我尝试在 Jetpack Compose 中实现它时。 The list scrolls under it's own layout but doesn't expand making the whole scrolling.该列表在它自己的布局下滚动,但不会展开整个滚动。 Horizontal one is fine but the vertical one tends to have a small height not making it the sameheight as the list itself.水平的很好,但垂直的往往具有较小的高度,无法使其与列表本身的高度相同。

需要这样的视图,以便视图可以展开以及随布局滚动。

Needed a view something like this so that the view can expand as well as scroll with the layout.需要这样的视图,以便视图可以展开以及随布局滚动。

@Composable
fun NewsList() {
LazyColumn {
    items(rows) { item ->
        Text(
            modifier = Modifier
                .height(80.dp),
            text = item
        )
    }
}

something like this you can create fr lazycolum像这样你可以创建 fr lazycolum

for gride you can try this(Not sure for this) is it right or not对于gride你可以试试这个(不确定这个)是否正确

LazyVerticalGrid(
cells = GridCells.Fixed(cellState),
content = {
    
    }
   
}

) )

Don't forget to import this library explicitly不要忘记显式导入这个库

 import androidx.compose.foundation.lazy.items
    @Composable
    fun Conversation(messages: List<Message>) {
        LazyColumn {
            items(messages) { message ->
                MessageCard(message)
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM