java - 确定 LayoutManager 预布局中出现的 View

标签 java android android-layout android-recyclerview android-animation

我想在 my custom LayoutManager 中支持预测动画当项目从可见屏幕边界外移动到可见点时。

我在 onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) 中执行的所有填充操作。

根据在预布局阶段支持预测动画的文档(state.isPreLayout()),我应该设置更改动画的初始条件(例如,在某处放置出现的 View )

问题是我无法在预布局中找到一种方法来确定哪些 View 将从外部移动,因为我只能在当前附加到 RecyclerView View 和 onItemsMoved( RecyclerView recyclerView, int from, int to, int itemCount) 方法在预布局阶段之后被调用。 (例如 onItemsRemoved 被称为 before pre-layout)

LayoutManager 的错误还是我能以某种方式 在预布局中确定哪些 View 将很快移动?

PS:我能够保持从可见点到外部的预测动画,因为我能够遍历可见 View 并使用 recycler.convertPreLayoutPositionToPostLayout 确定哪些将被移动。

//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对我帮助很大,但它也没有描述我需要的动画。

PPS: LinearLayoutManager 也不支持这样的动画。 (只有简单的淡入动画)

最佳答案

您不知道哪些项目将可见,但您知道哪些项目将消失(或更改),因此您可以基于此估计在哪个方向上需要多少空间。您可以检查 LinearLayoutManager 的代码以了解其工作原理。 您还可以阅读这些关于 RecyclerView 系统细节的文章。

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

关于java - 确定 LayoutManager 预布局中出现的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40510675/

相关文章:

javascript - Canvas js 导出启用无法在 Android 设备上运行

java - 按屏幕尺寸更改 LinearLayout

java - Eclipse错误: Target runtime Apache Tomcat v7. 0未定义

java - Executor shutdownNow 任务被阻止?

android - SQLCipherUtils.getDatabaseState 中带有 Room Persistence Library 异常的 SQLCipher

android - 相同的布局在 Samsung Galaxy Tab 上看起来不同

android - 查看覆盖操作工具栏?

java - HttpURLConnection 的响应与curl 响应不同

java - FastSineTransformer - 用零填充数组以适应长度

android - 在用户语言环境中显示日期和时间