【发布时间】:2018-04-26 15:53:18
【问题描述】:
我的 WearableRecyclerView 线条呈三角形,不像手表的主屏幕那样弯曲。我在我的代码中找不到任何错误。
分享活动:
private void initListView() {
WearableRecyclerView recyclerView = findViewById(R.id.lVDevices);
recyclerView.setEdgeItemsCenteringEnabled(true);
final ScrollingLayoutCallback scrollingLayoutCallback =
new ScrollingLayoutCallback();
adapter = new ShareAdapter(new ShareAdapter.Callback() {
@Override
public void onDeviceClicked(int position, String deviceName) {
onListItemClick(position, deviceName);
}
});
recyclerView.setLayoutManager(
new WearableLinearLayoutManager(this, scrollingLayoutCallback));
recyclerView.setAdapter(adapter);
}
ScrollingLayoutCallback:
public class ScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
private static final float MAX_ICON_PROGRESS = 0.65f;
@Override
public void onLayoutFinished(View child, RecyclerView 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
float 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);
}
}
ListView-XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<android.support.wear.widget.WearableRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lVDevices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:scrollbars="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
行的XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lVDevices_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableStart="@drawable/point_img"
android:layout_centerHorizontal="true"
android:textSize="18sp">
</TextView>
是回调方法的问题吗?我的意思是计算中是否有错误,因为它来自开发者网站。
编辑 1:ScrollingCallback 类中的新数学
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);
child.setScaleX(progresstoCenter);
child.setScaleY(progresstoCenter);
【问题讨论】:
-
我想你需要看看这个SO post。根据帖子中的answer,您的应用程序必须使用
@android:style/Theme.DeviceDefault作为它的主题,以便WatchViewStub将使用正确的矩形/圆形布局。帖子中给出了示例。如需进一步参考,您也可以参考documentation。 -
对不起,但这不起作用。我找到了一个小解决方案,我在编辑 1 下发布了它。这使它更平滑但不是很漂亮。问题是物体在中间非常接近并且非常快速地产生很大的距离。解决办法是使鼻窦变平,但我不知道该怎么做。
-
您有没有找到真正的解决方案?我不知道为什么 Google 会发布
WearableLinearLayoutManager.LayoutCallback代码,因为它看起来一点也不像手表上应用程序列表的动画。真令人沮丧。 -
@KrisB 你呢?你找到解决办法了吗?
-
不,我放弃了,只带了一个平面卷轴。修改这些值只会让它看起来更糟。真希望谷歌能开源 WearOS。
标签: android-recyclerview wear-os vertical-scrolling