Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag n Drop not working in 4.6.1 #1195

Merged
merged 3 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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