Skip to content

Instantly share code, notes, and snippets.

@hiking93
Last active February 27, 2019 18:04
Show Gist options
  • Save hiking93/329d6b9e4e252409b59dfd7b1436eb4e to your computer and use it in GitHub Desktop.
Save hiking93/329d6b9e4e252409b59dfd7b1436eb4e to your computer and use it in GitHub Desktop.
RecyclerView with context menu implemented.
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.ContextMenu
import android.view.View
/**
* Recycler view with context menu implemented.
*
* @author Hiking
*/
class ContextMenuRecyclerView : RecyclerView {
class ContextMenuInfo(
val recyclerView: RecyclerView,
val itemView: View,
val position: Int,
val id: Long
) : ContextMenu.ContextMenuInfo
private var contextMenuInfo: ContextMenuInfo? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
: super(context, attrs, defStyleAttr)
override fun getContextMenuInfo() = contextMenuInfo
override fun showContextMenuForChild(originalView: View): Boolean {
saveContextMenuInfo(originalView)
return super.showContextMenuForChild(originalView)
}
override fun showContextMenuForChild(originalView: View, x: Float, y: Float): Boolean {
saveContextMenuInfo(originalView)
return super.showContextMenuForChild(originalView, x, y)
}
private fun saveContextMenuInfo(originalView: View) {
val position = getChildAdapterPosition(originalView)
val longId = getChildItemId(originalView)
contextMenuInfo = ContextMenuInfo(this, originalView, position, longId)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment