Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine appearing view in LayoutManager pre-layout

I wanna support predictive animations in my custom LayoutManager when item is moved from outside of visible screen bounds to visible point.

All filling operations i do in the onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state).

According to a documentation to support predictive animations in pre-layout stage (state.isPreLayout()) i should set up the initial conditions for the change animation (e.g. place appearing views somewhere)

The problem is that i can't find a way to determine in pre-layout which views are going to be moved from outside, because i'm able to operate only with current attached to RecyclerView views and onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCount) method is called after pre-layout stage. (For example onItemsRemoved is called before pre-layout)

Is it the bug with LayoutManager or could i somehow determine in pre-layout which views are going to be moved soon?

PS: i'm able to maintain prediction animation from visible point to outside, because i'm able to loop through visible views and determine with recycler.convertPreLayoutPositionToPostLayout which ones are going to be moved.

//childViews is an iterable over RecyclerView items
for (View view : childViews) {
    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams();

    boolean probablyMovedFromScreen = false;

    if (!lp.isItemRemoved()) {
        //view won't be removed, but maybe it is moved offscreen
        int pos = lp.getViewLayoutPosition();

        //start view is a first visible view on screen
        int lowestPosition = getPosition(startView);
        int highestPosition = getPosition(endView);

        pos = recycler.convertPreLayoutPositionToPostLayout(pos);
        probablyMovedFromScreen = pos < lowestPosition || pos > highestPosition;
    }

    if (probablyMovedFromScreen) {
        //okay this view is going to be moved
    }

}

This article have helped me a lot, but it also doesn't describe an animation which i need.

PPS: LinearLayoutManager doesn't support such animation also. (there is just simple fade-in animation)

like image 945
Beloo Avatar asked Nov 09 '16 15:11

Beloo


1 Answers

You don't know which items will be visible but you know which items will go away (or changed) so based on that, you can estimate how much space is necessary in which direction. You can check the LinearLayoutManager's code to see how it works. You can also read these articles about the details of the RecyclerView system.

http://www.birbit.com/recyclerview-animations-part-1-how-animations-work http://www.birbit.com/recyclerview-animations-part-2-behind-the-scenes

like image 123
yigit Avatar answered Nov 11 '22 04:11

yigit