Skip to content

Commit

Permalink
Drag n Drop not working in 4.6.1 (#1195)
Browse files Browse the repository at this point in the history
* Fix Kotlin migration missing Nullable types

* Added drag n drop sample to test

* Cleaning
  • Loading branch information
BillCarsonFr authored Jun 3, 2021
1 parent 8fe48a6 commit 71d1198
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract class EpoxyTouchHelperCallback : ItemTouchHelper.Callback() {
dropTargets: List<RecyclerView.ViewHolder>,
curX: Int,
curY: Int
): EpoxyViewHolder = chooseDropTarget(
): EpoxyViewHolder? = chooseDropTarget(
selected as EpoxyViewHolder,
dropTargets as List<EpoxyViewHolder>,
curX,
Expand All @@ -100,8 +100,8 @@ abstract class EpoxyTouchHelperCallback : ItemTouchHelper.Callback() {
dropTargets: List<EpoxyViewHolder>,
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)
Expand Down
1 change: 1 addition & 0 deletions kotlinsample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</activity>

<activity android:name=".StickyHeaderActivity"/>
<activity android:name=".DragAndDropActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -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<List<String>>() {

override fun buildModels(data: List<String>?) {
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<View>(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<EpoxyModel<*>>() {
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,6 +17,8 @@ class StickyHeaderActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity)

findViewById<View>(R.id.epoxy_view_stub)?.visibility = View.GONE

recyclerView = findViewById(R.id.recycler_view)

// Sample usage when using [EpoxyAdapter]
Expand Down

0 comments on commit 71d1198

Please sign in to comment.