2
votes

When a card is selected in a CardScrollView, the onItemSelected() method is called. However, I cannot determine how to add an image to this card once selected. For now, I've done a kluge callback mechanism where I call updateViews(true):

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (position == mSelectedPosition)
        return;
    mSelectedPosition = position;
    if (mCardScrollView != null)
        mCardScrollView.updateViews(true);
}

Then in the adapter getView() I check if the item is selected, and if so load the image asynchronously:

if (mCardScrollView != null && mCardScrollView.getSelectedItemPosition() == pos) {
    String url = String.format(Board.THUMBNAIL_FORMAT, board, tim);
    CardImageLoader.loadCardImage(card, Card.ImageLayout.FULL, url, mCardScrollView);
}

The CardImageLoader then downloads the file and does yet another call to updateViews() after the download is successful, finally calling card.addImage() and the image displays.

This works, but can make things slow at times, and sometimes unresponsive. How to do it better?

UPDATE: Don't use a Card when you need to load a list of cards with images in a CardScrollView. Instead, use your own custom layout to emulate a card and use a standard image loader to do the loading. This works much better even with very long lists.

Google please make the card layout XMLs open so we can use them.

1
If you create a custom layout instead of using a card, can you still use CardScrollView? I'm guessing the answer is yes, because it probably just converts the cards into views, correct? Just wanted to confirm. Thanks!Synergy807
Yes, it works, it uses the view layout you give.johnarleyburns

1 Answers

0
votes

If you've kept hold of the list of cards you initialize the CardScrollView with (i.e. mCards from https://developers.google.com/glass/develop/gdk/ui/theme-widgets#creating_scrolling_cards_in_activities) and then just get the card from that position in the list, update it and call .invalidate() on it to refresh the view, that should work from onItemSelected.