Skip to content

Commit

Permalink
For testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 7, 2024
1 parent c1039b9 commit 2c71a91
Showing 1 changed file with 67 additions and 128 deletions.
195 changes: 67 additions & 128 deletions sample-app/src/main/java/com/example/composerecyclerview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -44,144 +47,80 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val userDataList = List(20) { index ->
UserData(
"User ${index + 1}",
20 + index,
if (index % 2 == 0) "Male" else "Female"
)
}

val otherUsersDataList = List(20) { index ->
UserData(
"User ${index + 21}",
20 + index,
if (index % 2 == 0) "Male" else "Female"
)
val userDataList = remember {
List(20) { index ->
UserData(
"User ${index + 1}",
20 + index,
if (index % 2 == 0) "Male" else "Female"
)
}.toMutableStateList()
}

ComposeRecyclerView(
modifier = Modifier.fillMaxSize(),
itemCount = 1 + userDataList.size + 1 + otherUsersDataList.size,
itemBuilder = { index ->
if (index == 0) {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Text(
"First List Header Composable",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(16.dp)
)
}
return@ComposeRecyclerView
Column {
Button(
onClick = {
val indexToDelete = 0
Log.i("LOL", "index to delete: $indexToDelete")
userDataList.removeAt(
indexToDelete
)
}

val userIndex = index - 1
if (userIndex < userDataList.size) {
CustomUserItem(user = userDataList[userIndex])
return@ComposeRecyclerView
}

if (userIndex == userDataList.size) {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Text(
"Second List Header Composable",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(16.dp)
)
}
return@ComposeRecyclerView
}

val otherUserIndex = index - userDataList.size - 2
if (otherUserIndex < otherUsersDataList.size) {
CustomUserItem(user = otherUsersDataList[otherUserIndex])
return@ComposeRecyclerView
}
},
onItemMove = { fromPosition, toPosition, itemType ->
// Update list when an item is moved
when (itemType) {
ITEM_TYPE_FIRST_HEADER -> {
// Do nothing
}

ITEM_TYPE_FIRST_LIST_ITEM -> {
val fromIndex = fromPosition - 1
val toIndex = toPosition - 1
Collections.swap(userDataList, fromIndex, toIndex)
) {
Text("delete 0")
}
ComposeRecyclerView(
modifier = Modifier.fillMaxSize(),
itemCount = userDataList.size,
itemBuilder = { index ->
Log.d("XXX", "Size: ${userDataList.size}\tIndex:$index")
CustomUserItem(user = userDataList[index])
},
onItemMove = { fromPosition, toPosition, itemType ->
// Update list when an item is moved
when (itemType) {
ITEM_TYPE_FIRST_LIST_ITEM -> {
val fromIndex = fromPosition - 1
val toIndex = toPosition - 1
Collections.swap(userDataList, fromIndex, toIndex)
}
}

ITEM_TYPE_SECOND_HEADER -> {
// Do nothing
},
onDragCompleted = {
// Update list or do API call when an item drag operation is completed
Log.d("MainActivity", "onDragCompleted: $it")
},
itemTypeBuilder = object : ComposeRecyclerViewAdapter.ItemTypeBuilder {
override fun getItemType(position: Int): Int {
// Determine the item type based on the position
// You can customize this logic based on your requirements
return ITEM_TYPE_FIRST_LIST_ITEM
}
},
onScrollEnd = {
// Do API call when the user reaches the end of the list during scrolling
Log.d("MainActivity", "onScrollEnd")
},
itemTouchHelperConfig = {
nonDraggableItemTypes =
setOf(ITEM_TYPE_FIRST_HEADER, ITEM_TYPE_SECOND_HEADER)

// ITEM_TYPE_SECOND_LIST_ITEM
else -> {
val fromIndex = fromPosition - userDataList.size - 2
val toIndex = toPosition - userDataList.size - 2
Collections.swap(otherUsersDataList, fromIndex, toIndex)
/*onMove = { recyclerView, viewHolder, target ->
// Handle item move
}
}
},
onDragCompleted = {
// Update list or do API call when an item drag operation is completed
Log.d("MainActivity", "onDragCompleted: $it")
},
itemTypeBuilder = object : ComposeRecyclerViewAdapter.ItemTypeBuilder {
override fun getItemType(position: Int): Int {
// Determine the item type based on the position
// You can customize this logic based on your requirements
return when {
position == 0 -> ITEM_TYPE_FIRST_HEADER // Header type
position <= userDataList.size -> ITEM_TYPE_FIRST_LIST_ITEM // First list item type
position == userDataList.size + 1 -> ITEM_TYPE_SECOND_HEADER // Header type
else -> ITEM_TYPE_SECOND_LIST_ITEM // Second list item type
onSwiped = { viewHolder, direction ->
// Handle item swipe
}
}
},
onScrollEnd = {
// Do API call when the user reaches the end of the list during scrolling
Log.d("MainActivity", "onScrollEnd")
},
itemTouchHelperConfig = {
nonDraggableItemTypes =
setOf(ITEM_TYPE_FIRST_HEADER, ITEM_TYPE_SECOND_HEADER)

/*onMove = { recyclerView, viewHolder, target ->
// Handle item move
}
onSwiped = { viewHolder, direction ->
// Handle item swipe
}
// Add more customization options as needed*/
},
) { recyclerView ->
recyclerView.addItemDecoration(
DividerItemDecoration(
recyclerView.context,
DividerItemDecoration.VERTICAL
// Add more customization options as needed*/
},
) { recyclerView ->
recyclerView.addItemDecoration(
DividerItemDecoration(
recyclerView.context,
DividerItemDecoration.VERTICAL
)
)
)

// To change layout to grid layout, uncomment the following lines
val gridLayoutManager = GridLayoutManager(this, 2).apply {
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position == 0 || position == userDataList.size + 1) {
2 // To show header title at the center of the screen and span across the entire screen
} else {
1
}
}
}
}
recyclerView.layoutManager = gridLayoutManager
}
}
}
Expand Down

0 comments on commit 2c71a91

Please sign in to comment.