这似乎很简单,但我却想不出来。
我有一个ExpandableListView,自定义的ExpandableListAdapter是从BaseExpandableListAdapter派生的。当我添加一个新项目时,我只想聚焦在新项目上。但我不能让它工作。
我尝试了以下操作,但没有成功。这是在拥有listview的活动中:
我对这个方法很有希望,但是这一行代码:
我有一个ExpandableListView,自定义的ExpandableListAdapter是从BaseExpandableListAdapter派生的。当我添加一个新项目时,我只想聚焦在新项目上。但我不能让它工作。
我尝试了以下操作,但没有成功。这是在拥有listview的活动中:
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对象)。我将Item分配给它,然后在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怎么办?
Handler.postDelayed(Runnable r, int delay);
。 - FoamyGuy