diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt index 6cc4b86c7e..bcf13f99ac 100644 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyTouchHelperCallback.kt @@ -85,7 +85,7 @@ abstract class EpoxyTouchHelperCallback : ItemTouchHelper.Callback() { dropTargets: List, curX: Int, curY: Int - ): EpoxyViewHolder = chooseDropTarget( + ): EpoxyViewHolder? = chooseDropTarget( selected as EpoxyViewHolder, dropTargets as List, curX, @@ -100,8 +100,8 @@ abstract class EpoxyTouchHelperCallback : ItemTouchHelper.Callback() { dropTargets: List, curX: Int, curY: Int - ): EpoxyViewHolder = - super.chooseDropTarget(selected, dropTargets, curX, curY) as EpoxyViewHolder + ): EpoxyViewHolder? = + super.chooseDropTarget(selected, dropTargets, curX, curY) as? EpoxyViewHolder override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int): Unit = onSelectedChanged(viewHolder as EpoxyViewHolder?, actionState) diff --git a/kotlinsample/src/main/AndroidManifest.xml b/kotlinsample/src/main/AndroidManifest.xml index f00ae31717..dff87a72b3 100644 --- a/kotlinsample/src/main/AndroidManifest.xml +++ b/kotlinsample/src/main/AndroidManifest.xml @@ -17,6 +17,7 @@ + \ No newline at end of file diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt new file mode 100644 index 0000000000..21ae61ec98 --- /dev/null +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/DragAndDropActivity.kt @@ -0,0 +1,82 @@ +package com.airbnb.epoxy.kotlinsample + +import android.os.Bundle +import android.util.Log +import android.view.View +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.recyclerview.widget.LinearLayoutManager +import com.airbnb.epoxy.EpoxyModel +import com.airbnb.epoxy.EpoxyRecyclerView +import com.airbnb.epoxy.EpoxyTouchHelper +import com.airbnb.epoxy.TypedEpoxyController + +class DragAndDropActivity : AppCompatActivity() { + private lateinit var recyclerView: EpoxyRecyclerView + private var fruits = mutableListOf( + "Apples", + "Blueberries", + "Bananas", + "Oranges", + "Dragon fruit", + "Mango", + "Avocado", + "Lychee" + ) + + private var epoxyController = object : TypedEpoxyController>() { + + override fun buildModels(data: List?) { + data?.forEach { fruit -> + dataBindingItem { + id("data binding $fruit") + text(fruit) + onClick { _ -> + Toast.makeText( + this@DragAndDropActivity, + "clicked $fruit", + Toast.LENGTH_LONG + ).show() + } + onVisibilityStateChanged { model, view, visibilityState -> + Log.d(TAG, "$model -> $visibilityState") + } + } + } + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity) + findViewById(R.id.epoxy_view_stub)?.visibility = View.GONE + + recyclerView = findViewById(R.id.recycler_view) + + EpoxyTouchHelper.initDragging(epoxyController) + .withRecyclerView(recyclerView) + .forVerticalList() + .forAllModels() + .andCallbacks(object : EpoxyTouchHelper.DragCallbacks>() { + override fun onModelMoved( + fromPosition: Int, + toPosition: Int, + modelBeingMoved: EpoxyModel<*>?, + itemView: View? + ) { + Log.d(TAG, "onModelMoved from:$fromPosition -> to:$toPosition") + val removed = fruits.removeAt(fromPosition) + fruits.add(toPosition, removed) + epoxyController.setData(fruits) + } + }) + + recyclerView.layoutManager = LinearLayoutManager(this) + recyclerView.setController(epoxyController) + epoxyController.setData(fruits) + } + + companion object { + private const val TAG = "DragAndDropActivity" + } +} diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt index 1ac2059ad7..e4a52bc144 100644 --- a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainActivity.kt @@ -135,6 +135,16 @@ class MainActivity : AppCompatActivity() { } } + itemCustomView { + id("custom view $i") + color(Color.MAGENTA) + title("Open Drag and Dropt activity") + listener { _ -> + Toast.makeText(this@MainActivity, "clicked", Toast.LENGTH_LONG).show() + startActivity(Intent(this@MainActivity, DragAndDropActivity::class.java)) + } + } + itemEpoxyHolder { id("view holder $i") title("this is a View Holder item") diff --git a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt index 40d74552f9..6131e27305 100644 --- a/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt +++ b/kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/StickyHeaderActivity.kt @@ -1,6 +1,7 @@ package com.airbnb.epoxy.kotlinsample import android.os.Bundle +import android.view.View import androidx.appcompat.app.AppCompatActivity import com.airbnb.epoxy.EpoxyRecyclerView import com.airbnb.epoxy.stickyheader.StickyHeaderLinearLayoutManager @@ -16,6 +17,8 @@ class StickyHeaderActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity) + findViewById(R.id.epoxy_view_stub)?.visibility = View.GONE + recyclerView = findViewById(R.id.recycler_view) // Sample usage when using [EpoxyAdapter]