2012-07-03 34 views
5

我使用數據庫中的數據創建了ExpandableListView。爲此,我使用的是CursorTreeAdapter,並使用包含從數據庫檢索的數據的Cursor對象填充它。ExpandableListView顯示沒有孩子的組

我認爲,默認情況下Android會考慮沒有孩子的組「不可擴展」

但它仍然顯示行上的展開圖標,當我點擊它時,它什麼都不做。我不想讓它顯示這個圖標。

我只想要有孩子的組顯示擴展圖標,這是不會發生的。它顯示所有行的擴展圖標(那些有子和沒有子的)。


UPDATE

我研究我的代碼,我已經看到了這個問題基本上是在設置groupIndicator的羣體。不過,我已經試過了很多像創建一個選擇器,並將其設置方法真實繪製基於狀態和可擴展的,就像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_empty="true" 
      android:state_expanded="false" 
      android:drawable="@android:color/transparent"/> 
    <item android:drawable="@drawable/expander_ic_maximized" /> 
</selector> 

但是當組被摺疊它會隱藏所有羣體,包括那些孩子(因爲Android認出倒塌組爲空)。

任何更好的方法來設置指標只爲有孩子的團體?


這是我的代碼。

public class ContactsExpandableListAdapter extends CursorTreeAdapter 
{ 

    TextView mContactNameTextView, mContactNumberTextView; 
    Cursor mChildrenCursor = null; 

    public ContactsExpandableListAdapter(Cursor cursor, Context context) 
    { 
     super(cursor, context); 
    } 

    @Override 
    protected Cursor getChildrenCursor(Cursor cursor) 
    { 
     if(mContactId == -1) 
      mContactId = null; 

     return mController.getContactById(mContactId); 
    } 

    public int getChildrenCount(int groupPosition) 
    { 
     return mChildrenCursor.getCount(); 
    } 

    @Override 
    protected View newGroupView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup) 
    { 
     View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false); 

     mContactNameTextView = (TextView) view.findViewById(R.id.contact_name); 

     if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)     mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      else 
       mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name"))); 
      view.setTag(cursor.getString(cursor.getColumnIndex("contact_id"))); 
      return view; 
     } 

     @Override 
     protected View newChildView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup) 
     { 
      View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false); 

      if(cursor.getString(cursor.getColumnIndex("contact_name")) == null) 
      { 
       mContactNameTextView = (TextView) view.findViewById(R.id.contact_name); 
       mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      } 
      else 
      { 
       mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number); 
       mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      } 

      view.setTag(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      return view; 
     } 

     @Override 
     protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean) 
     { 
      mContactNameTextView = (TextView) view.findViewById(R.id.contact_name); 
      if(cursor.getString(cursor.getColumnIndex("contact_name")) == null) 
       mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      else 
       mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name"))); 

view.setTag(cursor.getString(cursor.getColumnIndex("contact_id"))); 
     } 

     @Override 
     protected void bindChildView(View view, Context context, Cursor cursor, boolean paramBoolean) 
     { 
      if(cursor.getString(cursor.getColumnIndex("contact_name")) == null) 
      { 
       mContactNameTextView = (TextView) view.findViewById(R.id.contact_name); 
       mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      } 
      else 
      { 
       mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number); 
       mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number"))); 
      } 
     } 
    } 

回答

7

在XML中添加如下因素來ExpandableListView:

android:groupIndicator="@android:color/transparent" 

比你的適配器需要它的內部指示每個組項目視圖。例如,您可以在.xml的右側添加一個ImageView。

然後在適配器你做到以下幾點:

@Override 
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean) 
    ... 

    if (getChildrenCount(groupPosition) > 0) { 
     viewHolder.indicator.setVisibility(View.VISIBLE); 
     viewHolder.indicator.setImageResource(
       isExpanded ? R.drawable.indicator_expanded : R.drawable.indicator); 
    } else { 
     viewHolder.indicator.setVisibility(View.GONE); 
    } 
} 
+1

我該如何在無效方法中返回一個視圖? heheh也許它應該在'newGroupView()'方法中正確嗎? BTW什麼是** viewHolder **和**指標**是什麼? – rogcg

+0

對不起。返回是錯誤的,是真的。在遊標適配器中不需要返回視圖:D。關於ViewHolder(一種在列表中加快速度的模式):http://www.youtube.com/watch?v=wDBM6wVEO70&feature=player_detailpage#t=511s –

+0

上面的鏈接,他正在建立談論ViewHolder。然後他在http://www.youtube.com/watch解釋它?v = wDBM6wVEO70&feature = player_detailpage#t = 511s –

0

爲什麼不讓沒有孩子的小組離開?

更改您的查詢,讓您只有將有子女的組。

+0

但我想告訴那些沒有孩子太項目,但只顯示聯繫電話號碼,只是單個行擴大。 – rogcg

2

在您的適配器類用途:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) 
{ 
    if (getChildrenCount(groupPosition) == 0) { 
      indicator.setVisibility(View.INVISIBLE); 
    } 
    else { 
      indicator.setVisibility(View.VISIBLE); 
      indicator.setImageResource(isExpanded ? R.drawable.group_expanded : R.drawable.group_closed); 
    } 
} 
+1

這是什麼指標? –

+0

Indicator是ExpandableListView右側的圖標,它看起來像V .Its,顯示了像展開或摺疊狀態的listview。 – kabir

相關問題