WearableRecyclerView OffsettingHelper.updateChild()不起作用

时间:2017-03-20 14:26:03

标签: android android-recyclerview wear-os android-wear-2.0

我正在尝试构建一个RecyclerView,根据他们与屏幕中心的距离来设置项目,例如设置菜单,跟随官方“Android开发者”的this tutorial

喜欢这张图片:

enter image description here

问题是updateChild()方法永远不会被调用。我尝试使用断点来检查我的偏移助手是否实际设置了,并且在updateChild中还有一个,但是一切都设置正确但不起作用。

这是我的代码:

穿/的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);
    }
}

我有什么错过教程或做错了吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

经过几天的谷歌搜索和斗争,我明白了。

我反编译了WearableRecyclerView类,奇怪地发现没有使用mOffsettingHelper方法设置的setOffsettingHeler()实例,除了名为OffsettingLinearLayoutManager的私有类扩展LinearLayoutManager并将严格设置为Constructor方法上的recycleler视图。因此,如果您在代码中设置了自己的布局管理器,则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个实例。

希望它对某人有帮助。