### Data Binding Layout Setup (XML) Source: https://context7.com/nitrico/lastadapter/llms.txt Defines item layouts for LastAdapter using the `` tag and a data variable. The variable name must match the BR constant passed to LastAdapter, typically `BR.item`. This example shows layouts for a header and a person item. ```xml ``` ```xml ``` -------------------------------- ### Basic RecyclerView Adapter Setup with Type Mapping Source: https://context7.com/nitrico/lastadapter/llms.txt This snippet demonstrates the fundamental way to set up a LastAdapter by mapping data classes to their respective layout resources. The library handles view holder creation and data binding automatically based on the item's class type. ```kotlin LastAdapter(listOfItems, BR.item) .map
(R.layout.item_header) .map(R.layout.item_person) .map(R.layout.item_point) .map(R.layout.item_car) .into(recyclerView) ``` ```java new LastAdapter(listOfItems, BR.item) .map(Header.class, R.layout.item_header) .map(Person.class, R.layout.item_person) .map(Point.class, R.layout.item_point) .map(Car.class, R.layout.item_car) .into(recyclerView); ``` -------------------------------- ### Gradle Setup for LastAdapter Source: https://github.com/nitrico/lastadapter/blob/master/README.md This snippet shows how to configure your Android project's Gradle files to include the LastAdapter library. It requires enabling data binding and adding the LastAdapter dependency. For Kotlin projects, `kotlin-kapt` and the data binding compiler might also be necessary. ```gradle android { ... dataBinding.enabled true } dependencies { compile 'com.github.nitrico.lastadapter:lastadapter:2.3.0' // kapt 'com.android.databinding:compiler:GRADLE_PLUGIN_VERSION' // this line only for Kotlin projects } ``` -------------------------------- ### Dynamic Layout Selection with LayoutHandler (Kotlin) Source: https://context7.com/nitrico/lastadapter/llms.txt Demonstrates using a lambda to define layout logic for different item types and positions at runtime. This allows for flexible UI rendering based on data context. ```kotlin LastAdapter(listOfItems, BR.item).layout { item, position -> when (item) { is Header -> if (position == 0) R.layout.item_header_first else R.layout.item_header is Person -> R.layout.item_person is Point -> R.layout.item_point is Car -> R.layout.item_car else -> -1 } }.into(recyclerView) ``` -------------------------------- ### Basic LastAdapter Usage with Data Binding Source: https://github.com/nitrico/lastadapter/blob/master/README.md This demonstrates how to create a RecyclerView adapter using LastAdapter. It involves defining item layouts with `` tags, ensuring a consistent variable name (e.g., 'item') in the `` block, and then mapping item classes to their respective layouts using the adapter builder. ```xml ``` ```java // Java new LastAdapter(listOfItems, BR.item) .map(Header.class, R.layout.item_header) .map(Point.class, R.layout.item_point) .into(recyclerView); ``` ```kotlin // Kotlin LastAdapter(listOfItems, BR.item) .map
(R.layout.item_header) .map(R.layout.item_point) .into(recyclerView) ``` -------------------------------- ### Dynamic Layout Selection with LayoutHandler (Java) Source: https://context7.com/nitrico/lastadapter/llms.txt Shows how to implement the `LayoutHandler` interface in Java to dynamically determine the layout for each item based on its type and position. This provides runtime control over item views. ```java new LastAdapter(items, BR.item, stableIds).handler(new LayoutHandler() { @Override public int getItemLayout(@NotNull Object item, int position) { if (item instanceof Header) { return position == 0 ? R.layout.item_header_first : R.layout.item_header; } else if (item instanceof Point) { return R.layout.item_point; } else if (item instanceof Person) { return R.layout.item_person; } else if (item instanceof Car) { return R.layout.item_car; } return -1; } }).into(list); ``` -------------------------------- ### StableId for Efficient Animations (Kotlin) Source: https://context7.com/nitrico/lastadapter/llms.txt Explains how implementing the `StableId` interface on data classes enables stable IDs, which are crucial for smooth animations and preventing UI flickering during data updates in the RecyclerView. ```kotlin // Kotlin - Data class implementing StableId class Person(val id: Long, val name: String, val surname: String) : StableId { override val stableId = id } // Enable stable IDs in adapter constructor LastAdapter(items, BR.item, stableIds = true) .map(R.layout.item_person) .into(recyclerView) ``` -------------------------------- ### Dynamic Layout Selection with LayoutHandler Source: https://github.com/nitrico/lastadapter/blob/master/README.md This shows how to use the `LayoutHandler` interface to dynamically determine the layout for each item based on its type and position. This is useful for scenarios where different layouts are needed for the same item type or based on specific conditions. ```java // Java sample new LastAdapter(listOfItems, BR.item) .handler(handler) .into(recyclerView); private LayoutHandler handler = new LayoutHandler() { @Override public int getItemLayout(@NotNull Object item, int position) { if (item instanceof Header) { return (position == 0) ? R.layout.item_header_first : R.layout.item_header; } else { return R.layout.item_point; } } }; ``` ```kotlin // Kotlin sample LastAdapter(listOfItems, BR.item).layout { item, position -> when (item) { is Header -> if (position == 0) R.layout.item_header_first else R.layout.item_header else -> R.layout.item_point } }.into(recyclerView) ``` -------------------------------- ### Automatic Updates with ObservableList (Kotlin) Source: https://context7.com/nitrico/lastadapter/llms.txt Demonstrates how `LastAdapter` automatically handles updates when using `ObservableArrayList`. Adding, removing, or changing items in the list triggers UI refresh without manual adapter notifications. ```kotlin val items = ObservableArrayList().apply { add(Header("Section 1")) add(Point(1, 1)) add(Point(1, 2)) add(Header("Section 2")) add(Person(99098, "Miguel", "Moreno")) add(Car(1899393, "911 Carrera")) } LastAdapter(items, BR.item) .map
(R.layout.item_header) .map(R.layout.item_point) .map(R.layout.item_person) .map(R.layout.item_car) .into(recyclerView) // Later: Adding items automatically updates the RecyclerView items.add(Point(2, 1)) // Adapter notifies automatically items.removeAt(0) // Adapter notifies automatically ``` -------------------------------- ### RecyclerView Adapter with Event Callbacks and Lifecycle Methods Source: https://context7.com/nitrico/lastadapter/llms.txt This section illustrates how to define custom behavior for RecyclerView items using the `Type` class in Kotlin. It allows for onCreate, onBind, onRecycle lifecycle callbacks, as well as click and long-click listeners, providing a `Holder` object for interaction. ```kotlin private val typeHeader = Type(R.layout.item_header) .onCreate { holder -> println("Created ${holder.binding.item} at #${holder.adapterPosition}") } .onBind { holder -> println("Bound ${holder.binding.item} at #${holder.adapterPosition}") } .onRecycle { holder -> println("Recycled ${holder.binding.item} at #${holder.adapterPosition}") } .onClick { holder -> Toast.makeText(context, "Clicked #${holder.adapterPosition}", Toast.LENGTH_SHORT).show() } .onLongClick { holder -> Toast.makeText(context, "Long-clicked #${holder.adapterPosition}", Toast.LENGTH_SHORT).show() } // Use the type in adapter LastAdapter(items, BR.item) .map
(typeHeader) .map(typePoint) .into(recyclerView) ``` -------------------------------- ### Dynamic Type Selection with TypeHandler (Java) Source: https://context7.com/nitrico/lastadapter/llms.txt Implements the `TypeHandler` interface in Java to dynamically provide `BaseType` instances based on item and position. This allows for varied item interactions and data binding configurations. ```java new LastAdapter(items, BR.item, stableIds).handler(new TypeHandler() { @Override public BaseType getItemType(@NotNull Object item, int position) { if (item instanceof Header) { return position == 0 ? typeHeaderFirst : typeHeader; } else if (item instanceof Point) { return typePoint; } else if (item instanceof Person) { return typePerson; } else if (item instanceof Car) { return typeCar; } return null; } }).into(list); ``` -------------------------------- ### Dynamic Type Selection with TypeHandler (Kotlin) Source: https://context7.com/nitrico/lastadapter/llms.txt Utilizes a lambda with the `TypeHandler` to return different `Type` or `ItemType` instances dynamically. This enables distinct callbacks and behaviors for items of the same class. ```kotlin LastAdapter(items, BR.item, stableIds).type { item, position -> when (item) { is Header -> if (position == 0) typeHeaderFirst else typeHeader is Point -> typePoint is Person -> typePerson is Car -> typeCar else -> null } }.into(recyclerView) ``` -------------------------------- ### Java RecyclerView Adapter with ItemType for Event Handling Source: https://context7.com/nitrico/lastadapter/llms.txt For Java developers, this snippet shows how to use the abstract `ItemType` class to define event handling and lifecycle callbacks for RecyclerView items. This approach offers a cleaner syntax compared to using lambdas directly in Java. ```java private final ItemType typePerson = new ItemType(R.layout.item_person) { @Override public void onCreate(@NotNull final Holder holder) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "Clicked " + holder.getBinding().getItem().getName() + " at #" + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show(); } }); } @Override public void onBind(@NotNull Holder holder) { Log.d(TAG, "Bound " + holder.getBinding().getItem() + " at #" + holder.getAdapterPosition()); } @Override public void onRecycle(@NotNull Holder holder) { Log.d(TAG, "Recycled " + holder.getBinding().getItem() + " at #" + holder.getAdapterPosition()); } }; // Use ItemType instances in adapter new LastAdapter(items, BR.item, stableIds) .map(Person.class, typePerson) .map(Car.class, typeCar) .map(Header.class, typeHeader) .into(list); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.