在ExpandableListView中重点添加项目

时间:2012-09-04 13:15:08

标签: android android-listview

这看起来很简单,但对于我的生活,我无法理解这一点。

我有一个ExpandableListView,它带有一个来自BaseExpandableListAdapter的自定义ExpandableListAdapter。当我添加一个新项目时,我只想关注新项目。我无法让它发挥作用。

我尝试了以下操作,但不起作用。这是列表视图的活动:

private void processNewItem(C360ItemOwner itemOwner, int pos){
    Item item = itemOwner.newItem(pos);
    expAdapter.notifyDatasetChanged();
    expandList.requestFocus();
    if (item.getParentType() == Item.PARENT_IS_CHECKLIST) {//main level
        expandList.setSelectedGroup(pos);
    } else //Item.PARENT_IS_ITEM //sub level
        expandList.setSelectedChild(item.getParentAsItem().getPosition(), pos, true);
}

我尝试在getView方法的适配器中执行此操作,但这不起作用:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {
    if (convertView == null)
        convertView = inf.inflate(R.layout.edit_group_item, null);
    Item group = (Item) getGroup(groupPosition);
    convertView.setTag(group);
    EditText edtParent = (EditText) convertView.findViewById(R.id.edtParent);
    edtParent.setTag(convertView);
    scatterItemText(edtParent);
    if (group.isNew()) {
        Toast.makeText(edtParent.getContext(), "Found Focus Item " + groupPosition, Toast.LENGTH_SHORT).show();
        edtParent.requestFocus();
    }
    return convertView;
}

我尝试在适配器中设置几个字段:focusItem,一个只写的Item对象和focusEdit,只读EditText obect。所以我分配了项目,在getView中,它将编辑存储在一个字段中。我称之为:

private void processNewItem(C360ItemOwner itemOwner, int pos){
    Item item = itemOwner.newItem(pos);
    expAdapter.setFocusItem(item);//<--Assign it here
    expAdapter.notifyDatasetChanged();
    EditText edtFocus = expAdapter.getFocusEdit();
    if (edtFocus != null)
        edtFocus.requestFocus();

}

然后在适配器的getView中:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {
    if (convertView == null)
        convertView = inf.inflate(R.layout.edit_group_item, null);
    Item group = (Item) getGroup(groupPosition);
    convertView.setTag(group);
    EditText edtParent = (EditText) convertView.findViewById(R.id.edtParent);
    edtParent.setTag(convertView);
    scatterItemText(edtParent);
    if (group == focusItem) {
        Toast.makeText(edtParent.getContext(), "Found Focus Item " + groupPosition, Toast.LENGTH_SHORT).show();
        focusEdit = edtParent;
    }
    return convertView;
}

我对这最后一种方法非常抱歉,但这一行:

EditText edtFocus = expAdapter.getFocusEdit();

在getView()方法运行之前发生,这将分配它(所以它总是为null)! :-( plz halp

编辑:我知道这会很草率,但如果我运行一个线程等待几百毫秒以便getView()可能会运行然后尝试获取focusEdit呢?

1 个答案:

答案 0 :(得分:0)

好的,在Foamy Guy的帮助下弄明白了。我等待一下检查focusEdit的预感就算了。我最初尝试1000毫秒给它足够的时间:

private void setFocus() {
    EditText edtFocus = expAdapter.getFocusEdit();
    if (edtFocus != null)
        edtFocus.requestFocus();
    else
        Toast.makeText(getActivity(), "BLAH!", Toast.LENGTH_SHORT).show();
}

final Runnable r = new Runnable()
{
    public void run() 
    {
        setFocus();

    }
};

private void processNewItem(C360ItemOwner itemOwner, int pos){
    Item item = itemOwner.newItem(pos);
    if (itemOwner.getItemCount() == 1)
        checkHasItems();
    expAdapter.setFocusItem(item);
    resetListAdapter(true);
    Handler handler = new Handler();
    handler.postDelayed(r, 1000);//this works
    //handler.postDelayed(r, 10);//this works too
    //handler.postDelayed(r, 1);//this works too
    //handler.post(r);//this too!
    //r.run();//NOT this.  This fails.
}

然后我尝试了10毫秒,它仍然有效。然后只有一毫秒,令人惊讶的是也有效。 Foamy建议尝试只是发布没有延迟,确实有效。我终于尝试直接根据Foamy的请求运行runnable但是失败了。

所以不确定,但这可以完成工作。谢谢你的帮助。