سؤال

I'm trying to preview a ListFragment in the Graphical Layout Editor. As it has no fragment layout to preview, I simply created an xml with a ListView inside, just to see how the items look, and set Preview List Content with one of the default item layouts, just to try.

The problems are:

  1. The list in the preview is empty, no matter what Preview List Content I choose; (I don't know what I was doing wrong, now it works)
  2. I want to preview my own custom list item View, not just a layout, because I'll be doing few things when isInEditMode() is true.

Any idea?

EDIT: The fragment is a ListFragment, but I can live with using a "temporary" listview.xml layout. What I want is a way to see a listview with my CustomListItemView inside. In the fragment, I'll place them there with a custom adapter; in the layout editor I wish I could choose CustomListItemView from Preview List Content menu.

I can't just use the custom item's layout as Preview List Content, because its root is a <merge> (my custom item extends RelativeLayout). This could be solved by changing for a moment <merge> into <RelativeLayout>. However, this custom item view changes depending on data, so I'd love to see a preview of the various states, by using the isInEditMode() facility.

The best would be if it all could work without the temporary listview.xml layout, but this is becoming a feature request.

Right now I'm resorting to use a vertical LinearLayout.

هل كانت مفيدة؟

المحلول

I took a look, and it looks like the answer is that this isn't possible today, because of two bugs.

  1. For some reason, ListView preview doesn't work if the ListView is the root element of the layout, rather than being embedded within the layout. The workaround for this is simple; just surround the ListView with some other layout such as a LinearLayout, setting you ListView's layout_width and layout_height to match_parent.

  2. For some reason, when a layout with a ListView along with a custom view is included into another layout, the custom list item layout isn't working. I know this used to work, so it must have broken at some point, probably when we switched from using comment-style metadata to the tools-namespace metadata to record which layout to use.

I've filed this issue to track the work:
http://code.google.com/p/android/issues/detail?id=36838

نصائح أخرى

you can find useful my post about using a layout for a listfragment, it will not solve all of your problems but at least you can work directly with the effective layout you are going to use:

ListFragment Layout from xml

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top