如何像在 android wear 2.0 中一样为圆形手表创建圆形列表?
像这样:
在 android wear 应用程序启动器中可以看到循环列表。
首先,您需要将 ListView 替换为WearableRecyclerView。它可以像普通的 ListView 一样使用。但请确保,您从android.support.wear.widget
. 不要使用来自android.support.wearable.view
. 这个应该被划掉,所以你不会花很长时间来检查你是否使用了正确的。如果只有一个 WearableRecyclerView 可供选择,请确保添加compile 'com.android.support:wear:27.0.0'
到build.gradle (wear) 文件中的依赖项。还要确保您<android.support.wear.widget.WearableRecyclerView/>
在activity.xml中使用。如果您只想要一个没有任何自定义项目缩放的圆形 ListView,只需在您的onLayoutInflated()
方法中调用它:
your_recyclerview.setEdgeItemsCenteringEnabled(true);
your_recyclerview.setLayoutManager(new WearableLinearLayoutManager(your_activity_context));
如果您想让项目在靠近屏幕中心时按比例放大,事情会变得更复杂一些。
首先:将其粘贴到您的 Activity.java 中:
private class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
private static final float MAX_ICON_PROGRESS = 2F;
@Override
public void onLayoutFinished(View child, RecyclerView parent) {
float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight();
float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;
float progresstoCenter = (float) Math.sin(yRelativeToCenterOffset * Math.PI);
float mProgressToCenter = Math.abs(0.5f - yRelativeToCenterOffset);
mProgressToCenter = Math.min(mProgressToCenter, MAX_ICON_PROGRESS);
child.setScaleX(1 - mProgressToCenter);
child.setScaleY(1 - mProgressToCenter);
child.setX(+(1 - progresstoCenter) * 100);
}
}
然后回到你的onLayoutInflated()方法,输入以下内容:
CustomScrollingLayoutCallback customScrollingLayoutCallback = new CustomScrollingLayoutCallback();
your_recycler_view.setLayoutManager(new WearableLinearLayoutManager(your_context, customScrollingLayoutCallback));
your_recycler_view.setCircularScrollingGestureEnabled(true);
完毕。
这现在可以通过 Android Wear 2.0 实现WearableRecyclerView
。
Wear 2.0 引入了
WearableRecyclerView
用于显示和操作针对圆形显示优化的垂直项目列表的类。WearableRecyclerView
扩展现有RecyclerView
类以在可穿戴应用程序中提供弯曲布局和圆形滚动手势。
您可能想了解更多关于Android Wear 2.0 Preview 3的信息。