Android 如何与 fragment 中的嵌套 recyclerView 交互

Android how to interact with nested recyclerView from fragment

如何在 fragment 中正确地将数据发送到 child adapter? 我基本上是在尝试实现像 comments-section 这样的 Instagram,例如一堆评论,每个评论都可以有更多评论(回复)。

为此,我使用一个 main recyclerView + main adapter,这些实例保留在我的片段中,并且在主适配器中我绑定了 children 评论(recyclerView + adapter).

向主适配器添加注释很容易,因为 object 在片段中始终可用,所以我只需调用 mainAdapter.addComments(newComments):

主适配器

fun addComments(newComments: List<Comment>){
    comments.addAll( 0, newComments) //loading comments or previous comments go to the beginning
    notifyItemRangeInserted(0, newComments.size)
}

但是如何调用一个特定的 nested-rV 的 addComments?我读到我不应该保存适配器实例并且只使用位置。 我正尝试在我的 Fragment 中执行以下操作:

val item = rVComments.findViewHolderForItemId(mAdapter.itemId)!!.itemView
val adapt = item.rVReplies.adapter as ChildCommentsAdapter
adapt.addComment(it.data.comment)

但这并不是很好:因为我们只有 RecyclerView,如果用户在发布或获取项目后滚动,则特定的 ViewHolder 通常已经被回收,这会导致 NullPointerException。 因此,最初的问题是:如何与嵌套的回收视图及其适配器正确交互?如果答案是通过接口,请提供一个示例,因为我已经尝试过但没有成功,因为我不应该t 保存适配器 objects.

您可以通过放置注释来使用单个 multi-view 类型的适配器来实现 作为父项的一部分,您可以将子项添加到父项下方并调用 notifyItemRangeInserted.

这样您就不必处理大部分回收问题。

当您想更新评论时,您只需更新父项内的评论并调用 notifyItemChanged

如果您需要,我创建了一个 library 可以在编译时为您生成该代码。 它支持您想要的确切案例等等。

根据@Gil Goldzweig 的建议,这是我所做的:如果是带有回复的 Instagram 之类的评论系统,我确实使用了嵌套的 recyclerView 系统。它只是让添加和删除项目变得更容易。但是,对于 如何在片段中正确地将数据发送到子适配器这一问题? 你不知道。它变得非常混乱。从我的片段中,我将数据发送到我的 mainAdapter,后者又将数据发送到相关的 childAdapter。使其顺利的关键是在向 mainAdapter 添加评论时使用 notifyItemRangeInserted,然后在添加对评论的回复时使用 notifyItemChanged。第二个事件将允许使用 payload 将数据发送到子适配器。这是代码以防其他人感兴趣:

片段

class CommentsFragment : androidx.fragment.app.Fragment(), Injectable,
    SendCommentButton.OnSendClickListener, CommentsAdapter.Listener {


@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

private val viewModel by lazy {
    ViewModelProviders.of(requireActivity(), viewModelFactory).get(CommentsViewModel::class.java)
}
private val searchViewModel by lazy {
    ViewModelProviders.of(requireActivity(), viewModelFactory).get(SearchViewModel::class.java)
}

private val mAdapter = CommentsAdapter(this)
private var contentid: Int = 0 //store the contentid to process further posts or requests for more comments
private var isLoadingMoreComments: Boolean = false //used to check if we should fetch more comments


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {


    return inflater.inflate(R.layout.fragment_comments, container, false)

}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    //hide the action bar
    activity?.findViewById<BottomNavigationView>(R.id.bottomNavView)?.visibility = View.GONE


    contentid = arguments!!.getInt("contentid") //argument is mandatory, since comment is only available on content

    ivBackArrow.setOnClickListener{ activity!!.onBackPressed() }

    viewModel.initComments(contentid) //fetch comments

    val layoutManager = LinearLayoutManager(this.context)
    layoutManager.stackFromEnd = true
    rVComments.layoutManager = layoutManager
    mAdapter.setHasStableIds(true)
    rVComments.adapter = mAdapter



    setupObserver() //observe initial comments response
    setupSendCommentButton()
    post_comment_text.setSearchViewModel(searchViewModel)
    setupScrollListener(layoutManager) //scroll listener to load more comments

    iVCancelReplyTo.setOnClickListener{
        //reset ReplyTo function
        resetReplyLayout()
    }


}

private fun loadMoreComments(){
    viewModel.fetchMoreComments(contentid, mAdapter.itemCount)
    setupObserver()
}

/*
1.check if not already loading
2.check scroll position 0
3.check total visible items != total recycle items
4.check itemcount to make sure we can still make request
 */
private fun setupScrollListener(layoutManager: LinearLayoutManager){
    rVComments.addOnScrollListener(object: RecyclerView.OnScrollListener(){
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            val visibleItemCount = rVComments.childCount
            val totalItemCount = layoutManager.itemCount
            val pos = layoutManager.findFirstCompletelyVisibleItemPosition()
            if(!isLoadingMoreComments && pos==0 && visibleItemCount!=totalItemCount && mAdapter.itemCount%10==0){
                //fetch more comments
                isLoadingMoreComments = true
                loadMoreComments()
            }
        }

    })
}





private fun setupSendCommentButton() {
    btnSendComment.setOnSendClickListener(this)
}

override fun onSendClickListener(v: View?) {
    if(isInputValid(post_comment_text.text.toString())) {
        val isReply = mAdapter.commentid!=null
        viewModel.postComment(post_comment_text.text.toString(), mAdapter.commentid?: contentid, isReply) //get reply ID, otherwise contentID
        observePost()
        post_comment_text.setText("")
        btnSendComment.setCurrentState(SendCommentButton.STATE_DONE)
    }
}

override fun postCommentAsReply(username: String) {
    //main adapter method to post a reply
    val replyText = "${getString(R.string.replyingTo)} $username"
    tVReplyTo.text = replyText
    layoutReplyTo.visibility=View.VISIBLE
    post_comment_text.requestFocus()
}

override fun fetchReplies(commentid: Int, commentsCount: Int) {
    //main adapter method to fetch replies
    if(!isLoadingMoreComments){ //load one series at a time
        isLoadingMoreComments = true
        viewModel.fetchReplies(commentid, commentsCount)
        viewModel.replies.observe(this, Observer<Resource<List<Comment>>> {
            if (it?.data != null) when (it.status) {
                Resource.Status.LOADING -> {
                    //showProgressBar(true)
                }
                Resource.Status.ERROR -> {
                    //showProgressBar(false)
                    isLoadingMoreComments = false
                }
                Resource.Status.SUCCESS -> {
                    isLoadingMoreComments = false
                    mAdapter.addReplies(mAdapter.replyCommentPosition!!, it.data)
                    rVComments.scrollToPosition(mAdapter.replyCommentPosition!!)
                }
            }
        })
    }

}


private fun isInputValid(text: String): Boolean = text.isNotEmpty()

private fun observePost(){
    viewModel.postComment.observe(this, Observer<Resource<PostCommentResponse>> {
        if (it?.data != null) when (it.status) {
            Resource.Status.LOADING -> {
                //showProgressBar(true)
            }
            Resource.Status.ERROR -> {
                //showProgressBar(false)
            }
            Resource.Status.SUCCESS -> {
                if(it.data.asReply){
                    //dispatch comment to child adapter via main adapter
                    mAdapter.addReply(mAdapter.replyCommentPosition!!, it.data.comment)
                    rVComments.scrollToPosition(mAdapter.replyCommentPosition!!)
                }else{
                    mAdapter.addComment(it.data.comment)
                }
                resetReplyLayout()
                //showProgressBar(false)
            }
        }
    })
}



private fun setupObserver(){
    viewModel.comments.observe(this, Observer<Resource<List<Comment>>> {
        if (it?.data != null) when (it.status) {
            Resource.Status.LOADING -> {
                //showProgressBar(true)
            }
            Resource.Status.ERROR -> {
                isLoadingMoreComments = false
                //showProgressBar(false)
            }
            Resource.Status.SUCCESS -> {
                mAdapter.addComments(it.data)
                isLoadingMoreComments = false
                //showProgressBar(false)
            }
        }
    })
}

private fun resetReplyLayout(){
    layoutReplyTo.visibility=View.GONE
    mAdapter.replyCommentPosition = null
    mAdapter.commentid = null
}



override fun onStop() {
    super.onStop()
    activity?.findViewById<BottomNavigationView>(R.id.bottomNavView)?.visibility = View.VISIBLE
}

}

主适配器

class CommentsAdapter(private val listener: Listener) : RecyclerView.Adapter<CommentsAdapter.ViewHolder>(), ChildCommentsAdapter.ChildListener {

//method from child adapter
override fun postChildReply(replyid: Int, username: String, position: Int) {
    commentid = replyid
    replyCommentPosition = position
    listener.postCommentAsReply(username)
}

interface Listener {
    fun postCommentAsReply(username: String)
    fun fetchReplies(commentid: Int, commentsCount: Int=0)
}


class ViewHolder(val view: View) : RecyclerView.ViewHolder(view)

private var comments = mutableListOf<Comment>()
private var repliesVisibility = mutableListOf<Boolean>() //used to store visibility state for replies
var replyCommentPosition: Int? = null //store the main comment's position
var commentid: Int? = null //used to indicate which comment is replied to



override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
   val view = LayoutInflater.from(parent.context)
           .inflate(R.layout.item_comment, parent, false)

    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    val comment = comments[position]

    with(holder.view) {
        //reset visibilities (rebinding purpose)
        rVReplies.visibility = View.GONE
        iVMoreReplies.visibility = View.GONE
        tVReplies.visibility = View.GONE

        content.loadUserPhoto(comment.avatarThumbnailURL)
        text.setCaptionText(comment.username!!, comment.comment)
        tvTimestamp.setTimeStamp(comment.timestamp!!)

        val child = ChildCommentsAdapter(
            //we pass parent commentid and position to child to be able to pass it again on click
            this@CommentsAdapter, comments[holder.adapterPosition].id!!, holder.adapterPosition
        )
        val layoutManager = LinearLayoutManager(this.context)
        rVReplies.layoutManager = layoutManager
        rVReplies.adapter = child


        //initial visibility block when binding the viewHolder
        val txtMore = this.resources.getString(R.string.show_more_replies)
        if(comment.repliesCount>0) {
            tVReplies.visibility = View.VISIBLE
            if (repliesVisibility[position]) {
                //replies are to be shown directly
                rVReplies.visibility = View.VISIBLE
                child.addComments(comment.replies!!)
                tVReplies.text = resources.getString(R.string.hide_replies)

                if (comment.repliesCount > comment.replies!!.size) {
                    //show the load more replies arrow if we can fetch more replies
                    iVMoreReplies.visibility = View.VISIBLE
                }
            } else {
                //replies all hidden
                val txt = txtMore + " (${comment.repliesCount})"
                tVReplies.text = txt
            }
        }

        //second visibility block when toggling with the show more/hide textView
        tVReplies.setOnClickListener{
            //toggle child recyclerView visibility and change textView text
            if(holder.view.rVReplies.visibility == View.GONE){
                //show stuff
                if(comment.replies!!.isEmpty()){
                    Timber.d(holder.adapterPosition.toString())
                    //fetch replies if none were fetched yet
                    replyCommentPosition = holder.adapterPosition
                    listener.fetchReplies(comments[holder.adapterPosition].id!!)
                }else{
                    //load comments into adapter if not already
                    if(comment.replies!!.size>child.comments.size){child.addComments(comment.replies!!)}
                }

                repliesVisibility[position] = true
                holder.view.rVReplies.visibility = View.VISIBLE
                holder.view.tVReplies.text = holder.view.resources.getString(R.string.hide_replies)
                if (comment.repliesCount > comment.replies!!.size && comment.replies!!.isNotEmpty()) {
                    //show the load more replies arrow if we can fetch more replies
                    iVMoreReplies.visibility = View.VISIBLE
                }
            }else{
                //hide replies and change text
                repliesVisibility[position] = false
                holder.view.rVReplies.visibility = View.GONE
                holder.view.iVMoreReplies.visibility = View.GONE
                val txt = txtMore + " (${comment.repliesCount})"
                holder.view.tVReplies.text = txt
            }
        }



        tvReply.setOnClickListener{
            replyCommentPosition = holder.adapterPosition
            commentid = comments[holder.adapterPosition].id!!
            listener.postCommentAsReply(comments[holder.adapterPosition].username!!)
        }

        iVMoreReplies.setOnClickListener{
            replyCommentPosition = holder.adapterPosition
            listener.fetchReplies(comments[holder.adapterPosition].id!!, layoutManager.itemCount) //pass amount of replies too
        }


    }
}

@Suppress("UNCHECKED_CAST")
override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
    if(payloads.isNotEmpty()){
        //add reply to child adapter
        with(holder.view){
            Timber.d(payloads.toString())
            val adapter = rVReplies.adapter as ChildCommentsAdapter
            if(payloads[0] is Comment){
                adapter.addComment(payloads[0] as Comment)
            }else{
                //will be of type List<Comment>
                adapter.addComments(payloads[0] as List<Comment>)
                val comment = comments[position]
                if (comment.repliesCount > comment.replies!!.size) {
                    //show the load more replies arrow if we can fetch more replies
                    iVMoreReplies.visibility = View.VISIBLE
                }else{
                    iVMoreReplies.visibility = View.GONE
                }
            }
        }
    }else{
        super.onBindViewHolder(holder,position, payloads) //delegate to normal binding process
    }
}


override fun getItemCount(): Int = comments.size


//add multiple replies to child adapter at pos 0
fun addReplies(position: Int, newComments: List<Comment>){
    comments[position].replies!!.addAll(0, newComments)
    notifyItemChanged(position, newComments)
}

//add a single reply to child adapter at last position
fun addReply(position: Int, newComment: Comment){
    comments[position].replies!!.add(newComment)
    comments[position].repliesCount += 1 //update replies count in case viewHolder gets rebinded
    notifyItemChanged(position, newComment)
}

//add a new comment to main adapter at last position
fun addComment(comment: Comment){
    comments.add(comment) //new comment just made goes to the end
    repliesVisibility.add(false)
    notifyItemInserted(itemCount-1)
}

//add multiple new comments to main adapter at pos 0
fun addComments(newComments: List<Comment>){
    comments.addAll( 0, newComments) //loading comments or previous comments go to the beginning
    repliesVisibility.addAll(0, List(newComments.size) { false })
    notifyItemRangeInserted(0, newComments.size)
}



}

childAdapter非常基础,逻辑几乎为0。