package com.dollearn.student.dialog
|
|
import android.view.Gravity
|
import androidx.core.os.bundleOf
|
import androidx.fragment.app.FragmentManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.activity.BaseActivity
|
import cn.sinata.xldutils.utils.myToast
|
import cn.sinata.xldutils.utils.showAllowingStateLoss
|
import com.dollearn.student.R
|
import com.dollearn.student.network.HttpManager
|
import com.dollearn.student.network.entity.Student
|
import com.dollearn.student.network.request
|
import com.dollearn.student.ui.home.AddStudentActivity
|
import com.dollearn.student.ui.home.ChooseUserActivity
|
import com.dollearn.student.ui.home.adapter.StudentAdapter
|
import com.dollearn.student.utils.extention.clickDelay
|
import kotlinx.android.synthetic.main.dialog_switch_student.*
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
|
/**
|
* 单选
|
*/
|
class SwitchStudentDialog:BaseDialogFragment() {
|
override fun setContentView() = R.layout.dialog_switch_student
|
|
override fun setGravity() = Gravity.BOTTOM
|
|
private val list = arrayListOf<Student>()
|
private val adapter by lazy { StudentAdapter(list,StudentAdapter.TYPE_MATCH_CHOOSE) }
|
|
private val checked by lazy { arguments?.getParcelable<Student>("checked") }
|
|
var callback:ResultCallback? = null
|
|
override fun initView() {
|
rv_student.layoutManager = LinearLayoutManager(requireContext())
|
rv_student.adapter = adapter
|
getStudent()
|
|
iv_close.clickDelay {
|
dismissAllowingStateLoss()
|
}
|
adapter.setOnItemClickListener { view, position ->
|
val stuId = list[position].id
|
adapter.checked.clear()
|
adapter.checked.add(stuId?:"")
|
adapter.notifyDataSetChanged()
|
}
|
tv_action.setOnClickListener {
|
if (adapter.checked.isEmpty())
|
myToast("请选择一个人员")
|
else{
|
val list1 = list.filter { it.id in adapter.checked }
|
callback?.checked(list1)
|
dismissAllowingStateLoss()
|
}
|
}
|
}
|
|
private fun getStudent(){
|
HttpManager.getParticipant().request(requireActivity() as BaseActivity){_,data->
|
list.clear()
|
list.addAll(data?: arrayListOf())
|
if (checked!=null)
|
adapter.checked.add(checked!!.id?:"" )
|
adapter.notifyDataSetChanged()
|
}
|
}
|
|
interface ResultCallback{
|
fun checked(list: List<Student>)
|
}
|
|
companion object{
|
/**
|
* @param filterReal true:需要过滤掉未实名的成员
|
*/
|
fun show(fm:FragmentManager,student:Student?,callback: ResultCallback){
|
val chooseStudentDialog = SwitchStudentDialog()
|
chooseStudentDialog.arguments = bundleOf("checked" to student)
|
chooseStudentDialog.callback = callback
|
chooseStudentDialog.showAllowingStateLoss(fm,"stu")
|
}
|
}
|
}
|