【问题标题】:Scroll expandablelistview to a group as the activity is created创建活动时将可扩展列表视图滚动到一个组
【发布时间】:2016-02-18 20:05:06
【问题描述】:

嗨,

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

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 但没有成功。 请帮忙

【问题讨论】:

    标签: android scroll expandablelistview


    【解决方案1】:

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

    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) 在 Second Activity 中接收 Intent 并将整数位置“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;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多