Google Glass Immersion Custom Layout without CardB

2020-05-08 06:51发布

问题:

I am trying to build an immersion via a custom XML layout.

My understanding is if I use CardBuilder, I would need to embed_inside but I wanted to use the entire screen with my XML layout.

It appeared that this was previously possible using card however that interface has been depreciated.

I may just be missing a basic Glass concept, but every example or document I have read so far accesses via CardBuilder.Layout and uses the predefined glass layouts.

回答1:

You don't need to use the CardBuilder if you prefer to use your own custom layout: make sure to follow our design guidelines to make sure you meet our specs.

To use your custom layout, simply inflate a View as you would normally and use in your application.

For an Activity, you could something like:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.my_custom_layout);
}

If you are using a CardScrollView, simply modify the CardScrollAdapter's getView method to return your View:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // You can inflate the View using a LayoutInflater: 
    //     LayoutInflater.from(mContext).inflate(R.layout.my_custom_layout, parent);
    return mCustomView;
}


回答2:

I've had this issue myself, and I was able to solve it very easily. There isn't too much documentation about it, which is disappointing.

Since the Card class is deprecated and the CardBuilder.EMBED_INSIDE is fairly limited. The only option you have is to use a custom View, as was mentioned before. But you don't need to manually inflate it! If you use are using the CardScrollView and the CardScrollAdapter. You can do the following in your Activity:

private CardScrollView _cardScroller;
private ArrayList<View> _cardsList;
private MyCustomView _myView;
@Override
protected void onCreate(Bundle bundle) {
    _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); 
}

Now I used a custom CardScrollAdapter because now it has views instead of CardBuilders:

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 0;//should be changed, it's just an example
    }
} 

Now simply create a layout that you want in an xml. And create your custom view: public class MyCustomView extends FrameLayout{

    public MyCustomView (Context context) {
        super(context);
        initView();
    }

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

You can add as many custom views as you like. I hope it helps