2010-12-07 85 views
0

我有一个ExpandableListView有很多孩子。一些孩子包括一个标题和描述,有些则缺乏描述。我使用了一个SimpleExpandableListAdapter,并且这个孩子的布局由LinearLayout中的两个TextView项目组成。如何阻止空的TextView元素占用列表中的太多空间?

我遇到的问题是空描述仍占用空间,在没有描述的项目之间创建太多间距。

有没有办法在适配器上实时隐藏第二个TextView,或者设置布局以便空的TextView不占用任何空间?

谢谢。

回答

4

基督教是正确的(如果他已经张贴作为一个答案,我会简单地接受它;))。

解决方案是创建我自己的适配器,结果很简单,虽然在设置元素的可见性时有几个问题。基本上,无论你是隐藏还是可见,你都必须每次都设置它。否则,当滚动浏览列表时,您会发现不同的列表元素会在他们不应该显示的时候突然显示隐藏的元素,反之亦然。 下面是一些示例代码:

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

} 
1

尝试能见度设置为View.GONE

+0

隐藏所有的描述。有没有办法在适配器上实时设置它? – anakin78z 2010-12-07 01:10:55

+1

+1 @anakin创建您自己的适配器。 – Cristian 2010-12-07 01:20:48

0

似乎还设置Fave.setHeight(0)会做的伎俩在适配器。