2

我無法弄清楚如何在android中實現這一點。我有這樣一個佈局:如何實現Expandablelistview子自定義佈局?

enter image description here

我希望它這樣的加號按鈕的點擊會顯示完整的佈局是這樣的:

enter image description here

這種佈局是孩子的可擴展列表視圖項目。因此,當展開式列表視圖項目被點擊時,它會顯示該類別中短暫丟失的項目。然後,如果用戶想要看到全部,他將點擊加號按鈕。 任何幫助將不勝感激。

回答

1

創建一個名爲一個XML佈局expandable_list_layout

<ExpandableListView 
    android:id="@+id/data_list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_below="@id/dynamicHeadingTxt" 
    android:layout_marginTop="3dp" 
    android:layout_weight="8" 
    android:cacheColorHint="#00ffffff" 
    android:childDivider="@drawable/child_separator" 
    android:divider="@drawable/child_separator" 
    android:groupIndicator="@null" > 
</ExpandableListView> 

創建自定義適配器。讓我們稱之爲InboxExpandableListAdapter。這個類應該擴展BaseExpandableListAdapter並覆蓋

public class ada extends BaseExpandableListAdapter{ 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public Object getGroup(int groupPosition) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public int getGroupCount() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public long getGroupId(int groupPosition) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public boolean hasStableIds() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return false; 
} 
} 

如果你看這裏,在被覆蓋的方法有兩個參數,即一個groupposition和childposition。這些建議我們應該向適配器提供一些必須包含組和他們的孩子的列表。我正在爲該類創建構造函數

public InboxExpandableListAdapter(Activity context, List<String> data, List<String> data_secondry, 
     List< List<String>> dataCollections) { 
    this.context = context; 
    this.dataCollections = dataCollections; 
    this.data = data; 
    this.data_secondry = data_secondry; 
} 

現在,getChildView方法和getGroupView將填充每個項目的視圖。所以,這些方法裏面,充氣要顯示組的佈局和它們各自的孩子

public View getChildView(final int groupPosition, final int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    final String data = (String) getChild(groupPosition, childPosition); 
    LayoutInflater inflater = context.getLayoutInflater(); 

    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.child_item, null); 
    } 

    TextView item = (TextView) convertView.findViewById(R.id.data); 

    item.setText(data); 
    return convertView; 
} 

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

    String laptopName = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.group_item, 
       null); 
    } 

    TextView item = (TextView) convertView.findViewById(R.id.data); 
    item.setTypeface(null, Typeface.BOLD); 
    item.setText(laptopName); 
    return convertView; 
} 
0

CoordinatorLayout是專爲此類案件的事。看看Android Developers blog的示例

或者,您可能會將兩個佈局分組 - 一個是最上面的一行,另一個是其上的其他行。然後處理「+」按鈕Click您將在其上顯示後者(可能的動畫)的事件。

我會繼續使用CoordinatorLayout,因爲這是更好的方法,不過隨意嘗試兩種方法,看看更適合您。

+0

我已閱讀了CoordinatorLayout,但我仍不清楚這是如何實現的。請爲此提供更多指導。謝謝 – ifiok