这似乎很简单,但对于我的生活,我无法弄清楚。
我有一个带有自定义 ExpandableListAdapter 的 ExpandableListView,它继承自 BaseExpandableListAdapter。当我添加一个新项目时,我只想关注新项目。我不能让它工作。
我尝试了以下方法,但不起作用。这是在列表视图的活动中:
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 对象。所以我分配了项目并在 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() 方法运行之前发生,该方法将分配它(因此它始终为空)!:-(请停止
编辑:我知道这会很草率,但是如果我确实运行了一个线程来等待几百毫秒以便 getView() 可能运行然后尝试获取 focusEdit 怎么办?