14.Compose 之简易朋友圈列表

01. Compose 可组合组件之Row And Column

02. Compose 可组合组件之 属性 modifier

03. Compose 可组合组件之Card 图片

04. Compose 字体

05. Compose State

06. Compose SnackBar

07. Compose List

08. Compose ConstrainLayout

09. Compose Button

10. Compose CheckBox

11. Compose 对于复杂界面的尝试

12. Compose 之原生xml布局加入Compose代码

13. Compose 之Compose代码插入xml布局

14. Compose 之简易朋友圈列表

@ExperimentalComposeUiApi
@ExperimentalFoundationApi
class WechatFriendsCircle : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            FriendsCircle()
        }
    }

    @Preview
    @Composable
    fun FriendsCircle() {
        val screenWidth = getScreenWidth()

        var picWidth by remember { mutableStateOf(0) }

        LazyColumn(modifier = Modifier.fillMaxSize(), content = {
            items(count = 10) {
                ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
                    val (headRef, titleRef, desRef, picsRef) = createRefs()
                    Image(
                        modifier = Modifier
                            .size(40.dp)
                            .padding(4.dp)
                            .clip(CircleShape)
                            .border(1.dp, Color.DarkGray, CircleShape)
                            .constrainAs(headRef) {
                                top.linkTo(parent.top, margin = 10.dp)
                                start.linkTo(parent.start, margin = 10.dp)
                            },
                        painter = painterResource(id = R.drawable.vector_empty),
                        contentDescription = null
                    )
                    Text(
                        modifier = Modifier
                            .constrainAs(titleRef) {
                                top.linkTo(headRef.top)
                                start.linkTo(headRef.end, margin = 10.dp)
                                end.linkTo(parent.end, margin = 10.dp)
                                bottom.linkTo(headRef.bottom)
                                width = Dimension.fillToConstraints
                            },

                        text = "这是一个标题 $it",
                        textAlign = TextAlign.Start,
                        fontSize = 20.sp,
                        color = Color.Black,
                        fontWeight = FontWeight.Bold,
                    )

                    Text(
                        modifier = Modifier.constrainAs(desRef) {
                            top.linkTo(headRef.bottom, margin = 10.dp)
                            start.linkTo(headRef.end, margin = 10.dp)
                            end.linkTo(parent.end, margin = 10.dp)
                            width = Dimension.fillToConstraints
                        },
                        text = "王嘉尔,1994年3月28日出生于香港,中国香港流行乐男歌手、音乐人、主持人、设计师、创意总监 。王嘉尔,1994年3月28日出生于香港,中国香港流行乐男歌手、音乐人、主持人、设计师、创意总监 王嘉尔,1994年3月28日出生于香港,中国香港流行乐男歌手、音乐人、主持人、设计师、创意总监 王嘉尔,1994年3月28日出生于香港,中国香港流行乐男歌手、音乐人、主持人、设计师、创意总监  $it"
                    )


                    Column(modifier = Modifier
                        .fillMaxWidth()
                        .onSizeChanged {
                            picWidth = it.width
                        }
                        .constrainAs(picsRef) {
                            top.linkTo(desRef.bottom, margin = 10.dp)
                            start.linkTo(headRef.end, margin = 10.dp)
                            end.linkTo(parent.end, margin = 10.dp)
                            width = Dimension.fillToConstraints
                        }
                    ){
                        Row(modifier = Modifier
                            .fillMaxWidth()) {
                            for (index in 0..2){
                                PicItem(pxToDp((picWidth/3)).dp)
                            }
                        }
                        Spacer(modifier = Modifier.fillMaxWidth().height(4.dp))
                        Row(modifier = Modifier
                            .fillMaxWidth()) {
                            for (index in 0..2){
                                PicItem(pxToDp((picWidth/3)).dp)
                            }
                        }
                        Spacer(modifier = Modifier.fillMaxWidth().height(4.dp))
                        Row(modifier = Modifier
                            .fillMaxWidth()) {
                            for (index in 0..2){
                                PicItem(pxToDp((picWidth/3)).dp)
                            }
                        }
                    }


                    //稍微 有些卡 列表嵌套列表的形式
//                    val numbers = (0..8).toList()
//                    val pxValue = with(LocalDensity.current) { 10.dp.toPx() }
//                    val picWidth = (screenWidth - pxValue * 2) / 3
//
//                    LazyVerticalGrid(
//                        cells = GridCells.Fixed(3),
//                        modifier = Modifier
//                            .fillMaxWidth()
//                            .height(pxToDp(picWidth.toInt() * 3).dp)
//                            .constrainAs(picsRef) {
//                                top.linkTo(desRef.bottom, margin = 10.dp)
//                                start.linkTo(headRef.end, margin = 10.dp)
//                                end.linkTo(parent.end, margin = 10.dp)
//                                width = Dimension.fillToConstraints
//                            }
//
//                    ) {
//                        items(numbers.size) {
//                            Image(
//                                modifier = Modifier
//                                    .height(pxToDp(picWidth.toInt()).dp)
//                                    .padding(horizontal = 2.dp)
//                                    .clip(RoundedCornerShape(corner = CornerSize(4.dp)))
//                                    .border(
//                                        1.dp,
//                                        Color.Gray,
//                                        RoundedCornerShape(corner = CornerSize(4.dp))
//                                    )
//                                    .aspectRatio(1f),
//                                painter = painterResource(id = R.drawable.vector_empty),
//                                contentDescription = null
//                            )
//                        }
//                    }
                }
            }
        })
    }

    @Composable
    fun PicItem(width: Dp){
        Image(
            modifier = Modifier
                .width(width)
                .padding(horizontal = 2.dp)
                .clip(RoundedCornerShape(corner = CornerSize(4.dp)))
                .border(
                    1.dp,
                    Color.Gray,
                    RoundedCornerShape(corner = CornerSize(4.dp))
                )
                .aspectRatio(1f),
            painter = painterResource(id = R.drawable.vector_empty),
            contentDescription = null
        )
    }


    private fun pxToDp(px: Int): Int {
        return (px / Resources.getSystem().displayMetrics.density).toInt()
    }

    private fun getScreenWidth(): Int {
        return Resources.getSystem().displayMetrics?.widthPixels ?: 0
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值