2015-06-22 71 views
-2

我是新來的android。我需要在不點擊父視圖的情況下自動膨脹可展開列表視圖的子項。我搜索了很多網站,我沒有發現任何一條線索可以像這樣工作。需要膨脹自動展開列表視圖,而不點擊父視圖

public class ExpListActivity extends ExpandableListActivity { 

    private ExpandableListView mExpList; 
    private ExpListAdapter mExpListAdapter; 

    final String mArrGroupelements[] = {""}; 
    final String mArrChildelements[][] = { { "Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" } }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activitt_explist); 

     mExpList = getExpandableListView(); 
     mExpListAdapter = new ExpListAdapter(this, mArrGroupelements, mArrChildelements); 


    } 
    public void onClick(View v){ 

     mExpList.setAdapter(mExpListAdapter); 

    } 

} 


public class ExpListAdapter extends BaseExpandableListAdapter { 

    Context mContext; 
    String[] mArrGroupelements; 
    String[][] mArrChildelements; 

    public ExpListAdapter(Context context, String[] mArrGroupelements, String[][] mArrChildelements) { 
     mContext = context; 
     this.mArrGroupelements = mArrGroupelements; 
     this.mArrChildelements = mArrChildelements; 
    } 

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

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return mArrChildelements[groupPosition].length; 
    } 

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

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

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

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

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

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

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      tv = (TextView) inflater.inflate(R.layout.parent, null); 
     } 
     tv.setText(mArrGroupelements[groupPosition]); 
     convertView = tv; 
     return convertView; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, 
      ViewGroup parent) { 
     TextView tv = (TextView) convertView; 
//  tv.setWidth(100); 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      tv = (TextView) inflater.inflate(R.layout.list_cell, null); 
     } 
     tv.setText(mArrChildelements[groupPosition][childPosition]); 
     convertView = tv; 
     tv.setWidth(100); 
     return convertView; 
    } 

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

} 
+0

你可以用'mExpList.expandGroup(我)'某個特定羣體擴大,或者你可以將它放在一個'for'循環來展開所有組 –

+0

你可以給我示例code.where我需要放置循環plz – user3189148

回答

1

更改onCreate到以下

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activitt_explist); 

     mExpList = getExpandableListView(); 
     mExpListAdapter = new ExpListAdapter(this, mArrGroupelements, mArrChildelements); 
     for(int i=0; i>mArrGroupelements.length; i++){ 
       mExpList.expandGroup(i); 
     } 
    } 
+0

,但當我點擊按鈕它顯示的父母,當我點擊父母然後顯示child.but我父母爲null.now當我點擊按鈕它應該加載孩子直接不parent.plz – user3189148

+0

,我只有一個家長在該可擴展列表視圖... – user3189148