2012-03-22 59 views
45

我想編程一個Android接口,它使用了可擴展列表和另一面的片段,因此我可以通過單擊可擴展列表的子元素來加載不同的資源。但不幸的是,我無法在任何地方找到任何關於此列表的好教程。是的,我查看了API演示,並且使用BaseExpandableListAdapter創建了一個普通列表,但仍然需要理解這些列表,如果沒有一個好的教程,它有點難,你有什麼好的或者我可以檢查的信息嗎?Android ExpandableListView - 尋找教程

+0

我不能相信這是標記爲「沒有建設性」!這是非常有建設性的,答案非常有幫助!感謝您發佈這個問題,DeX03。 – 2017-09-15 05:27:37

回答

50

你可以找到工作的擴展列表視圖例如通過以下鏈接:

點擊孩子,你可以這樣處理。

getExpandableListView().setOnChildClickListener(new OnChildClickListener() {     
    public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 
     // your code... 
    } 
}); 

希望這會幫助你。 謝謝..

+0

但對於不擴展ExpandableListActivity的活動來說,這種方式的工作方式不同,因爲這就是我所擁有的,我該如何解決該問題? – DeX03 2012-03-23 13:57:37

+0

沒關係,我只是修復它,很好的幫助! – DeX03 2012-03-23 20:43:55

58

創建項目列表

List<ParentItem> itemList = new ArrayList<ParentItem>(); 

ParentItem parent1 = new ParentItem(); 
parent1.getChildItemList().add(new ChildItem()); 
parent1.getChildItemList().add(new ChildItem()); 
parent1.getChildItemList().add(new ChildItem()); 

ParentItem parent2 = new ParentItem(); 
parent2.getChildItemList().add(new ChildItem()); 
parent2.getChildItemList().add(new ChildItem()); 
parent2.getChildItemList().add(new ChildItem()); 

itemList.add(parent1); 
itemList.add(parent2); 

ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(context, itemList); 

數據對象

public class ParentItem { 

     private List<ChildItem> childItemList; 

     public ParentItem() { 
       childItemList = new ArrayList<ChildItem>(); 
     } 

     public List<ChildItem> getChildItemList() { 
       return childItemList; 
     } 
} 

public class ChildItem { 
     // filll with your data 
} 

適配器

public class ExpandableListViewAdapter extends BaseExpandableListAdapter { 

     private static final class ViewHolder { 
       TextView textLabel; 
     } 

     private final List<ParentItem> itemList; 
     private final LayoutInflater inflater; 

     public ExpandableListViewAdapter(Context context, List<ParentItem> itemList) { 
       this.inflater = LayoutInflater.from(context); 
       this.itemList = itemList; 
     } 

     @Override 
     public ChildItem getChild(int groupPosition, int childPosition) { 

       return itemList.get(groupPosition).getChildItemList().get(childPosition); 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
       return childPosition; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
       return itemList.get(groupPosition).getChildItemList().size(); 
     } 

     @Override 
     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, 
           final ViewGroup parent) { 
       View resultView = convertView; 
       ViewHolder holder; 


       if (resultView == null) { 

         resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id 
         holder = new ViewHolder(); 
         holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id 
         resultView.setTag(holder); 
       } else { 
         holder = (ViewHolder) resultView.getTag(); 
       } 

       final ChildItem item = getChild(groupPosition, childPosition); 

       holder.textLabel.setText(item.toString()); 

       return resultView; 
     } 

     @Override 
     public ParentItem getGroup(int groupPosition) { 
       return itemList.get(groupPosition); 
     } 

     @Override 
     public int getGroupCount() { 
       return itemList.size(); 
     } 

     @Override 
     public long getGroupId(final int groupPosition) { 
       return groupPosition; 
     } 

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, View theConvertView, ViewGroup parent) { 
       View resultView = theConvertView; 
       ViewHolder holder; 

       if (resultView == null) { 
         resultView = inflater.inflate(android.R.layout.test_list_item, null); //TODO change layout id 
         holder = new ViewHolder(); 
         holder.textLabel = (TextView) resultView.findViewById(android.R.id.title); //TODO change view id 
         resultView.setTag(holder); 
       } else { 
         holder = (ViewHolder) resultView.getTag(); 
       } 

       final ParentItem item = getGroup(groupPosition); 

       holder.textLabel.setText(item.toString()); 

       return resultView; 
     } 

     @Override 
     public boolean hasStableIds() { 
       return true; 
     } 

     @Override 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
       return true; 
     } 

} 

給你

============================== 
+Parent 1 
============================== 
-child 1.1 
============================== 
-child 1.2 
============================== 
-child 1.3 
============================== 
+Parent 2 
============================== 
-child 2.1 
============================== 
-child 2.2 
============================== 
-child 2.3 
============================== 
+1

但是,我如何處理列表中的孩子的點擊事件?因爲這是我找到的所有教程的主要問題 – DeX03 2012-03-22 20:55:59

+0

'冒號(;)'丟失' itemList = new ArrayList ()' – 2014-03-01 07:30:46

+1

我得到null holder.textLabel.setText(item.toString() );',如何修復它? – 2014-03-01 08:21:51

1

這種方式可以處理事件:

getExpandableListView().setOnChildClickListener(new OnChildClickListener() 
{    
    public boolean onChildClick(ExpandableListView parent, 
     View v, int groupPosition, int childPosition, long id) 
    { 
     // your code... 
    } 
} 
+2

不相干的答案.... – 2013-08-27 09:15:56