With Glass, you can build rich interactions with your cards such as scrolling and animations.
The Glass display and touchpad are great for displaying swipable cards, like in the Glass timeline. If you’re building an activity, you can create the same type of effect with the CardScrollView
widget.
CardScrollAdapter
to supply cards to the CardScrollView
. You can build a standard view hierarchy yourself or use the CardBuilder
class.CardScrollView
that uses the CardScrollAdapter
[TODO: ADD LINK] as the supplier for cards.CardScrollView
[TODO: ADD LINK] or display the CardScrollView
[TODO: ADD LINK] in a layout.Here’s a simple implementation that scrolls through three cards:
public class CardScrollActivity extends Activity {
private List<CardBuilder> mCards;
private CardScrollView mCardScrollView;
private ExampleCardScrollAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createCards();
mCardScrollView = new CardScrollView(this);
mAdapter = new ExampleCardScrollAdapter();
mCardScrollView.setAdapter(mAdapter);
mCardScrollView.activate();
setContentView(mCardScrollView);
}
private void createCards() {
mCards = new ArrayList<CardBuilder>();
mCards.add(new CardBuilder(this, CardBuilder.Layout.TEXT)
.setText("This card has a footer.")
.setFootnote("I'm the footer!"));
mCards.add(new CardBuilder(this, CardBuilder.Layout.CAPTION)
.setText("This card has a puppy background image.")
.setFootnote("How can you resist?")
.addImage(R.drawable.puppy_bg));
mCards.add(new CardBuilder(this, CardBuilder.Layout.COLUMNS)
.setText("This card has a mosaic of puppies.")
.setFootnote("Aren't they precious?")
.addImage(R.drawable.puppy_small_1);
.addImage(R.drawable.puppy_small_2);
.addImage(R.drawable.puppy_small_3));
}
private class ExampleCardScrollAdapter extends CardScrollAdapter {
@Override
public int getPosition(Object item) {
return mCards.indexOf(item);
}
@Override
public int getCount() {
return mCards.size();
}
@Override
public Object getItem(int position) {
return mCards.get(position);
}
@Override
public int getViewTypeCount() {
return CardBuilder.getViewTypeCount();
}
@Override
public int getItemViewType(int position){
return mCards.get(position).getItemViewType();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mCards.get(position).getView(convertView, parent);
}
}
}
Since CardScrollView
[TODO: ADD LINK] extends AdapterView
you can implement the standard Android listeners.
setOnItemClickListener()
on your CardScrollView
.onItemClick()
handler for the tap event.Here is an extension to the previous example that plays a tap sound when you tap on a card:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
setupClickListener();
setContentView(mCardScrollView);
}
private void setupClickListener() {
mCardScrollView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.TAP);
}
});
}
There are three animations available for scrolling cards: Navigation, Insertion, and Deletion.
animate()
[TODO: ADD LINK] and use a value from the CardScrollView.Animation
[TODO: ADD LINK] enum.notifyDataSetChanged()
. The animate()
[TODO: ADD LINK] method handles updating your data set view.private class ExampleCardScrollAdapter extends CardScrollAdapter {
...
// Inserts a card into the adapter, without notifying.
public void insertCardWithoutNotification(int position, CardBuilder card) {
mCards.add(position, card);
}
}
private void insertNewCard(int position, CardBuilder card) {
// Insert new card in the adapter, but don't call
// notifyDataSetChanged() yet. Instead, request proper animation
// to inserted card from card scroller, which will notify the
// adapter at the right time during the animation.
mAdapter.insertCardWithoutNotification(position, card);
mCardScrollView.animate(position, CardScrollView.Animation.INSERTION);
}
Keep in mind the following design and performance implications when creating card scrollers.
To increase performance, a CardScrollView
[TODO: ADD LINK] only loads a subset of the cards that a CardScrollAdapter
[TODO: ADD LINK] provides (generally, the ones that are visible to the user, and a few more). Because of this, a card can be in any of these four general states:
onDetachedToWindow()
[TODO: ADD LINK] method if a card was previously attached and then detached.getView()
, because the card is close to being “activated.” You are notified by the card’s onAttachedToWindow()
[TODO: ADD LINK] method when this happens.true
in this case.getSelectedView()
[TODO: ADD LINK] returns the currently selected card. The isSelected()
method returns true in this case.If you are animating your card’s view or doing other costly operations, start and stop the operations in onAttachedToWindow()
[TODO: ADD LINK] and onDetachedToWindow()
[TODO: ADD LINK] to save resources.
When a card goes from being attached to detached, the view object associated with the card can be recycled and used by a card that is being attached. Recycling views with updated information is much more efficient than creating new views.
To take advantage of card recycling, implement the getItemViewType()
, getViewTypeCount()
[TODO: ADD LINK], and getView()
[TODO: ADD LINK] methods of the CardScrollAdapter
[TODO: ADD LINK] class. You then use some of the convenience methods in the CardBuilder
[TODO: ADD LINK] class to implement recycling in your CardScrollAdapter
[TODO: ADD LINK], like in the following example:
private List<CardBuilder> mCards;
...
/**
* Returns the number of view types for the CardBuilder class. The
* CardBuilder class has a convenience method that returns this value for
* you.
*/
@Override
public int getViewTypeCount() {
return CardBuilder.getViewTypeCount();
}
/**
* Returns the view type of this card, so the system can figure out
* if it can be recycled. The CardBuilder.getItemViewType() method
* returns it's own type.
*/
@Override
public int getItemViewType(int position){
return mCards.get(position).getItemViewType();
}
/**
* When requesting a card from the adapter, recycle the view if possible.
* The CardBuilder.getView() method automatically recycles the convertView
* it receives, if possible, or creates a new view if convertView is null or
* of the wrong type.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mCards.get(position).getView(convertView, parent);
}
When a card is selected and being displayed to users, you might not want changes to the underlying adapter to affect the card that users see at that moment. For example, if a user is viewing a selected card, and a card is removed to the left of that card, the card the user is viewing can potentially shift to the left, because the CardScrollAdapter
[TODO: ADD LINK] reassigns IDs to the underlying data set when changes occur, by default.
If it logically makes sense to assign your cards unique IDs, you can maintain a consistent ID in the underlying data set to prevent the aforementioned problem. To do this, override hasStableIds()
and return true
. This specifies to the system that the CardScrollAdapter
[TODO: ADD LINK] maintains stable IDs across data set changes. In addition, implement getItemId()
[TODO: ADD LINK] to return the appropriate unique ID for the cards in your adapter. The default implementation returns the position index of the card in the adapter, which is inherently unstable.
When you have an empty data set for adapters, the default view is to show a black screen. If you want to show a different view in these cases, do not use setEmptyView()
[TODO: ADD LINK]. Instead, create a single card in your CardScrollAdapter
[TODO: ADD LINK].
Many built-in immersions on Glass provide “tugging” feedback when swiping backward and forward don’t perform an action. For example, you can see this feedback when swiping after taking a photo.
If your immersion does not use horizontal swipe gestures to perform application-specific functions, provide this tugging effect by wrapping your layout inside a CardScrollView
[TODO: ADD LINK] that contains one card.
public class TuggableView extends CardScrollView {
private final View mContentView;
/**
* Initializes a TuggableView that uses the specified layout
* resource for its user interface.
*/
public TuggableView(Context context, int layoutResId) {
this(context, LayoutInflater.from(context)
.inflate(layoutResId, null));
}
/**
* Initializes a TuggableView that uses the specified view
* for its user interface.
*/
public TuggableView(Context context, View view) {
super(context);
mContentView = view;
setAdapter(new SingleCardAdapter());
activate();
}
/**
* Overridden to return false so that all motion events still
* bubble up to the activity's onGenericMotionEvent() method after
* they are handled by the card scroller. This allows the activity
* to handle TAP gestures using a GestureDetector instead of the
* card scroller's OnItemClickedListener.
*/
@Override
protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
super.dispatchGenericFocusedEvent(event);
return false;
}
/** Holds the single "card" inside the card scroll view. */
private class SingleCardAdapter extends CardScrollAdapter {
@Override
public int getPosition(Object item) {
return 0;
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int position) {
return mContentView;
}
@Override
public View getView(int position, View recycleView,
ViewGroup parent) {
return mContentView;
}
}
}
onCreate
method in your activity to display the CardScrollView
that contains your layout.@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// was: setContentView(R.layout.main_activity);
setContentView(new TuggableView(this, R.layout.main_activity));
}
Portions of this page are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 4.0 Attribution License.