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 item is clicked then the event is fired. As soon as another page is swiped then on clicking the item nothing happens. I have done listView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
and same in the XML file as well.
Here is the code
public class TalkFragmentListActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener
{
private SwipePagerAdapter pageAdapter = null;
private ViewPager viewPager = null;
.
.
private void initializePaging()
{
foodFragment = new TalkListFragment();
foodFragment.setTitle("Food");
foodFragment.setUrl("");
shoppingFragment = new TalkListFragment();
shoppingFragment.setTitle("Shopping");
shoppingFragment.setUrl("");
eventsFragment = new TalkListFragment();
eventsFragment.setTitle("Events");
eventsFragment.setUrl("");
nightlifeFragment = new TalkListFragment();
nightlifeFragment.setTitle("Night Life");
nightlifeFragment.setUrl("");
serviceFragment = new TalkListFragment();
serviceFragment.setTitle("Service");
serviceFragment.setUrl("");
conceirgeFragment = new TalkListFragment();
conceirgeFragment.setTitle("Conceirge");
conceirgeFragment.setUrl("");
pageAdapter = new SwipePagerAdapter(getSupportFragmentManager());
pageAdapter.addFragment(foodFragment);
pageAdapter.addFragment(shoppingFragment);
pageAdapter.addFragment(eventsFragment);
pageAdapter.addFragment(nightlifeFragment);
pageAdapter.addFragment(serviceFragment);
pageAdapter.addFragment(conceirgeFragment);
viewPager = (ViewPager) super.findViewById(R.id.talk_list_pager);
viewPager.setAdapter(this.pageAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(this);
}
Here's the TalkListClass:
public class TalkListFragment extends ListFragment implements OnClickListener, OnItemClickListener
{
private TalkListAdapter adapter = null;
private ListView listView = null;
.
.
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
TalkFragmentListActivity activity = (TalkFragmentListActivity) getActivity();
adapter = new TalkListAdapter(activity, R.layout.talks_fragment_item, getTalkList());
listView = (ListView) activity.findViewById(android.R.id.list);
// listView = (ListView) activity.findViewById(R.id.talk_items_listview);
setListAdapter(adapter);
listView.setOnItemClickListener(this);
listView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
.............
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (listView != null)
{
TalkListAdapter adptr = (TalkListAdapter) parent.getAdapter();
Talk talk = (Talk) adptr.getItem(position);
Intent i = new Intent(view.getContext(), TalkDetailActivity.class);
i.putExtra("talk", talk);
this.startActivity(i);
}
}
Now the program behaves well on the first page but when the pages is swiped then ItemOnClick never works. There are many questions on ItemClickListener but I cannot find the solution for this particular problem.
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.