2013-03-15 59 views
4

我正在使用Android中的ExpandableListView。當我編輯EditText中的數據時,BaseExpandableListAdapter調用getgroupview()

我有膨脹這一觀點的對話:

<ExpandableListView 
    android:id="@+id/comments_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/editTextNewComment" 
    android:layout_alignParentLeft="true" > 
</ExpandableListView> 

<EditText 
    android:id="@+id/editTextNewComment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@+id/imageButtonPostNewComment" 
    android:ems="10" 
    android:singleLine="true" > 

    <requestFocus /> 
</EditText> 

我BaseExpandableListAdapter看起來像這樣的getgroupview() - 方法:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
     ViewGroup parent) { 

    View row = convertView; 
    ViewHolder holder = null; 

    Log.d("LIST", "getGroupView() groupPosition " + groupPosition + " isExpanded " + isExpanded + " row " + row); 

    if(row==null) { 
     LayoutInflater infl = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = infl.inflate(R.layout.list_item_comment,parent,false); 
     holder = new ViewHolder(row); 
     holder.imbutton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Integer pos = (Integer) v.getTag(); 
       //Do something 
      } 
     }); 
     row.setTag(holder); 
    } 
    else { 
     holder = (ViewHolder) row.getTag(); 
    } 
    holder.imbutton.setTag(Integer.valueOf(groupPosition)); 
    holder.header.setText(""+ (groupPosition+1) + " - " + comments[groupPosition].getOwner()); 
      //.... etc - setting all the textviews accordingly 

    return row; 
} 

我的問題:

當我在我的edittext中寫入東西,getgroupview一直都會被調用!這是從日誌輸出只是在EditText上增加1個字母:

03-15 14:07:51.072: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.080: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.088: D/LIST(4176): getChildView() 
03-15 14:07:51.096: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.104: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.111: D/LIST(4176): getChildView() 
03-15 14:07:51.119: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.119: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.127: D/LIST(4176): getChildView() 
03-15 14:07:51.135: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.135: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.143: D/LIST(4176): getChildView() 
03-15 14:07:51.151: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.151: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.158: D/LIST(4176): getChildView() 
03-15 14:07:51.205: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.205: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.213: D/LIST(4176): getChildView() 
03-15 14:07:51.213: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.213: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.221: D/LIST(4176): getChildView() 
03-15 14:07:51.221: D/LIST(4176): getGroupView() groupPosition 0 isExpanded false row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.221: D/LIST(4176): getGroupView() groupPosition 1 isExpanded true row android.widget.RelativeLayout{41a17d98 V.E..... ......I. 0,0-0,0} 
03-15 14:07:51.229: D/LIST(4176): getChildView() 

getGroupView()和getChildView()被調用多次對輸入的每個字母。在上面的例子中,listview顯然被重繪了八次(在這個例子中,我有兩個組視圖和一個子視圖)。

我錯過了什麼?當我在我的edittext中鍵入東西時,甚至不需要重繪列表視圖。即使它正在重繪,爲什麼有八個調用不同的getview方法?

編輯: 我現在看到,即使我摺疊和/或擴大groupviews,有調用getgroupview和getchildview方法的PLENTY。這是應該如何?

EDIT2: 現在在正常的活動中使用帶有edittext的列表進行了一些測試,並且似乎列表不會像頻繁那樣重繪。所以這個問題似乎是因爲我在對話中提出這個問題。不明白爲什麼它甚至需要重新繪製。

+0

[ExpandableList getChildView每名兒童的兩倍運行]的可能重複(http://stackoverflow.com/questions/6557240/expandablelist-getchildview-running-per-child) – 2015-01-21 07:50:18

回答

7

你永遠無法知道什麼時候getView方法(也可用於getGroupView真)將被調用多少次。

但我確實看到一些可以使其調用超過需要的東西:在您的佈局中,您將ExpandableListView高度設置爲wrap_content。我不認爲它是一件好事:

你讓他需要重新繪製它的孩子,以便知道它的大小以適應它的內容,而它不應該因爲它的可滾動佈局而包裝它的內容。

嘗試將其設置爲0dip而是用layout_weight="1",以留出空間爲您的文本視圖:

<ExpandableListView 
    android:id="@+id/comments_list" 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    android:layout_above="@+id/editTextNewComment" 
    android:layout_alignParentLeft="true" > 
</ExpandableListView> 
+0

去掉wrap_content似乎解決了我的問題。出於某種原因,當我編輯edittext時,listview不想重繪。 :) 謝謝你,先生! – ullstrm 2013-03-15 14:28:25

+0

而且:wrap_content似乎是一個非常糟糕的主意。但是我之所以使用它的原因是因爲我在對話框中列出了這個列表,我只希望對話框像它現在所需要的一樣大。不得不看這個。 – ullstrm 2013-03-15 14:34:24

+0

@Guian我非常愛你!你已經救了我的一週! – CelinHC 2013-08-16 13:27:20

相關問題