WearableRecyclerView OffsettingHelper.updateChild() 不工作

WearableRecyclerView OffsettingHelper.updateChild() not working

我正在尝试构建一个 RecyclerView,它根据项目与屏幕中心的距离(如设置菜单)来缩放项目,遵循官方 "Android Developers" 的 this tutorial

喜欢这张图片:

问题是 updateChild() 方法根本不会被调用。我尝试使用断点来检查我的偏移助手是否真的设置了,并且在 updateChild 中又设置了一个,但是一切都设置正确但无法正常工作。

这是我的代码:

wear/build.gradle

compile 'com.google.android.support:wearable:2.0.0'
compile 'com.google.android.gms:play-services-wearable:10.2.0'
provided 'com.google.android.wearable:wearable:2.0.0'

MainActivity.java

mRecyclerView = (WearableRecyclerView) findViewById(R.id.recycler);

mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mAdapter = new MainAdapter(this, null);

mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setCenterEdgeItems(true);
mRecyclerView.setOffsettingHelper(new RecyclerOffsettingHelper());

RecyclerOffsettingHelper.java

public class RecyclerOffsettingHelper extends DefaultOffsettingHelper {

    /** How much should we scale the icon at most. */
    private static final float MAX_ICON_PROGRESS = 0.65f;

    private float mProgressToCenter;

    public RecyclerOffsettingHelper() {}

    @Override
    public void updateChild(View child, WearableRecyclerView parent) {
        super.updateChild(child, parent);

        // Figure out % progress from top to bottom
        float centerOffset = ((float) child.getHeight() / 2.0f) /  (float) parent.getHeight();
        float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;

        // Normalize for center
        mProgressToCenter = Math.abs(0.5f - yRelativeToCenterOffset);
        // Adjust to the maximum scale
        mProgressToCenter = Math.min(mProgressToCenter, MAX_ICON_PROGRESS);

        child.setScaleX(1 - mProgressToCenter);
        child.setScaleY(1 - mProgressToCenter);
    }
}

我在教程中有什么遗漏或做错了什么吗? 提前致谢。

经过几天的谷歌搜索和斗争,我找到了原因。

我反编译了 WearableRecyclerView class 并且奇怪地发现没有使用 mOffsettingHelper 方法设置的 mOffsettingHelper 实例,除了在私有 class 命名 OffsettingLinearLayoutManager 扩展 LinearLayoutManager 并将严格设置为构造函数方法上的回收器视图。因此,如果您在代码中设置自己的布局管理器,OffsettingHelper 将无用。

所以我想出了一个解决方法并创建了我自己的布局管理器来覆盖 scrollVerticallyByonLayoutChildren 方法以在 mOffettingHelper 对象上调用 updateChide

这是我自定义的工作代码 LayoutManager:

public class OffsettingLinearLayoutManager extends LinearLayoutManager {
    private WearableRecyclerView mWearableRecyclerView;

    public OffsettingLinearLayoutManager(Context context) {
        super(context);
    }

    public OffsettingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public OffsettingLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void attachRecyclerView(WearableRecyclerView wearableRecyclerView) {
        this.mWearableRecyclerView = wearableRecyclerView;
    }

    public void detachRecyclerView() {
        this.mWearableRecyclerView = null;
    }

    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int scrolled = super.scrollVerticallyBy(dy, recycler, state);
        this.updateLayout();
        return scrolled;
    }

    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        super.onLayoutChildren(recycler, state);
        if(this.getChildCount() != 0) {
            this.updateLayout();
        }
    }

    private void updateLayout() {
        if(mWearableRecyclerView != null && mWearableRecyclerView.getOffsettingHelper() != null) {
            for(int count = 0; count < this.getChildCount(); ++count) {
                View child = this.getChildAt(count);
                mWearableRecyclerView.getOffsettingHelper().updateChild(child, mWearableRecyclerView);
            }
        }
    }
}

注意:不要忘记使用attachRecyclerView方法来设置你的WearableRecyclerView实例。

希望对大家有所帮助。