Skip to content

Instantly share code, notes, and snippets.

@hiking93
Last active March 15, 2018 03:29
Show Gist options
  • Save hiking93/2851e1945b5736e5b1531d40e196ceb5 to your computer and use it in GitHub Desktop.
Save hiking93/2851e1945b5736e5b1531d40e196ceb5 to your computer and use it in GitHub Desktop.
Context menu example.
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.*
import kotlinx.android.synthetic.main.fragment_main.*
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?) =
inflater.inflate(R.layout.fragment_main, container, false)!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView.apply {
layoutManager = LinearLayoutManager(context!!)
adapter = MainAdapter(context!!)
registerForContextMenu(this)
}
}
override fun onCreateContextMenu(menu: ContextMenu, view: View,
menuInfo: ContextMenu.ContextMenuInfo?) {
menu.setHeaderTitle(R.string.menu_title)
activity!!.menuInflater.inflate(R.menu.context_menu, menu)
}
override fun onContextItemSelected(item: MenuItem): Boolean {
val info = item.menuInfo as ContextMenuRecyclerView.ContextMenuInfo
val position = info.position
val data = info.itemView.tag
val message = getString(R.string.clicked_format, item.title, position, data)
AlertDialog.Builder(context!!).setMessage(message).show()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment