Skip to content

Instantly share code, notes, and snippets.

@nikartx
Created July 31, 2020 14:05
Show Gist options
  • Save nikartx/0760587edce63532655c762d68ed7e12 to your computer and use it in GitHub Desktop.
Save nikartx/0760587edce63532655c762d68ed7e12 to your computer and use it in GitHub Desktop.
Floating sticky dialog for RecyclerView
/**
* Sticky dialog for [RecyclerView].
* Create instance when RecyclerView will be initialized and
* call [StickyDialog.show] when a user clicked by item in a list
*
* @author Ivan V on 31.07.2020.
* @version 1.0
*/
class StickyDialog : DialogFragment {
private var rView: RecyclerView? = null
private var listener: EventListener? = null
private var posX: Int = 0
private var posY: Int = 0
companion object {
fun newInstance(source: RecyclerView?): StickyDialog {
return StickyDialog(source)
}
}
constructor() {}
constructor(rView: RecyclerView?) {
this.rView = rView
this.rView!!.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
if (e.action == MotionEvent.ACTION_DOWN) {
posX = e.x.toInt()
posY = e.y.toInt()
}
return false
}
})
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_FRAME, R.style.Theme_AppCompat_Dialog)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.dialog_layout, container, false)
// Init view, listener e.t.c
return view
}
override fun onResume() {
super.onResume()
setupParams()
}
private fun setupParams() {
val window = dialog!!.window
window?.setBackgroundDrawableResource(android.R.color.transparent)
val layoutParams = window?.attributes
layoutParams?.gravity = Gravity.TOP or Gravity.START
layoutParams?.dimAmount = 0.1f
layoutParams?.x = posX
layoutParams?.y = posY
window?.attributes = layoutParams
}
fun show(manager: FragmentManager, listener: EventListener?) {
super.show(manager, null)
this.listener = listener
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
rView = null
listener = null
}
interface EventListener {
fun copy()
fun edit()
fun delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment