2017-04-07 83 views
0

我正在使用ExpandableListView以及從我獲得的數據生成的組項和子項。現在我需要在每個生成的組的底部使用一個onClickListener按鈕,該按鈕調用一個函數,該函數必須知道該按鈕所屬的組。爲ExpandableListView中的每個組添加一個按鈕

它添加到我的list_group.xml佈局文件將增加對樣本數據標題 -Header按鈕並將其添加到我的list_item.xml將它添加到每一個的樣本數據 -TextView元素。

這是我第一個使用Android Studio的項目,我試圖解決這個問題。

這裏是how the list looks like
我嘗試添加的按鈕,其按鈕產品的屏幕截圖。

這是我的函數來填充數據列表:

private void prepareListData(ccyPair[] ccyPairs) { 
      listDataHeader = new ArrayList<String>(); 
      listDataChild = new HashMap<String, List<String>>(); 
      int counter = 0; 
      // Adding child data 
      for (ccyPair p : ccyPairs){ 
       listDataHeader.add("Sample Data Title"); 
       List<String> tempExpInfo = new ArrayList<String>(); 
       tempExpInfo.add("Sample data"); 
       tempExpInfo.add("Sample data"); 
       tempExpInfo.add("Sample data"); 
       tempExpInfo.add("Sample data"); 
       tempExpInfo.add("Sample data"); 
       tempExpInfo.add("Sample data"); 
       tempExpInfo.add("BUTTON"); 
       listDataChild.put(listDataHeader.get(counter), tempExpInfo); 
       counter++; 
      } 
     } 

list_group.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="8dp" 
    android:background="@color/colorExpandableList"> 


    <TextView 
     android:id="@+id/lblListHeader" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
     android:textSize="17dp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:background="@color/colorExpandableList" 
     android:textColor="#2282c1" 
     android:layout_alignParentTop="true"/> 

</RelativeLayout> 

list_item.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorExpandableList"><![CDATA[ 
    android:orientation="vertical" > 



    ]]> 

    <TextView 
     android:id="@+id/lblListItem" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorExpandableList" 
     android:paddingBottom="5dp" 
     android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" 
     android:paddingTop="5dp" 
     android:textSize="17dip" /> 


</LinearLayout> 

而ExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

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

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item, null); 
     } 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItem); 

     txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

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

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

回答

1

getChildView()方法是你想看看

第一種選擇:

在getChildView()方法你必須區分哪一個競爭你想膨脹。

if(isLastChild){ 
    convertView = Inflater.inflate(R.layout.list_item_with_button, null); 
} else { 
    convertView = Inflater.inflate(R.layout.list_item, null); 
} 

對於最後一個孩子,您創建了一個包含按鈕組件的list_item_with_button.xml。然後,您可以檢查isLastChild布爾標誌來擴充這個新的佈局,以獲得最後一個位置的按鈕。

第二個選項:您可以編輯現有list_item.xml和

View button = convertView.findViewById(R.id.myButton); 
if(isLastChild){ 
    v.setVisibility(View.VISIBLE); 
} else { 
    v.setVisibility(View.GONE); 
} 
+0

感謝按鈕添加到它,其中顯示在最後位置!第一個選項確實有效,但也很糟糕。但第二個選擇沒有任何問題的工作! – Luanhu

相關問題