我怎样才能像android wear 2.0那样为round watch创建循环列表?.
这样地:
循环列表显示在android wear应用程序启动程序中。
最佳答案
首先,你需要用一个可穿戴的RecyclerView替换你的ListView。
它可以像普通的listview一样使用。但要确保从android.support.wear.widget
中导入正确的一个。不要使用android.support.wearable.view
中的一个。这个应该划掉,这样你就不用花很长时间来检查你是否用对了。如果只有一个WearableRecyclerService可供选择,请确保将compile 'com.android.support:wear:27.0.0'
添加到build.gradle(wear)文件中的依赖项中。
还要确保在activity.xml中使用<android.support.wear.widget.WearableRecyclerView/>
。如果您只想要一个没有任何自定义项缩放的循环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);
}
}
然后返回onLayoutFlated()方法,并键入以下内容:
CustomScrollingLayoutCallback customScrollingLayoutCallback = new CustomScrollingLayoutCallback();
your_recycler_view.setLayoutManager(new WearableLinearLayoutManager(your_context, customScrollingLayoutCallback));
your_recycler_view.setCircularScrollingGestureEnabled(true);
完成。