2012-10-15 226 views
1

我有一個ExpandableListView,並且正在爲它設置一個自定義的BaseExpandableAdapter。我的問題是,我可以誇大我想顯示在適配器本身的構造函數中的佈局(XML),而不是每次都在getChildView()中膨脹它?我將動態地更改要顯示的圖像,但每個可擴展列表都要在getChildView()中選擇。代碼在這裏。在適配器的構造函數中填充xml文件

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context mContext; 
    private LayoutInflater mInflater; 
    GridLayout gl = null; 
    HorizontalScrollView hScrl; 
    public MyExpandableListAdapter (ArrayList<SensorType> parentGroup, Context context) {  
     mContext = context; 
     mInflater = LayoutInflater.from(mContext); 
     View view = infalInflater.inflate(R.layout.expandlist_items, null); 
    gl = (GridLayout) view.findViewById(R.id.gl); 
     hScrl = (HorizontalScrollView) view.findViewById(R.id.hScroll); 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     if (childPosition == 0) { 
      gl1.removeAllViews(); 
     } 
     int mMax = 8; // show 8 images 
     for (int i =0; i < mMax; i++) { 
     ImageView myImg = new ImageView(mContext);        
       myImg.setBackgroundRes(R.drawable.image1); 
       gl.addView(myImg); 
      } 

return hScrl; 
} 

上面是否有任何問題,它是正確顯示圖像和滾動以及如果有更多的圖像。但我的問題是,這擴大布局和獲得gl和hscrl的工作,應該在適配器的構造函數中(如上所示),或者它應該在getChildView中?

這4 LOC:

mInflater = LayoutInflater.from(mContext); 
View view = infalInflater.inflate(R.layout.expandlist_items, null); 
gl = (GridLayout) view.findViewById(R.id.gl); 
hScrl = (HorizontalScrollView) view.findViewById(R.id.hScroll); 

回答

1

我發現,如果我需要在適配那麼充氣應getChildView()或getView來完成每一個項目被一再膨脹的佈局( )任何適配器。 但是在上面的代碼中,我在構造函數中將它膨脹一次,並手動將圖像添加到GridLayout中,因此我無需在getChildView()中一次又一次地膨脹它。 因此,它工作正常,因爲它在適配器的構造函數中膨脹一次。

相關問題