在创建 activity 时将可扩展列表视图滚动到一个组

Scroll expandablelistview to a group as the activity is created

嗨,

我想根据我从第一个 activity 获得的整数值滚动到第二个 activity 的组。

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,但没有用。 请帮忙

让我突破这个问题的可能解决方案是:

1) 从 First 获取子位置 Activity(参见图片以便更好地理解)

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) 在秒 Activity 中接收意图并将整数位置 "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) 在您的第二个Activity 适配器中接收整数并将其用于 'setSelectedGroup' 您的 ExpandableListView。

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