2

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!

Jennifer
  • 21
  • 1
  • 4

1 Answers1

5

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.

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • Thanks for the comment Tony, but I don't think the adapter is the issue (what you suggest is what I already had). The issue seems to be with getting the layout xml resource. If I don't use it to set up my cards, everything runs just fine (though it looks terrible because I'm not setting any layout parameters). When I try to set the layout and its components with the xml I get various runtime exceptions because its not finding the resources or doesn't like something about how I'm setting them up. Any thoughts on how to get a layout resource for use by something other than the Activity?? – Jennifer Jan 11 '14 at 19:19
  • 2
    Is the code in your original post all in the same method? If so, it looks like you're calling `setContentView` multiple times, which will cause problems since that sets the layout for the entire activity. It sounds like what you want to be doing is to call `getLayoutInflater()` to get the `LayoutInflater` service, then call its `inflate` method to turn your XML resource into a view hierarchy. Calling that method once for each card will give you independent view hierarchies so you can set individual fields to different values and then put the top-level views into your adapter. – Tony Allevato Jan 12 '14 at 02:36