ItemDecoration overrides getItemOffsets () and animation

I apply equal fields to mine RecyclerViewusing GridLayoutManager, overriding the method getItemOffsets()(see my code below).

However, when an object is deleted from the adapter, the delete animation is called without offsets. Thus, the anomaly begins in a different position than the object to be removed.

I tried to get the position through getSpanIndex(position), but the position ( parent.getChildAdapterPosition(view)) returns NO_POSITION, since the object is already removed from the adapter when called getItemOffsets().

Is there any way to get offsets in my case?

@Override
public void getItemOffsets(Rect outRect, View view, 
                     RecyclerView parent, RecyclerView.State state) {

    GridLayoutManager mgr = parent.getLayoutManager();
    int position = parent.getChildAdapterPosition(view);

    if (position == RecyclerView.NO_POSITION) {
       // here I need to access the position of the current element
       // and call outRect.set(left, top , right, bottom);
       // which is not possible because it is no longer in the adapter
        return;
    } 

    int spanCount = mgr.getSpanCount();
    int spanSize = mgr.getSpanSizeLookup().getSpanSize(position);
    int spanIndex = mgr.getSpanSizeLookup().getSpanIndex(position, spanCount);

    if (spanIndex == spanCount-1) {
        // last element
        left = space / 2;
        right = space;
    } else if (spanIndex == 0) {
        // first element
        left = space;
        right = space / 2;
    } else {
        // middle element
        left = space / 2;
        right = space / 2;
    }
    outRect.set(left, top, right, bottom);
}
+5
source share
5 answers

LayoutParams. - LayoutManager, .

int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewAdapterPosition();
+4

, . , ItemDecorator, LayoutManager, RecyclerView, , .

, Recycler ViewHolder . , (getLayoutPosition(), getAdapterPosition()).

parent.getChildViewHolder(view).getLayoutPosition();
parent.getChildViewHolder(view).getAdapterPosition;

, . ItemCount, adapter.getItemCount(), , Adapter Recycler.

state.getItemCount()
+4

, , ViewHolder#getOldPosition.

javadoc :

LayoutManager , RecyclerView 3 ViewHolders .

onLayout ViewHolder, .

, getOldPosition() ViewHolder, , .

, ItemDecoration # getItemOffsets, :

parent.getChildViewHolder(view).getOldPosition()

, :).

+2

, . , , .

, .

:

ItemDecoration getItemOffsets() parent.getChildViewHolder(view).getAdapterPosition();, , . RecyclerView.NO_POSITION, . , , parent.getChildViewHolder(view).getOldPosition(), . RecyclerView.NO_POSITION, ChildViewHolder .

, parent.getChildViewHolder(view).getItemViewType()

:

override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
    super.getItemOffsets(outRect, view, parent, state)
    var position = parent?.getChildViewHolder(view)?.adapterPosition
    if (position == RecyclerView.NO_POSITION) {
        val oldPosition = parent?.getChildViewHolder(view)?.oldPosition
        if (oldPosition == RecyclerView.NO_POSITION) return
        position = oldPosition
    }

    val viewType = parent?.getChildViewHolder(view)?.itemViewType

    when (viewType) {
        //do your outRect here
    }
}
+2

spanIndex  (view.layoutParams as?GridLayoutManager.LayoutParams)?.spanIndex

spanSize

(view.layoutParams as? GridLayoutManager.LayoutParams)?.spanSize

, , SpanSizeLookup , ,

. , . .

0

Source: https://habr.com/ru/post/1622594/


All Articles