OnChildClick events stop after scrolling through a list

I have an application that uses an ExpandableListView widget populated with elements through a BaseExpandableListAdapter. The application was created for Android version 1.6, and when I test it using the Android emulator (version 1.6 build), everything works fine. I can scroll the list up and down, and when I click on any items in the list, I get the onChildClick () event.

However, when I try to run the same application on a real Android phone (Gingerbread is running), I only get onChildClick () events if I do not scroll ExpandableListView. If I scroll through the list too much, I no longer get onChildClick events (and itemClickEvents as well).

So, I'm confused. Why am I not getting any onChild or onItem events for list items after I scroll through it? Please note that if I scroll it only a small amount (say, two or three lines), I get click events, but scroll more, and then I no longer get click events for any item in the list.

Any suggestions or recommendations would be appreciated.

Thanks.

+4
source share
4 answers

Well, I answer my question here for others who may have the same problem. The problem I am facing seems to be related to the focal status of the ExpandableListView widget. When I scroll through a list, the ListView widget seems to lose focus and I cannot select any of the listed items. I'm not sure the best way to fix this, but I did it for the job.

I added a scroll listener for the ListView widget as show:

getExpandableListView().setOnScrollListener(this); 

Then I added a method for my activity that will return focus back to the ListView widget after it scrolls:

 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // - this method is called when there is a scroll event in the main ExpandableListView widget. // - after the user stops scrolling the listview, make sure to set the focus to the widget if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE ) { ExpandableListView elv = getExpandableListView(); elv.requestFocusFromTouch(); } } 

Now the problem seems to be fixed. I am sure there is a better way (and if you know one, please post it here), otherwise my “hack” seems to work fine.

Hope this helps someone with a similar problem.

+5
source

Thank you, this answer solved my problem of losing focus when scrolling.

 ListView mRecentSearchList; mRecentSearchList.setOnScrollListener(new OnScrollListener(){ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //adapter.notifyDataSetChanged(); } public void onScrollStateChanged(AbsListView view, int scrollState) { //adapter.notifyDataSetChanged(); if(scrollState == OnScrollListener.SCROLL_STATE_IDLE ||scrollState == SCROLL_STATE_TOUCH_SCROLL || scrollState == SCROLL_STATE_FLING) { mRecentSearchList.requestFocusFromTouch(); } } }); 
+2
source

I had exactly the same problem, and I found that it was caused not by the list itself, but by the adapter (ResourceCursorTreeAdapter). To prevent the failure of the groups, I performed the following actions in my adapter:

 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = super.getGroupView(groupPosition, isExpanded, convertView, parent); ExpandableListView list = (ExpandableListView) parent; list.expandGroup(groupPosition); return view; } 

If you do the same, try replacing this with another solution.

0
source

In my case, I used the same solution suggested by cohoman , however the requestFocus () method provided me with a better user interface:

  navList.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(scrollState == OnScrollListener.SCROLL_STATE_IDLE) { navList.requestFocus(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } }); 
0
source

All Articles