2016-02-18 62 views
1

enter image description here滾動expandablelistview一組作爲活動創建

嗨,

我想滾動到基於整數值,我從我的第一項活動獲得第二個活動組。

public class Builder extends Activity{ 
ExpandableListView expandableListView; 
Mas_Adapter expandableListAdapter; 
List<String> expandableListTitle; 
Map<String, List<String>> expandableListDetail; 
int grp,chd; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mas_screen); 
    expandableListView = (ExpandableListView) findViewById(R.id.list); 

    Intent intent = getIntent(); 
    grp = intent.getIntExtra("grp", -1); 
    chd = intent.getIntExtra("chd", -1); 

    try { 
     expandableListDetail = CreateMas(grp+1); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("mes", "error in creating data"); 
    } 

    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
    expandableListAdapter = new Mas_Adapter(this, expandableListTitle, expandableListDetail); 
    expandableListView.setAdapter(expandableListAdapter); 
    expandableListView.setOnGroupClickListener(new OnGroupClickListener() { 
      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
             int groupPosition, long id) { 
      return true; // This way the expander cannot be collapsed 
      } 
     }); 
} 
} 

我試過scrollTo,setSelectedGroup但它沒有工作。 請幫助

回答

0

這給了我一個突破這個問題的一種可能的解決方案是:

1)獲取的孩子地位從第一個活動(爲更好地瞭解見圖片)

expandableListView.setOnChildClickListener(new OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 
      Intent intent = new Intent(FirstActivity.this,SecondActivity.class); 
      intent.putExtra("chd", childPosition); 
      startActivity(intent); 
      return false; 
     }}); 

2)接收第二活動中的意圖並將整數位置「chd」和「ExpandableListView」傳遞到具有其他參數的適配器

ExpandableListView exp = (ExpandableListView) findViewById(R.id.exp); 

Intent intent = getIntent(); 
int chd = intent.getIntExtra("chd", -1); 

//passing chd in Second Activity Adapter 
ExpandableListAdapter expAdapter = new ExpandableListAdapter(this,exp,chd); 
exp.setAdapter(expAdapter); 

3)在您的SecondActivity適配器中接收整數並將其用於ExpandableListView上的「setSelectedGroup」。

public class ExpandableListAdapter extends BaseExpandableListAdapter{ 
private Context context; 
int group; 
ExpandableListView expandableListView; 
    public ExpandableListAdapter(Context context,ExpandableListView exp,int grp){ 
    this.context = context; 
    this.expandableListView = exp; 
    this.group = grp; 
} 
@Override 
public View getGroupView(final int listPosition,final boolean isExpanded, 
          View convertView,final ViewGroup parent) { 
    if (convertView == null) {  
     // your code 
    } 
    textview.setText(listTitle); //set view of group before selecting it 
    if(group != -1){ 
      expandableListView.setSelectedGroup(group); 
     } 

    return convertView; 
    } 
}