【问题标题】:WearableRecyclerView OffsettingHelper.updateChild() not workingWearableRecyclerView OffsettingHelper.updateChild() 不工作
【发布时间】:2017-03-20 14:26:02
【问题描述】:

我正在尝试构建一个RecyclerView,它可以根据项目与屏幕中心的距离(如设置菜单)来缩放项目,遵循官方“Android 开发者”的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 作为相关XML 布局中的主容器,并将setCenterEdgeItems(boolean) 方法设置为true。如果有的话,你也可以添加日志。
  • @Mr.Rebot 是的,我使用 Wea​​rableRecyclerView 作为主容器,还使 setCenterEdgeItems(boolean) 为真,就像我添加的代码一样,但没有运气。而且恐怕没有重要的日志或错误要添加:(
  • 我现在也遇到了同样的问题,你解决了吗?
  • @Marc 查看我发布的答案。

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


【解决方案1】:

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

我反编译了WearableRecyclerView 类,但奇怪的是没有使用setOffsettingHeler() 方法设置的mOffsettingHelper 实例,除了在一个名为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 实例。

希望它对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多