Skip to content

Most visited

Recently visited

navigation

创建列表

在穿戴式设备上,用户可通过列表轻松地从一组选项中选择一个项目。Wearable 界面库包含 WearableRecyclerView 类,它是用于创建针对穿戴式设备优化的列表的 RecyclerView 实现。在穿戴式设备应用中,您可以通过创建一个新的 WearableRecyclerView 容器来适应此界面。

您应基于您要提供的用户体验决定是否使用 WearableRecyclerView。对于简单项目长列表,例如应用启动器或联系人列表,我们建议使用 WearableRecyclerView。每个项目都可能具有一个短字符串和一个关联图标。或者,每个项目都可能只有一个字符串或图标。对于非常短或复杂的列表,我们不建议使用 WearableRecyclerView。在这种情况下,可使用常规 Android 支持库中的 RecyclerView 或 ListView。

通过扩展现有 RecyclerView 类,默认情况下,WearableRecyclerView API 在一个垂直列表中显示垂直滚动项目列表。在穿戴式设备应用中,您可以使用 WearableRecyclerView API 选择曲线布局和循环滚动手势。

图 1. Android Wear 上的默认列表视图。

本课程将向您介绍在 Android Wear 应用中如何使用 WearableRecyclerView 类创建列表。本文档还将介绍如何针对可滚动项目选择曲线布局、启用循环滚动手势,以及自定义滚动时子项的外观。

使用 XML 将 WearableRecyclerView 添加到 Activity

以下布局将 WearableRecyclerView 添加到 Activity,以便在圆形设备和方形设备上正确显示此列表。

:在穿戴式设备支持库中, WearableRecyclerView 类取代了一个与其类似且已弃用的类。

res/layout/activity_main.xml

<android.support.wear.widget.WearableRecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_launcher_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

然后,对您的 Activity 应用布局:

import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wear.widget.WearableRecyclerView

public class MainActivity extends WearableActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    ...
}

创建曲线布局

要在穿戴式设备应用中创建适用于可滚动项目的曲线布局,请执行以下操作:

// To align the edge children (first and last) with the center of the screen
mWearableRecyclerView.setEdgeItemsCenteringEnabled(true);
...

mWearableRecyclerView.setLayoutManager(
                new WearableLinearLayoutManager(this));

如果应用具有自定义滚动时子项外观的特定要求(例如,在项目偏离中心时缩放图标和文本),则扩展 WearableLinearLayoutManager.LayoutCallback 类并重写 onLayoutFinished 函数。

以下代码段展示了如何通过扩展 WearableLinearLayoutManager.LayoutCallback 类自定义项目滚动以在远离中心时进行缩放:

public class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
    /** How much should we scale the icon at most. */
    private static final float MAX_ICON_PROGRESS = 0.65f;

    private float mProgressToCenter;

    @Override
    public void onLayoutFinished(View child, RecyclerView parent) {

        // Figure out % progress from top to bottom
        float centerOffset = ((float) child.getHeight() / 2.0f) / (float) mParentView.getHeight();
        float yRelativeToCenterOffset = (child.getY() / mParentView.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);
    }
}

CustomScrollingLayoutCallback customScrollingLayoutCallback =
                new CustomScrollingLayoutCallback();
mWearableRecyclerView.setLayoutManager(
                new WearableLinearLayoutManager(this, customScrollingLayoutCallback));

添加循环滚动手势

默认情况下,循环滚动在 WearableRecyclerView 中已停用。如果要在子视图中启用循环滚动手势,请使用 WearableRecyclerViewsetCircularScrollingGestureEnabled() 函数。您还可以通过定义以下一个或两个值来自定义循环滚动手势:

以下代码段演示如何设置这些函数:

setCircularScrollingGestureEnabled(true);
setBezelFraction(0.5f);
setScrollDegreesPerScreen(90);
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)