我的WearableRecyclerView,线条像三角形一样弯曲,而不像手表的主屏幕那样弯曲。我在代码中找不到任何错误。
ShareActivity:
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);
我已经创建了一个解决方案。
[像OP,我从不喜欢Android官方文档中提供的解决方案,该解决方案使列表元素沿三角形路径移动(从顶部中心到左中间的直线,然后从那里到底部的直线中心)。
解决方案很简单,使用trig将路径更改为半圆形。将CustomScrollingLayoutCallback替换为此:
class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
@Override
public void onLayoutFinished(View child, RecyclerView parent) {
final float MAX_ICON_PROGRESS = 0.65f;
try {
float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight();
float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;
// Normalize for center, adjusting to the maximum scale
float progressToCenter = Math.min(Math.abs(0.5f - yRelativeToCenterOffset), MAX_ICON_PROGRESS);
// Follow a curved path, rather than triangular!
progressToCenter = (float)(Math.cos(progressToCenter * Math.PI * 0.70f));
child.setScaleX (progressToCenter);
child.setScaleY (progressToCenter);
} catch (Exception ignored) {}
}
}
[倒数第四行的“ ... * 0.70f”是通过反复试验确定的,并更改了该曲线以使其与官方Android Wear曲线最匹配。