Skip to content Skip to sidebar Skip to footer

Android: Onitemclicklistener Event Not Fired When Page Is Swiped (fragmentactivity And Listfragment)

I have a Fragment activity that contains 6 fragments. Every fragment is instance of the same class (TalkListFragment - which extends ListFragment). Now in the first page when an it

Solution 1:

Replace the line:

listView = (ListView) activity.findViewById(android.R.id.list);

with:

listView = getListView();

With the first line of code you're searching in the parent activity for the current fragment's ListView. This works for the first fragment because at that initial moment the first fragment's ListView is correctly found and used in your listener. As you swipe to the second fragment and search again for a ListView in the parent activity you'll "find" the previous fragment's ListView(which isn't on the screen, but still exists in the ViewPager for better performance). It is on this invisible ListView that you set the OnItemClickListener, but you'll actually click on a different ListView the one that you see in the visible fragment(and which doesn't have a listener). By using getListView() you'll always have a correct reference to the fragment's ListView. Also a ListFragment has its own callback for ListView item clicks and I would recommend using that instead of setting the listener yourself.

Post a Comment for "Android: Onitemclicklistener Event Not Fired When Page Is Swiped (fragmentactivity And Listfragment)"