аналогично этому билету.
Перетаскивание между двумя фрагментами
Я думаю, что проблема связана с методом View.StartDragAndDrop в longclicklistener.
он инициируется в одном фрагменте, а не в другом, поэтому другой фрагмент не читает никаких действий перетаскивания. р>
Код: Выделить всё
private var _fragmentCreateActivity: FragmentCreateActivityBinding? = null
private var activitiesAdapter: ActivitiesAdapter? = null
private val activitiesArray: MutableList = mutableListOf()
private val binding get() = _fragmentCreateActivity!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_fragmentCreateActivity = FragmentCreateActivityBinding.inflate(inflater, container, false)
//TODO: find a better way to store the activities instead of an empty list
activitiesAdapter = ActivitiesAdapter(activitiesArray,this)
val addActivityButton = _fragmentCreateActivity?.btnAddActivity
_fragmentCreateActivity!!.rvActivities.adapter= activitiesAdapter
_fragmentCreateActivity!!.rvActivities.layoutManager= LinearLayoutManager(this.context)
addActivityButton?.setOnClickListener {
showAlertDialogButtonClicked(activitiesAdapter!!)
}
return binding.root
}
private fun showAlertDialogButtonClicked(adapter: ActivitiesAdapter) {
val bindingCrtItmDialog: DialogCreateItemBinding = DialogCreateItemBinding.inflate(layoutInflater)
// Create an alert builder
val builder = androidx.appcompat.app.AlertDialog.Builder(this.requireContext())
// set the custom layout
val customLayout: View = bindingCrtItmDialog.root
builder.setView(customLayout)
// add a button
builder.setPositiveButton("OK") { dialog: DialogInterface?, which: Int ->
// send data from the AlertDialog to the Activity
val activityTitle = bindingCrtItmDialog.CrtActEditText.text.toString()
if(activityTitle.isNotBlank()){
val newActivity = ActivityClass(activityTitle,null)
adapter.addActivity(newActivity)
}
}
// create and show the alert dialog
val dialog = builder.create()
dialog.show()
}
override fun onDestroyView() {
super.onDestroyView()
_fragmentCreateActivity = null
}
override fun onItemLongClick(position: Int, dayText: String?, v: View?) : Boolean {
val item = ClipData.Item(activitiesArray[position].name)
val dragData = ClipData(
activitiesArray[position].name,
arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN),
item)
// Instantiate the drag shadow builder.
val myShadow = View.DragShadowBuilder(v)
// Start the drag.
v?.startDragAndDrop(dragData, // The data to be dragged.
myShadow, // The drag shadow builder.
v, // No need to use local data.
0 // Flags. Not currently used, set to 0.
)
return true
}
Подробнее здесь: https://stackoverflow.com/questions/782 ... ain-a-stac