package com.kuanzhai.user.ui.menu
|
|
import androidx.core.os.bundleOf
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.clickDelay
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
|
import com.kuanzhai.user.OkApplication
|
import com.kuanzhai.user.R
|
import com.kuanzhai.user.dialog.CommentDialog
|
import com.kuanzhai.user.interfaces.StringCallback
|
import com.kuanzhai.user.network.HttpManager
|
import com.kuanzhai.user.network.entity.CommentBean
|
import com.kuanzhai.user.network.requestByF
|
import com.kuanzhai.user.ui.menu.adapter.CommentAdapter
|
import kotlinx.android.synthetic.main.fragment_comment.*
|
|
class CommentFragment:BaseFragment() {
|
override fun contentViewId() = R.layout.fragment_comment
|
|
private val id by lazy { arguments?.getLong("orderId")?:0L }
|
private var commentId = 0L
|
private val type by lazy { arguments?.getInt("type")?:0 }
|
private val isReply by lazy { arguments?.getBoolean("isReply")?:false }
|
|
private var page = 1
|
|
private val list = arrayListOf<CommentBean>()
|
private val adapter = CommentAdapter(list)
|
|
override fun onFirstVisibleToUser() {
|
commentId = arguments?.getLong("commentId")?:0L
|
lv_comment.setLayoutManager(LinearLayoutManager(requireContext()))
|
lv_comment.setAdapter(adapter)
|
lv_comment.setOnRefreshListener(object :SwipeRefreshRecyclerLayout.OnRefreshListener{
|
override fun onRefresh() {
|
page = 1
|
getData()
|
}
|
|
override fun onLoadMore() {
|
page++
|
getData()
|
}
|
})
|
iv_avatar.setImageURI(OkApplication.avatar)
|
tv_comment.clickDelay {
|
val commentDialog = CommentDialog()
|
commentDialog.arguments = bundleOf("id" to id,"type" to type)
|
commentDialog.setCallback(object :StringCallback{
|
override fun onRlt(rlt: String) {
|
page = 1
|
getData()
|
}
|
})
|
commentDialog.show(childFragmentManager,"comment")
|
}
|
adapter.callback = object :CommentAdapter.ReplyCallback{
|
override fun onComment(position: Int) {
|
val commentDialog = CommentDialog()
|
commentDialog.arguments = bundleOf("id" to id,"type" to type,"commentId" to list[position].id,"name" to list[position].userName
|
,"replyUserId" to list[position].userId)
|
commentDialog.setCallback(object :StringCallback{
|
override fun onRlt(rlt: String) {
|
page = 1
|
getData()
|
}
|
})
|
commentDialog.show(childFragmentManager,"comment")
|
}
|
|
override fun onReply(position: Int, subPosition: Int) {
|
val commentDialog = CommentDialog()
|
commentDialog.arguments = bundleOf("id" to id,"type" to type,"commentId" to list[position].replyCommentList[subPosition].id,
|
"name" to list[position].replyCommentList[subPosition].userName,"replyUserId" to list[position].replyCommentList[subPosition].userId)
|
commentDialog.setCallback(object :StringCallback{
|
override fun onRlt(rlt: String) {
|
page = 1
|
getData()
|
}
|
})
|
commentDialog.show(childFragmentManager,"comment")
|
}
|
}
|
getData()
|
}
|
|
private fun getData(){
|
HttpManager.getCommentList(id,type,page).requestByF(this,success = {_,data->
|
lv_comment.isRefreshing = false
|
if (page == 1)
|
list.clear()
|
list.addAll(data?: emptyList())
|
adapter.notifyDataSetChanged()
|
if (commentId!=0L){ //首次进入跳转到对应评论
|
val indexOf = data?.map { it.id }?.indexOf(commentId)?:0
|
lv_comment.mRecyclerView.scrollToPosition(indexOf)
|
(lv_comment.mRecyclerView.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(indexOf,0)
|
if (isReply)
|
adapter.showReplyList.add(commentId)
|
commentId = 0L
|
}
|
}){_,_->
|
lv_comment.isRefreshing = false
|
}
|
}
|
}
|