1

I have an issue. Since the Card class is deprecated and the CardBuilder.EMBED_INSIDE is fairly limited. The only option is to use a custom View. I'd also like to use the CardScrollView and the CardScrollAdapter.

visit Google Glass Immersion Custom Layout without CardBuilder.Layout.EMBED_INSIDE

But my problem is, I can't have multiple views.

Here is MyCustomViewClass:

   public class MyCustomView extends FrameLayout{
public MyCustomView (Context context) {
    super(context);
    initView();
}

private void initView()
{
    View view = inflate(getContext(), R.layout.imageview, null);
    addView(view);

    View view2 = inflate(getContext(), R.layout.secondview, null);
    addView(view2);
}

And thats my main activity class:

public class InspectionActivity extends Activity {

private CardScrollView mCardScroller;
private GestureDetector mGestureDetector;
private View mView;
private CardScrollView _cardScroller;
private ArrayList<View> _cardsList;
private MyCustomView _myView;
protected List<CardBuilder> mCards;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    createCards();
    _cardsList = new ArrayList<View>();
    _myView= new MyCustomView (this);
    _cardsList.add(_myView);
    _cardScroller = new CardScrollView(this) ;
    MainCardsScrollAdapter adapter = new MainCardsScrollAdapter(_cardsList);
    _cardScroller.setAdapter(adapter);
    _cardScroller.activate();
    setContentView(_cardScroller);
}

private void createCards() {
    mCards = new ArrayList<CardBuilder>();
}
public class MainCardsScrollAdapter extends CardScrollAdapter
{
    ArrayList<View> _cardsList;
    public MainCardsScrollAdapter(ArrayList<View> cardsList)
    {
        _cardsList = cardsList;
    }

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

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


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return _cardsList.get(i);
    }

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

    @Override
    public int getViewTypeCount() {
        return CardBuilder.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position){
        return 2;//should be changed, it's just an example
    }
} }
Community
  • 1
  • 1

1 Answers1

4

Alright so from your post I'm guessing it's only inflating one of your layouts into the CardScrollView try the following.

  1. Create a adapter class that looks something like this

    public class mainAdapter extends CardScrollAdapter {
    
       private List<CustomCard> mCards;
       private LayoutInflater inflater;
    
       public mainAdapter(List<CustomCard> cards, LayoutInflater inf)
       {
           this.mCards = cards;
           this.inflater = 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).getLayout();
           view = inflater.inflate(card, viewGroup, false);
           return view;
       }
    
       @Override
       public int getPosition(Object o) {
           return this.mCards.indexOf(o);
       }
    }
    

my CustomCard class looks like this, you could just use a List<Integer> instead tho

public class CustomCard {
    public int getLayout() {
        return layout;
    }

    public int layout;

    public CustomCard(int layout)
    {
        this.layout = layout;
    }
}
  1. In your activity class create and fill a list with the desired layouts and pass them to your adapter as follows.

in the onCreate()

CreateCards();
mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(new mainAdapter(mCards, getLayoutInflater()));

and the CreateCards() method would look something like this

public void CreateCards() {
        mCards.add(new CustomCard(R.layout.firstview));
        mCards.add(new CustomCard(R.layout.secondview));
        mCards.add(new CustomCard(R.layout.thirdview));
    }

Hope this is of use for you

NoSixties
  • 2,443
  • 2
  • 28
  • 65
  • thank you for this. My approach was to use card builder with EMBED_LAYOUT. I faced a strange issue that my card adapter showed the wrong layouts when I had more then 6 cards in it. I would assume a bug in the CardBuilder. With your solution it worked like a charme. Since this layout has no footer, you might want to look at the given XML Layout here for padding and stuff: https://developers.google.com/glass/develop/gdk/card-design – Anthea Dec 09 '15 at 09:02