在 Google Glass 应用程序中,如何知道用户已移动到新卡片?

How to know a user as moved to a new Card in a Google Glass app?

CardScrollViewCardScrollAdapter 是否有办法让我知道 当用户从一张卡片上移开时另一个?

最初我使用 GestureDetector 来检测向左或向右的滑动,但在 Glass 应用程序中用户可以使用,例如,双指滑动来快速平移和移动到远程卡(甚至语音控制)。

必须有比跟踪所有这些定向事件更好的方法来准确了解新卡片何时显示。

我知道 CardScrollView.getSelectedItemPosition() 让我看到了当前的卡片,但不是新卡片显示的确切时间。

我还认为 CardScrollAdapter.getView() 每次显示新卡片时都会 运行,但在 Glass 应用程序中它 运行在 activity 执行开始时出现一次。

任何帮助都会很棒。

我的 Glass 在我的存储中的某个地方,所以我现在不能测试它,但你可以试试 onDetachedToWindow 和 onAttachedToWindow。大概是这样的:

private View buildView() {
    CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT);

    card.setText(R.string.whatever);

    View view = card.getView();
    view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View v) {

        }

        @Override
        public void onViewDetachedFromWindow(View v) {

        }
    });

    return view;
}

另外 Card lifecycle 可能会给你一些想法。

解决方案实际上在@EntryLevelDev 所建议的文档中

CardScrollView notifies you with the following listener interfaces that are inherited from AdapterView:

AdapterView.OnItemSelectedListener - An item is selected after the user finishes scrolling through the list and settles on an item.

来自:https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/widget/CardScrollView#onAttachedToWindow()