1

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

像这张图片:

在此处输入图像描述

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

这是我的代码:

磨损/构建.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);
    }
}

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

4

1 回答 1

0

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

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

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

这是我的自定义的工作代码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实例。

希望它可以帮助某人。

于 2017-03-25T08:54:16.640 回答