2
votes

I'm using a CardScrollView to present a stack of content in an immersion app and I don't want to use the standard Card as I'd like more control of the layout.

I have an xml layout file that defines the layout I want but I'm having trouble figuring out how to create a local instance of this layout in the code so that I can get/set its components and add them to my card scroll adapter's list.

Has anyone done this, or know where I could find an example or tutorial or something?

Thanks!

Here's the code I've tried so far (its in the onCreate method of an Activity)

setContentView(R.layout.activity_menu_top);

viewCards = new ArrayList<RelativeLayout>();
TextView tv;

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout_menutop);

tv = (TextView) layout.findViewById(R.id.textSectionTitle);
tv.setText("Appetizers");
tv = (TextView) layout.findViewById(R.id.textPreviousItem);
tv.setText("");
tv = (TextView) layout.findViewById(R.id.textNextItem);
tv.setText("Entress >");
viewCards.add(layout);

   //repeat that last chunk a bunch of times

menuCSV = new CardScrollView(this);
menuCSV.setOnItemClickListener(this);
adapter = new MenuCardScrollAdapter();
menuCSV.setAdapter(adapter);
menuCSV.activate();

setContentView(menuCSV);

The 'TextView' ids I'm referencing are child views of the 'RelativeLayout' as I've defined in my layout xml. I've tried a few variations on the code above and I'm having trouble figuring out the correct way to get and set these elements and then stick them in a List as a single object for the 'CardScrollAdapter'. I'm sure this is supposed to be pretty straightforward but I can't figure out what I'm missing.

Thanks again!

1
Could you please post the code you are using for CardScrollView ?Talha Q

1 Answers

5
votes

Without knowing what your CardScrollAdapter looks like, the general idea would be to do something like this: you can create a subclass of CardScrollAdapter that takes a list of views in its constructor, and then you return the appropriate one inside getView:

private class YourCardScrollAdapter extends CardScrollAdapter {

    private List<RelativeLayout> mViews;

    public YourCardScrollAdapter(List<RelativeLayout> views) {
        mViews = views;
    }

    // ...other methods...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return mViews.get(position);
    }
}

This approach is fine if you have a small number of fixed cards (for example, if you're using the scroller like a menu with a fixed set of options). If you're using more dynamic cards where the content and number might vary based on other factors, you may want to consider inflating and populating the layouts inside getView instead of pre-inflating them. That way, only the view the user is currently looking at is in memory (and a couple on each side, to make scrolling faster), but the rest won't be loaded until they're needed so that resources are better conserved.