2012-05-29 33 views

回答

0

我創造了我自己的ExpandableListView。我使用XML和類中的佈局來構建組件。

令人驚訝的是,這很容易做到。

這是容易得多比標準ExpandableListView理解,因爲我創建了一個類,併爲列表中的每個元素的佈局(在列表本身,組和項目)。沒有必要搞亂地圖列表,這在我看來會降低代碼的表現力和可讀性。

此外,該列表變得非常靈活和可定製。我可以在運行時輕鬆添加和刪除組和項目。現在我可以自由修改列表的外觀和內部組件。

我創建可以做同樣的標準和更多的ExpandableListView。只是不知道表現是否受損,但沒有注意到任何明顯的問題。

+0

你是如何把按鈕,使其工作?我有這個問題,我不能拿出一個解決方案http://stackoverflow.com/questions/11205052/button-in-a-row-in-expandablelistview – noloman

0

您需要充氣包含一個按鈕自定義XML文件中的groupView,像這樣的(如inflate_xml_groupview.xml):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/FrameLayoutGroupView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 


    <Button 
     android:id="@+id/myButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ButtonOfMyExpandableListGroupView" 
     android:visibility="visible" /> 

</FrameLayout> 

然後,你必須創建一個擴展BaseExpandableListAdapter定製ExpandableListAdapter並獲得按鈕在getGroupView()方法,像這樣:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 

     convertView = inflater.inflate(R.layout.inflate_xml_groupview, null); 
     holder = new ViewHolder(); 
     holder.Button = (Button) convertView.findViewById(R.id.myButton); 
     convertView.setTag(holder); 
     } else { 
     holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.position = ListOfItems.get(groupPosition).getPosition(); 
     Button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       Toast.makeText(getApplicationContext(), "Button " + groupPosition + " is clicked !", Toast.LENGTH_SHORT).show(); 
       // DO STUFF 
     } 
    }); 
} 

希望這有助於。

19

Android ExpandableListView可以在Group或Child中有任何按鈕。

確保按鈕不可作爲焦點,像下面的適配器。

editButton.setFocusable(false); 

這將有助於點擊集團和按鈕內部group.parent seperately

+0

是的,這應該是答案。 –

+6

在代碼中執行它非常重要,而不是XML。像魅力一樣工作,感謝分享。 –

+0

對我來說同樣的問題。複選框與標題佈局..不擴大/摺疊。添加此行後如同魅力 –

相關問題