CoordinatorLayout & AppBarLayout

But what about the following?

My idea of implementation

First of all, the UI structure of the whole page is realized by RecyclerView.

The realization of Sticker above is to add a layout exactly the same as that of the Item to be topped in RecyclerView at the top of the layout. Then listen to RecyclerView scrolling:

mPostDetailRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        if (pageStatus == null || pageStatus.stickerPos == -1) returnval preHolder = recyclerView.findViewHolderForAdapterPosition(pageStatus!! .stickerPos - 1) val targetHolder = recyclerView.findViewHolderForAdapterPosition(pageStatus!! .stickerPos) var offset =if(targetHolder ! = null) {/ / roll out - targetHolder. ItemView. Top}else{0}if(preHolder ! = null) { offset = -1 }if (offset < 0) {
            mPostDetailCommentHeaderSticker.visibility = View.GONE
        } else {
            mPostDetailCommentHeaderSticker.visibility = View.VISIBLE
        }
    }
}
Copy the code

Up here pageStatus. StickerPos is the location of Sticker’s data in RecyclerView. The above logic is quite complicated, but it does implement the requirement (-_-). Let me explain:

That is, I judge whether Sticker appears depending on itemView. Top and the previous itemView:

  1. If the currentBecomes the itemviewDisplayed in theRecyclerView, according to ititemView.topTo see if it rolled to the top
  2. If it’s the previous oneitemviewNo longerRecyclerViewIf it was recycled, it must have rolled out, which is displayed directly

The logic above is strange, but it does fulfill the need for a ceiling.

However, in the next iteration of requirements, the preceding ItemView of the Sticker ItemView will constantly change, so the above code has a bug… So what’s the solution?

A simpler, more general approach

And then I thought, “Look, I can display StickerItemView without relying on the previous ItemView:

mPostDetailRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        val currentDataPos = recyclerView.getChildAdapterPosition(recyclerView.getChildAt(0))

        if(pageStatus ! = null && currentDataPos < pageStatus.stickerPos) { mPostDetailCommentHeaderSticker.visibility = View.GONE }else {
            mPostDetailCommentHeaderSticker.visibility = View.VISIBLE
        }
    }
})
Copy the code

The above code fixes the bug very easily. And its logic is simple and universal:

Judge whether the position of the first item displayed in RecyclerView is greater than that of StickerItem. If so, display the top Sticker

Effect:

This implementation only applies to RecyclerView

PASS: If that’s what you’re doing, ignore me.

More tips: AdvancedAndroid