1.Create an android application project in Studio
Create an android application project in eclipse. To drag and drop list items in list view, we need a custom list view. So first create a class for custom list view and extend it to ListView super class.
2.OnItemLongClickListener
Set OnItemLongClick listener in adapter view and create a hover cell for the selected item.
holder.rowView.setOnLongClickListener(View.OnLongClickListener {
mStartDragListener.requestDrag(holder)
true})
3. activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.com.androiditemdraganddrop.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/contactsRecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
4. StartDragListener.kt Interface
package android.com.androiditemdraganddrop
import androidx.recyclerview.widget.RecyclerView
interface StartDragListener {
fun requestDrag(viewHolder: RecyclerView.ViewHolder)
}
5. MainNavigator.kt Interface
package android.com.androiditemdraganddrop
interface MainNavigator {
fun onRecentActivityClick(accountName: String)
}
6. ItemMoveCallback.kt
package android.com.androiditemdraganddrop
interface MainNavigator {
fun onRecentActivityClick(accountName: String)
}
Here is main adapter this adapter is use-full to drag Item from one position to another position
7. MainAdapter.xml file
package android.com.androiditemdraganddrop
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_favorites.view.*
import java.util.*
import kotlin.collections.ArrayList
import kotlin.com.androiditemdraganddrop.R
class MainAdapter(
private val context: Context,
private val favoritesList: ArrayList<String>,
private val mStartDragListener: StartDragListener
) :
RecyclerView.Adapter<MainAdapter.MyViewHolder>(),
ItemMoveCallback.ItemTouchHelperContract {
lateinit var mListener: MainNavigator
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_favorites,
parent,
false
)
)
}
override fun getItemCount(): Int {
return favoritesList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.accountName?.text = favoritesList.get(position)
holder.rowView.setOnLongClickListener(View.OnLongClickListener {
mStartDragListener.requestDrag(holder)
true
})
holder.itemView.setOnClickListener {
mListener.onRecentActivityClick(favoritesList[position])
}
}
class MyViewHolder(internal var rowView: View) : RecyclerView.ViewHolder(rowView) {
val accountName = rowView.account_name
val imgProfilePic = rowView.img_profilePic
}
override fun onRowMoved(fromPosition: Int, toPosition: Int) {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(favoritesList, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(favoritesList, i, i - 1)
}
}
notifyItemMoved(fromPosition, toPosition)
}
override fun onRowSelected(myViewHolder: MyViewHolder) {
myViewHolder.rowView.setBackgroundColor(Color.WHITE)
}
override fun onRowClear(myViewHolder: MyViewHolder) {
myViewHolder.rowView.setBackgroundColor(Color.WHITE)
}
}
Here is the screen shorts.
Finally MainActivity.kt file to send data to adapter
8.MainActivity.kt file
package android.com.androiditemdraganddrop
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.com.androiditemdraganddrop.R
class MainActivity : AppCompatActivity(), StartDragListener, MainNavigator {
private var mFavoritesAdapter: MainAdapter? = null
private val mFavoriteList: ArrayList<String> = ArrayList()
internal var touchHelper: ItemTouchHelper? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mFavoriteList.add("Account 1")
mFavoriteList.add("Account 2")
mFavoriteList.add("Account 3")
mFavoriteList.add("Account 4")
mFavoriteList.add("Account 5")
mFavoriteList.add("Account 6")
mFavoriteList.add("Account 7")
mFavoriteList.add("Account 8")
mFavoriteList.add("Account 9")
mFavoriteList.add("Account 10")
//Favorites list
val layoutManager = LinearLayoutManager(this)
contactsRecycleView!!.layoutManager = (layoutManager)
mFavoritesAdapter = MainAdapter(this, mFavoriteList, this)
val callback = ItemMoveCallback(mFavoritesAdapter!!)
touchHelper = ItemTouchHelper(callback)
touchHelper!!.attachToRecyclerView(contactsRecycleView)
contactsRecycleView!!.adapter = mFavoritesAdapter
mFavoritesAdapter!!.mListener = this
}
/*
* This method used for requesting drag items.
* */
override fun requestDrag(viewHolder: RecyclerView.ViewHolder) {
touchHelper!!.startDrag(viewHolder)
}
/*
* This method used for fetching item details.
* */
override fun onRecentActivityClick(accountName: String) {
Toast.makeText(this, accountName, Toast.LENGTH_LONG).show()
}
}
No comments:
Post a Comment