diff --git a/.gitignore b/.gitignore index d2203c9..8b39b9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .* *.iml +.idea gradle.log local.properties diff --git a/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt b/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt index 269c575..c7ee895 100644 --- a/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt +++ b/bento-sample-app/src/main/java/com/yelp/android/bentosampleapp/ListViewActivity.kt @@ -1,6 +1,7 @@ package com.yelp.android.bentosampleapp import android.os.Bundle +import android.util.Log import android.view.Menu import android.view.MenuItem import android.widget.ArrayAdapter @@ -44,6 +45,7 @@ class ListViewActivity : AppCompatActivity() { addCarouselComponent(controller) addArrayAdapterComponent(controller) addAnimatedComponent(controller) + addListComponentRemovableItems(controller) } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -79,8 +81,7 @@ class ListViewActivity : AppCompatActivity() { controller.addComponent(simpleComponent) } - - private fun addListComponent(controller: ComponentController) { + private fun addListComponent(controller: ComponentController, removable: Boolean = false) { with(ListComponent(null, ListComponentExampleViewHolder::class.java)) { setStartGap(50) @@ -90,6 +91,26 @@ class ListViewActivity : AppCompatActivity() { } } + private fun addListComponentRemovableItems(controller: ComponentController) { + val component = ListComponent(null, ListComponentExampleViewHolder::class.java) + with(component) { + setStartGap(50) + setData(listOf("List element 1")) + toggleDivider(true) + controller.addComponent(this) + + listOf(1, 1, 1, 2, 4).forEachIndexed { index, i -> insertData(i, "List element ${index + 2}") } + insertData(0, "List element First") + insertData(0, "List element First") + insertData(1 + component.count / 2, "List element Last") + removeData("List element First") + val index = removeData("List element 1") + insertData(index, "List element 1") + removeData("List element 2") + removeData("List element nonexistent") + } + } + private fun addArrayAdapterComponent(controller: ComponentController) { val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, (1 until 42).map { "ArrayAdapter element $it" }) diff --git a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java index 4dccb92..38e7df9 100644 --- a/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java +++ b/bento/src/main/java/com/yelp/android/bento/components/ListComponent.java @@ -86,17 +86,31 @@ public void appendData(@NonNull List data) { } /** - * Removes the provided data items from the list. + * Inserts a single list item at the specified index in the list. + * + * @param index The index used to insert the given data in the list. + * @param data The list item to be inserted. + */ + public void insertData(int index, @NonNull T data) { + mData.add(index, data); + // Update 2 items if dividers are showing. + notifyItemRangeInserted(index, mShouldShowDivider ? 2 : 1); + } + + /** + * Removes the first occurrence of the specified data item from the list. * * @param data The data item to remove from the list. + * @return The index that the data occupied. Returns -1 if nothing was removed. */ - public void removeData(@NonNull T data) { + public int removeData(@NonNull T data) { int index = mData.indexOf(data); // Check if the object indeed is in the list. if (index != -1) { mData.remove(index); notifyItemRangeRemoved(getRemoveIndexStart(index), getRemoveItemCount()); } + return index; } /**