1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

Added drag and drop behavior to recycler view.

This commit is contained in:
Ubu 2019-02-24 11:42:17 +03:00
parent e410354f30
commit c13fcdb92e
9 changed files with 159 additions and 15 deletions

View file

@ -80,4 +80,7 @@ dependencies {
testImplementation unitTestDependencies.junit
//Acceptance tests dependencies
implementation project(':domain')
}

View file

@ -1,26 +1,39 @@
package com.agileburo.anytype
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import anytype.io.mobile.model.Block
class DocumentAdapter() : RecyclerView.Adapter<DocumentAdapter.ViewHolder>() {
class DocumentAdapter(private val blocks : MutableList<Block>) : RecyclerView.Adapter<DocumentAdapter.ViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_text_holder, parent, false)
return ViewHolder.TextHolder(view)
}
override fun getItemCount(): Int {
return 10
return blocks.size
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
fun onItemMoved(fromPosition: Int, toPosition: Int) : Boolean {
swapPosition(fromPosition, toPosition)
notifyItemMoved(fromPosition, toPosition)
return true
}
private fun swapPosition(fromPosition: Int, toPosition: Int) {
blocks.swap(fromPosition, toPosition)
}
sealed class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
class TextHolder(itemView : View) : ViewHolder(itemView)
}

View file

@ -0,0 +1,27 @@
package com.agileburo.anytype
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
import android.support.v7.widget.helper.ItemTouchHelper.DOWN
import android.support.v7.widget.helper.ItemTouchHelper.UP
class DragAndDropBehavior(val adapter : DocumentAdapter) : ItemTouchHelper.Callback() {
override fun getMovementFlags(p0: RecyclerView, p1: RecyclerView.ViewHolder): Int {
return makeMovementFlags(UP or DOWN, 0)
}
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
return adapter.onItemMoved(viewHolder.adapterPosition, target.adapterPosition)
}
override fun isLongPressDragEnabled(): Boolean {
return true
}
override fun isItemViewSwipeEnabled(): Boolean {
return false
}
override fun onSwiped(p0: RecyclerView.ViewHolder, p1: Int) {}
}

View file

@ -0,0 +1,7 @@
package com.agileburo.anytype
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}

View file

@ -10,13 +10,22 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnART.setOnClickListener {
val editorFragment = kRichEditorFragment{
}
val editorFragment = kRichEditorFragment{}
supportFragmentManager.beginTransaction()
.add(R.id.container, editorFragment)
.commit()
}
dragAndDropButton.setOnClickListener {
supportFragmentManager
.beginTransaction()
.add(R.id.container, PageFragment.getInstance())
.commit()
}
}
}

View file

@ -0,0 +1,42 @@
package com.agileburo.anytype
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.helper.ItemTouchHelper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import anytype.io.mobile.model.Block
import kotlinx.android.synthetic.main.fragment_page.*
class PageFragment : Fragment() {
companion object {
fun getInstance() : PageFragment {
return PageFragment()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_page, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
pageRecycler.apply {
layoutManager = LinearLayoutManager(context)
adapter = configureAdapter()
}
}
private fun configureAdapter() : DocumentAdapter {
return DocumentAdapter(
blocks = mutableListOf(Block(), Block(), Block(), Block(), Block(), Block(), Block())
).apply {
val callback = DragAndDropBehavior(this)
val touchHelper = ItemTouchHelper(callback)
touchHelper.attachToRecyclerView(pageRecycler)
}
}
}

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context=".MainActivity">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context=".MainActivity">
<Button
android:id="@+id/btnART"
@ -48,4 +48,17 @@
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"/>
<Button
android:text="Drag and Drop List"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dragAndDropButton"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/pageRecycler"
android:layout_width="0dp"
android:layout_height="0dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp">
<View android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:alpha="0.3"
android:background="@color/blue"/>
</FrameLayout>