Focus object added to ExpandableListView

It seems simple, but for the life of me I cannot understand it.

I have an ExpandableListView with a custom ExpandableListAdapter that came from BaseExpandableListAdapter. When I add a new element, I just want to focus the new element. I can't get this to work.

I tried the following, which does not work. This applies to list activity:

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

I tried to do this in the adapter of the getView method, which does not work:

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

I tried to configure a couple of fields in the adapter: focusItem, the Item object is write-only and focusEdit is a read-only EditText obect. Therefore, I assign the element to getView, it saves the editing in the field. I call it this way:

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

Then in the getView of the adapter:

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

I really hope for this last method, but this line:

 EditText edtFocus = expAdapter.getFocusEdit(); 

Occurs before the getView () method is run, which would assign it (therefore it is always null)! :-( plz halp

EDIT: I know this will be messy, but what if I executed a thread to wait a few hundred milliseconds so that getView () could start up and then try to get focusEdit?

+4
source share
1 answer

Ok, figured it out with the help of Foamy Guy. My hunch is to wait a bit to verify that focusEdit has worked out. First I tried 1000 milliseconds to give it a lot of time:

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

Then I tried 10 milliseconds and still worked. Then only one millisecond and it is surprising that they also worked. Pena suggested trying to just post a message without delay, and it really works. I finally tried to run runnable directly at the request of Foamy, but that failed.

So not sure, but it does its job. Thanks for helping the guys.

0
source

Source: https://habr.com/ru/post/1432280/


All Articles