我有一个包含 6 个片段的片段活动。每个片段都是同一类的实例(TalkListFragment - 它扩展了 ListFragment)。现在在第一个页面中,当单击一个项目时会触发该事件。一旦滑动另一个页面,然后单击该项目,什么都不会发生。我listView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
在 XML 文件中也做了同样的事情。
这是代码
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);
}
这是 TalkList 类:
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);
}
}
现在程序在第一页上表现良好,但是当页面被刷过时,ItemOnClick 永远不会工作。ItemClickListener 上有很多问题,但我找不到这个特定问题的解决方案。