我正在尝试构建一个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);
}
}
我在教程中有什么遗漏或做错了吗?提前致谢。