Search code examples
google-glassgoogle-gdk

scrolling through xml layouts


I'm trying to use layout files instead of the cardbuilder. Currently I have my first card inflated. This would be the card that is being used as card when you start the application.

Now when you scroll to the left I want to show my next card. However I can't seem to find how I would achieve this when I'm using layout files. Does anyone have experience with this? This for a immersion btw.

this is the code I used to show my first card.

 @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View convertView;
            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.farmhouse_main, viewGroup);

            return convertView;
        }

Edit

After looking at the code that @EntryLevelDev submitted I wrote my own CardScrollAdapter I did this in a way to make sure that you only have to make changes in one method when you want to insert or delete a certain card.

My adapter

public class MainAdapter extends CardScrollAdapter {

private List<CustomCard> mCards;
private LayoutInflater inflate;
public MainAdapter(List<CustomCard> cards, LayoutInflater inf)
{
    this.mCards = cards;
    this.inflate = inf;
}

@Override
public int getCount() {
    return mCards.size();
}

@Override
public Object getItem(int i) {
    return mCards.get(i);
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    int card = mCards.get(i).getContent();
    view = inflate.inflate(card, viewGroup, false);

    return view;
}

@Override
public int getPosition(Object o) {
    return this.mCards.indexOf(o);
}
}

my CustomCard class

public class CustomCard {
public int content;


public CustomCard(int content)
{
    this.content = content;
}

public int getContent() {
    return content;
}
}

Then in my activity I made a method to add the cards to the list and called this method in my onCreate() before setting the adapter.

public void CreateCards()
{
    mCards.add(new CustomCard(R.layout.card_simple_layout));
    mCards.add(new CustomCard(R.layout.main_farmhouse));
}

Would this be considered good practice or should I be going a different way? Please keep in mind that this is just a sample project and I used the project EntryLevelDev used to play around with it.


Solution

  • You can use CardScrollView for that.

    Please see the GDK doc:

    In onCreate:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // create the CardScrollView set the adapter in onCreate
        mCardScrollView = new CardScrollView(this);
        mCardScrollView.setAdapter(new YourScrollAdapter());
        // ...
    }
    

    The adapter (Here I use my card_simple_layout layout.):

    See the doc for more complete example of CardScrollAdapter

    protected class YourScrollAdapter extends CardScrollAdapter {
    
        //TODO override other CardScrollAdapter methods
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView;
                if (view == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    view = inflater.inflate(R.layout.card_simple_layout, parent, false);
                    ViewHolder viewHolder = new ViewHolder();
                    viewHolder.text = (TextView) view.findViewById(R.id.content);
                    view.setTag(viewHolder);
                }
                ViewHolder holder = (ViewHolder) view.getTag();
                holder.text.setText(mOpeningDescription);
                return view;
        }
    
    }
    
    static class ViewHolder {
        public TextView text;
    }
    

    Edit 1:

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == 0) {
                View view = convertView;
                if (view == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    view = inflater.inflate(R.layout.card_simple_layout, parent, false);
                    ViewHolder viewHolder = new ViewHolder();
                    viewHolder.text = (TextView) view.findViewById(R.id.content);
                    view.setTag(viewHolder);
                }
                ViewHolder holder = (ViewHolder) view.getTag();
                holder.text.setText(mOpeningDescription);
                return view;
            } else if (position == 1) {
                return mContentView;
            }
            return null;
        }
    

    My mContentView is from the CardBuilder which uses another layout. So now we have 2 cards: one with a custom layout and the other from CardBuilder.

    See a full example on Github.