Google Glass全屏自定义布局卡

时间:2015-04-19 11:54:03

标签: java android android-layout android-studio google-glass

我有一个问题。由于Card类已弃用且CardBuilder.EMBED_INSIDE相当有限。唯一的选择是使用自定义视图。我还想使用CardScrollView和CardScrollAdapter。

访问Google Glass Immersion Custom Layout without CardBuilder.Layout.EMBED_INSIDE

但我的问题是,我无法提供多种观点。

这是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);
}

这就是我的主要活动课程:

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
    }
} }

1 个答案:

答案 0 :(得分:4)

好的,所以从你的帖子我猜测它只会将你的一个布局膨胀到CardScrollView尝试以下内容。

  1. 创建一个类似于此

    的适配器类
    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);
       }
    }
    
  2. 我的CustomCard类看起来像这样,你可以使用List<Integer>而不是

    public class CustomCard {
        public int getLayout() {
            return layout;
        }
    
        public int layout;
    
        public CustomCard(int layout)
        {
            this.layout = layout;
        }
    }
    
    1. 在您的活动类中,使用所需的布局创建并填充列表,并按如下方式将它们传递给您的适配器。
    2. onCreate()

      中的

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

      并且CreateCards()方法看起来像这样

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

      希望这对你有用