How do I stop empty TextView Elements from taking up too much space in a list?
I have an ExpandableListView with lots of children. Some of the children consist of a title and description, while some lack a description. I use a SimpleExpandableListAdapter, and the layout for the children consists of two TextView items in a LinearLayout.
The problem I'm having is that empty descriptions still take up space, creating too much spacing between items that do not have descriptions.
Is there a way to hide the second Tex开发者_开发问答tView on the fly in the adapter, or to set up the layout so that the empty TextView doesn't take up any space?
Thanks.
Christian was correct (and if he had posted it as an answer, I would have simply accepted it ;) ).
The solution was to create my own adapter, which turned out to be rather simple, though there are a few gotchas when setting visibility of an element. Basically, you have to set it every time, no matter if you're hiding it or making it visible. Otherwise you'll find that different list elements will suddenly show the hidden element when they're not supposed to, or vice versa, when scrolling through the list. Here is some example code:
public class SpellListAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public SpellListAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.list_item_fave, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
String spell = c.getString(c.getColumnIndexOrThrow(SpellDbAdapter.KEY_SPELL));
int fave = c.getInt(c.getColumnIndexOrThrow(SpellDbAdapter.KEY_FAVORITE));
TextView Spell = (TextView) v.findViewById(R.id.text);
if (Spell != null) {
Spell.setText(spell);
}
//Set Fave Icon
TextView Fave = (TextView) v.findViewById(R.id.fave_icon);
//here's an important bit. Even though the element is set to invisible by
//default in xml, we still have to set it every time. Then, if the
//spell is marked as a favorite, set the view to visible.
Fave.setVisibility(View.INVISIBLE);
if (fave == 1){
Fave.setVisibility(View.VISIBLE);
}
}
}
Try setting the visibility to View.GONE
Seems that also set Fave.setHeight(0) would do the trick in your adapter.
精彩评论