From 3bcab0cb2e74774720b7bcc02e63ef0b1b12355d Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Tue, 11 May 2021 12:35:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E5=9B=A2=E5=8F=98=E9=87=8F=E9=87=8D?= =?UTF-8?q?=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gyf/csams/ui/MainActivity.kt | 42 +++++++++---------- .../java/com/gyf/csams/ui/model/ViewModel.kt | 26 ++++++------ 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/gyf/csams/ui/MainActivity.kt b/app/src/main/java/com/gyf/csams/ui/MainActivity.kt index 9f87c24..34b23e6 100644 --- a/app/src/main/java/com/gyf/csams/ui/MainActivity.kt +++ b/app/src/main/java/com/gyf/csams/ui/MainActivity.kt @@ -32,8 +32,8 @@ import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController import com.gyf.csams.R +import com.gyf.csams.ui.model.AssociationDto import com.gyf.csams.ui.model.CarouselViewModel -import com.gyf.csams.ui.model.CommunityDto import com.gyf.csams.ui.model.ListViewModel import com.gyf.csams.ui.model.MarqueeViewModel import com.gyf.csams.ui.theme.CSAMSTheme @@ -69,7 +69,7 @@ fun Body() { Main(navController = navController) } composable(MainMenu.List.name) { - CommunityList(navController = navController) + AssociationList(navController = navController) val model:ListViewModel= viewModel() val snackBarMsg:String by model.snackBarMsg.observeAsState("") @@ -180,17 +180,17 @@ fun MainBackground() { * @param navController */ @Composable -fun CommunityList(navController: NavController) { +fun AssociationList(navController: NavController) { MainFrame( background = { - CommunityListBackground() + AssociationListBackground() }, mainMenu = MainMenu.List, nav = navController ) { - RegisterCommunity() - CommunitySearch() - CommunityListBody() + RegisterAssociation() + AssociationSearch() + AssociationListBody() } } @@ -199,7 +199,7 @@ fun CommunityList(navController: NavController) { * */ @Composable -fun RegisterCommunity() { +fun RegisterAssociation() { Row( horizontalArrangement = Arrangement.End, modifier = Modifier @@ -219,7 +219,7 @@ fun RegisterCommunity() { * */ @Composable -fun CommunityListBackground() { +fun AssociationListBackground() { Image( painter = painterResource(id = R.drawable.mb_bg_fb_07), contentDescription = null, @@ -233,24 +233,22 @@ fun CommunityListBackground() { * */ @Composable -fun CommunityListBody(model: ListViewModel = viewModel()) { - val communityList: MutableList? by model.communityDto.observeAsState() +fun AssociationListBody(model: ListViewModel = viewModel()) { + val associationList: MutableList? by model.associationDto.observeAsState() val listState = rememberLazyListState() -// Text(text ="communityList.size=${communityList?.size}") - LazyColumn(state = listState) { - communityList?.chunked(2)?.forEach { + associationList?.chunked(2)?.forEach { item{ Row { Spacer(modifier = Modifier.weight(0.1F)) Box(modifier = Modifier.weight(0.35F)) { - Community(communityDto = it[0]) + Association(associationDto = it[0]) } Spacer(modifier = Modifier.weight(0.05F)) if(it.size==2) { Box(modifier = Modifier.weight(0.35F)) { - Community(communityDto = it[1]) + Association(associationDto = it[1]) } }else{ Box(modifier = Modifier @@ -278,15 +276,15 @@ fun CommunityListBody(model: ListViewModel = viewModel()) { } @Composable -fun Community(communityDto: CommunityDto) { +fun Association(associationDto: AssociationDto) { Card { Image( - painter = painterResource(id = R.drawable.community_list_border), + painter = painterResource(id = R.drawable.association_list_border), contentDescription = null ) Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) { - Text(text = communityDto.name) + Text(text = associationDto.name) } } @@ -298,7 +296,7 @@ fun Community(communityDto: CommunityDto) { * */ @Composable -fun CommunitySearch(model: ListViewModel = viewModel()) { +fun AssociationSearch(model: ListViewModel = viewModel()) { val name: String by model.name.observeAsState("") Card(modifier = Modifier.padding(horizontal = 50.dp, vertical = 10.dp)) { Column { @@ -507,9 +505,7 @@ fun ClubActivitiesImage(model: CarouselViewModel = viewModel()) { @Composable fun DefaultPreview() { CSAMSTheme { -// CommunitySearch(model = ListViewModel()) - - Text(text = "234234") + Text(text = "666") } } diff --git a/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt b/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt index 1d162b0..d8314e9 100644 --- a/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt +++ b/app/src/main/java/com/gyf/csams/ui/model/ViewModel.kt @@ -68,7 +68,7 @@ class CarouselViewModel:ViewModel(){ } -data class CommunityDto(val name:String) +data class AssociationDto(val name:String) class ListViewModel:ViewModel(){ private val _name=MutableLiveData("") @@ -86,11 +86,11 @@ class ListViewModel:ViewModel(){ val snackBarMsg:LiveData = _snackBarMsg //社团列表 - private val _communityList=MutableLiveData>(mutableListOf()) - val communityDto:LiveData> = _communityList + private val _associationList=MutableLiveData>(mutableListOf()) + val associationDto:LiveData> = _associationList init { - loadCommunity() + loadAssociation() } fun onChangeName(name:String){ @@ -110,17 +110,17 @@ class ListViewModel:ViewModel(){ * 加载社团列表 * */ - private fun loadCommunity(){ + private fun loadAssociation(){ viewModelScope.launch { - _communityList.value?.apply { + _associationList.value?.apply { repeat(10 ) { - add(CommunityDto(name = "社团${_communityList.value?.size}")) + add(AssociationDto(name = "社团${_associationList.value?.size}")) } } - Logger.i("初始化社团列表size=${_communityList.value?.size}") + Logger.i("初始化社团列表size=${_associationList.value?.size}") } } @@ -133,20 +133,20 @@ class ListViewModel:ViewModel(){ */ fun addMore(){ viewModelScope.launch { - val c = _communityList.value + val c = _associationList.value if(c!=null){ - val t=mutableListOf() + val t=mutableListOf() t.addAll(c) t.apply { repeat(10 ) { - add(CommunityDto(name = "社团${t.size}")) + add(AssociationDto(name = "社团${t.size}")) } } Logger.i("t.size=${t.size}") - _communityList.postValue(t) - Logger.i("加载更多社团size=${_communityList.value?.size}") + _associationList.postValue(t) + Logger.i("加载更多社团size=${_associationList.value?.size}") _snackBarMsg.value="成功加载更多社团" }