2

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.

TheUnknown
  • 50
  • 5

2 Answers2

1

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.

pt2121
  • 11,720
  • 8
  • 52
  • 69
  • so we meet again lol. But if I understand it corretly this changes the text on the card instead of changing the entire layout. What I meant was going from `Layout A` to `layout B` like there is a completely other layout not just different text. I'm sorry I wasn't clear enough in my start post – TheUnknown Mar 11 '15 at 15:08
  • You can change the whole layout based on the card position by putting if-case in the getView. I'll edit my answer. – pt2121 Mar 11 '15 at 15:15
  • @TheUnknown you can check my project at https://github.com/prt2121/scrolling-card. Hope it's what you want! – pt2121 Mar 12 '15 at 02:38
  • I just ahd a chance to take a look at it. Yes and no. I see you used a combination of the builder and a layout. Lets say I want to add a third card that is also a layout now. I can't seem to get that working – TheUnknown Mar 12 '15 at 07:57
  • I just got it working with adding a third layout. However I'm gonna need to see if I can make this a lil prettier so it's easier to add or delete a card without having to change a bunch of code – TheUnknown Mar 12 '15 at 08:03
  • Yeah i know it's kinda ugly. I made it up quickly. I will improve the code when i have a chance. – pt2121 Mar 12 '15 at 11:51
  • I made my own solution to this. I edited my start post please take a look when you have time. – TheUnknown Mar 12 '15 at 12:02
  • Ok that looks a lot better! – pt2121 Mar 12 '15 at 12:09
0

I am pretty sure you can have a card inflated using LayoutInflater in getView & ViewHolder correspondingly as a traditional approach in Android Developement, But even it is a fact that there will be a major performance issue doing this. The scrolling wont be that smooth and also there may heating in Google Glass.

I would also recommend you to use CardScroller, CardScrollAdapter & CardScrollView for better performance.

However, a thumbsup for your creativity!!

AniV
  • 3,997
  • 1
  • 12
  • 17