This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyContent(listItems : List<Data>){ | |
| val listState = rememberLazyListState() | |
| LazyColumn(state = listState){ | |
| items(listItems){ | |
| // ... | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyContent(listItems : List<Data>){ | |
| val listState = rememberLazyListState() | |
| LazyColumn(state = listState){ | |
| items(listItems){ | |
| // ... | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun LazyListState.OnBottomReached( | |
| loadMore : () -> Unit | |
| ){ | |
| val shouldLoadMore = remember { | |
| derivedStateOf { | |
| val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull() | |
| ?: return@derivedStateOf true | |
| lastVisibleItem.index == layoutInfo.totalItemsCount - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun LazyListState.OnBottomReached( | |
| loadMore : () -> Unit | |
| ){ | |
| // state object which tells us if we should load more | |
| val shouldLoadMore = remember { | |
| derivedStateOf { | |
| // get last visible item | |
| val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun MyContent(listItems : List<Data>){ | |
| val listState = rememberLazyListState() | |
| LazyColumn(state = listState){ | |
| items(listItems){ | |
| // ... | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun LazyListState.OnBottomReached( | |
| loadMore : () -> Unit | |
| ){ | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ToDoItem -> Data Class to hold item data | |
| // ToDoDiff -> Diff Object for ToDoItem Class | |
| class TodoAdapter : AdapterX<TodoItem>(R.layout.item_todo, ToDoDiff){ | |
| override fun ViewHolder.onBind(item: TodoItem) { | |
| val todoNameTextView : TextView = itemView.findViewById(R.id.todo_name) | |
| val isCompleted : CheckBox = itemView.findViewById(R.id.todo_completed) | |
| todoNameTextView.text = item.name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Make the class abstract | |
| abstract class AdapterX <T>( | |
| @LayoutRes private val layoutResId : Int, | |
| diff : DiffUtil.ItemCallback<T> | |
| ) : ListAdapter<T, AdapterX.ViewHolder>(diff){ | |
| // An Extension function for ViewHolder with the List Item as Parameter | |
| // Make it abstract so that it can be overridden | |
| abstract fun ViewHolder.onBind(item : T) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Added Parameter for the Layout Resource to inflate | |
| class AdapterX<T>( | |
| @LayoutRes private val layoutResId : Int, | |
| diff : DiffUtil.ItemCallback<T> | |
| ) : ListAdapter<T, AdapterX.ViewHolder>(diff){ | |
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
| val view = LayoutInflater.from(parent.context).inflate(layoutResId, parent, false) | |
| return ViewHolder(view) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AdapterX<T>(diff L DiffUtil.ItemCallback<T>) : ListAdapter<T, AdapterX.ViewHolder>(diff){ | |
| // A Common Viewholder class for all adapters | |
| class ViewHolder(view : View) : RecyclerView.ViewHolder(view) | |
| } |
NewerOlder