flexbox-layout icon indicating copy to clipboard operation
flexbox-layout copied to clipboard

Showing blank space when removing very first item when content is long ( notifyItemRemoved() issue)

Open punitshah89 opened this issue 7 years ago • 4 comments

  • [x] I have searched existing issues and confirmed this is not a duplicate

Issues and steps to reproduce

Using demo-playground

  1. Change settings of app to use width wrap_content for list item
  2. Change FlexItemViewHolder class inside com.google.android.flexbox package to show long content at 0th position
  3. Change FlexItemAdapter class's removeItem() function inside com.google.android.flexbox package to remove 0th item from list so when we press '-' button it removes from top
  4. Add items using "+" fab multiple times (7 times) based on list used (see attached snapshot)
  5. Remove item using "-" fab once

Expected behavior

RecyclerView should remove simply remove item and move all items accordingly

Version of the flexbox library

0.3.1, 0.3.2

Code example/snapshots based on repro steps

screenshot_1521163662 screenshot_1521163667

class FlexItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
   val textList: List<String> = listOf<String>(
           "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy",
           "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', ",
           "fdsfdsfdsfds", "gfgdfgdfgdfdf", "gdfgdfgfdgfd", "gff", "gfdgfd", "gfgdf", "gfdg")
   private val textView: TextView = itemView.findViewById(R.id.textview)

   fun bindTo(params: RecyclerView.LayoutParams) {
       val adapterPosition = adapterPosition
       textView.apply {
           text = textList[adapterPosition]
           setBackgroundResource(R.drawable.flex_item_background)
           gravity = Gravity.CENTER
           layoutParams = params
       }
   }
}
    fun removeItem(position: Int) {
        if (position < 0 || position >= layoutParams.size) {
            return
        }
        layoutParams.removeAt(0)
        notifyItemRemoved(0)
        notifyItemRangeChanged(position, layoutParams.size)
    }

screenshot_1521163698 5. screenshot_1521163706

Note: if used notifyDataSetChange() instead of notifyItemRemoved works fine

punitshah89 avatar Mar 16 '18 01:03 punitshah89

Hi @thagikura, looks there is issue in updateDirtyPosition() function inside FlexboxLayoutManager.java

I'm not sure about the best solution, but firstVisiblePosition <= positionStart condition satisfies and returns from function, my guess is this condition should be firstVisiblePosition < positionStart. changing this solves the issue.


        if (firstVisiblePosition <= positionStart && positionStart <= lastVisiblePosition) {
            return;
        }

        // Assign the pending scroll position and offset so that the first visible position is
        // restored in the next layout.
        mPendingScrollPosition = getPosition(firstView);

        if (!isMainAxisDirectionHorizontal() && mIsRtl) {
            mPendingScrollPositionOffset = mOrientationHelper.getDecoratedEnd(firstView) +
                    mOrientationHelper.getEndPadding();
        } else {
            mPendingScrollPositionOffset = mOrientationHelper.getDecoratedStart(firstView) -
                    mOrientationHelper.getStartAfterPadding();
        }

punitshah89 avatar Mar 26 '18 05:03 punitshah89

I have the same problem here!!! (1)In addition, it only appears when the first element is deleted. (2)You will then find that after the first element is deleted, the next line will be blank.

WanDa1993 avatar May 09 '19 14:05 WanDa1993

According to the form, I think FlexBoxLayoutManager + RecyclerView, it's going to generate strange reuse behavior

eg: [xxxxxxx] [xxxxx] [xxxxxxxxxxxx] [xxxxxxxx]

removeItem -> [xxxxxxx] is index zero

addItem -> [xxxxxx] is index zero

notifyDataSetChanged

result: [ xxxxxx ] [ xxxx ] [xxxxxxxxxxxx] [xxxxxxxx]

WanDa1993 avatar May 13 '19 06:05 WanDa1993

thanks a lot

honglei92 avatar Jul 12 '21 08:07 honglei92