0

Is there a way for the CardScrollView or CardScrollAdapter to let me know when the user moves from one card to another?

Initially I was using the GestureDetector to detect swipes to the left or right, but in Glass applications users can use, e.g., two-finger swipes to quickly pan and move to a distant card (or even voice controls).

There must be a better way to know exactly when a new card is displayed than to track all these directional events.

I know CardScrollView.getSelectedItemPosition() gives me the current card in view, but not exactly when a new card is displayed.

I also thought that CardScrollAdapter.getView() would run every time a new card is displayed, but in Glass applications it runs once in the beginning of the activity execution.

Any help would be great.

Augusto
  • 115
  • 1
  • 10

2 Answers2

0

My Glass is somewhere in my storage so I can't test this now but you can try onDetachedToWindow and onAttachedToWindow. Probably something like this:

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;
}

Also Card lifecycle might give you some idea.

pt2121
  • 11,720
  • 8
  • 52
  • 69
0

The solution is actually in the documentation as suggested by @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.

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

Community
  • 1
  • 1
Augusto
  • 115
  • 1
  • 10