2011-04-19 83 views
35

我想提出與圖像的gridview的可擴展列表內的可擴展列表...我已經做出,但在GridView不顯示所有項目...的GridView裏面的Android

我怎麼能讓我的可擴展列表子適應gridview大小?

LIST ADAPTER

public class CustomListAdapter extends BaseExpandableListAdapter 
{ 
    String[]   catg = { "Administração, Escritorio e Industria", "Cultura e Entretenimento", "Educação e Crianças", "Eventos e Estado do tempo", "Amigos, Familia e Habitações", "Multimédia", "Diversos", "Números e Letras", "Restaurantes e Hoteis", "Desporto, Saude e Beleza", "Lojas", "Turismo e Natureza", "Transportes" }; 

    Context    myctx; 


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

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

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
    { 
     ViewGroup item = getViewGroupChild(convertView, parent); 
     GridView label = (GridView) item.findViewById(ipvc.estg.placebook.R.id.gridview); 
     label.setAdapter(new GridAdapter(parent.getContext(), groupPosition+1)); 
     return item; 
    } 

    private ViewGroup getViewGroupChild(View convertView, ViewGroup parent) 
    { 
     // The parent will be our ListView from the ListActivity 
     if (convertView instanceof ViewGroup) 
     { 
      return (ViewGroup) convertView; 
     } 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 
     ViewGroup item = (ViewGroup) inflater.inflate(ipvc.estg.placebook.R.layout.expandable_list_row, null); 
     return item; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) 
    { 
     return 1; 
    } 

    @Override 
    public Object getGroup(int groupPosition) 
    { 
     return catg[groupPosition]; 
    } 

    @Override 
    public int getGroupCount() 
    { 
     return catg.length; 
    } 

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



    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
    { 
     View item = getViewGroupGroup(convertView, parent); 

     TextView text = (TextView) item.findViewById(android.R.id.text1); 
     text.setText(catg[groupPosition]); 
     return item; 
    } 

    private View getViewGroupGroup(View convertView, ViewGroup parent) 
    { 
     // The parent will be our ListView from the ListActivity 
     if (convertView instanceof View) 
     { 
      return (View) convertView; 
     } 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View item1 = (View) inflater.inflate(android.R.layout.simple_expandable_list_item_1, null); 

     return item1; 
    } 

    @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 true; 
    } 

} 

列表行佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <GridView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:columnWidth="50dp" 
     android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" /> 
</LinearLayout> 
+0

可以顯示GridAdapter嗎?因爲我已經在導航抽屜裏實現了可擴展的listview,並且我想在可擴展的listview子項中添加gridview包含圖像。 – Hendy 2014-01-09 02:23:45

+0

嘿,你可以請分享完整的編碼? – Sharath 2016-02-08 05:10:28

回答

36

我已經做噸搜索我自己解決這種情況,但至今沒有發現任何東西,所以我做了一個解決方法。

此變通辦法假定您知道/可以檢索您的列的寬度以及網格單元格渲染器的高度(在版式xml中設置的dp大小)。

你應該裏面插入幾行你ExpandableListAdaptergetChildView方法:

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
{ 
    ViewGroup item = getViewGroupChild(convertView, parent); 
    GridView label = (GridView) item.findViewById(ipvc.estg.placebook.R.id.gridview); 
    label.setAdapter(new GridAdapter(parent.getContext(), groupPosition+1)); 

    // initialize the following variables (i've done it based on your layout 
    // note: rowHeightDp is based on my grid_cell.xml, that is the height i've 
    // assigned to the items in the grid. 
    final int spacingDp = 10; 
    final int colWidthDp = 50; 
    final int rowHeightDp = 20; 

    // convert the dp values to pixels 
    final float COL_WIDTH = getBaseContext().getResources().getDisplayMetrics().density * colWidthDp; 
    final float ROW_HEIGHT = getBaseContext().getResources().getDisplayMetrics().density * rowHeightDp; 
    final float SPACING = getBaseContext().getResources().getDisplayMetrics().density * spacingDp; 

    // calculate the column and row counts based on your display 
    final int colCount = (int)Math.floor((parentView.getWidth() - (2 * SPACING))/(COL_WIDTH + SPACING)); 
    final int rowCount = (int)Math.ceil((intValues.size() + 0d)/colCount); 

    // calculate the height for the current grid 
    final int GRID_HEIGHT = Math.round(rowCount * (ROW_HEIGHT + SPACING)); 

    // set the height of the current grid 
    label.getLayoutParams().height = GRID_HEIGHT; 

    return item; 
} 

通過增加上述,我能產生以下顯示的佈局:

expandable list with grid - portrait

expandable list with grid - landscape

我希望它能幫助你。

+0

這項工作就像一個魅力!非常感謝您的幫助:D – Zasuk 2011-04-21 12:43:44

+0

不客氣,快樂編碼更進一步! ;)也爲+1的原因。問題,讓我們希望別人也會發現它很有用 – rekaszeru 2011-04-21 12:44:37

+0

@rekaszeru你能告訴我'parentView'和'intValues'代表什麼?在此先感謝 – AnujAroshA 2012-05-02 08:16:15

1

如何使用畫廊而不是GridView? 查詢this了!

+4

請注意,[Gallery](http://developer.android.com/reference/android/widget/Gallery.html)已從開始API級別16(Jelly Bean)棄用。 – Maarten 2012-12-24 18:05:38

2

所有你需要做的是改變GrideView與ExpandableHeightGridView

android:isScrollContainer="false" 

,並在代碼

expandableHeightGridView.setExpanded(true); 
+0

您的解決方案有效。我認爲這是解決這個問題最簡單的方法。謝謝 – 53iScott 2015-06-19 07:23:12