1

我有一个包含 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 上有很多问题,但我找不到这个特定问题的解决方案。

4

1 回答 1

2

换行:

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

和:

listView = getListView();

使用第一行代码,您正在父活动中搜索当前片段的ListView. 这适用于第一个片段,因为在最初的那一刻,第一个片段ListView被正确地找到并在您的侦听器中使用。ListView当您滑动到第二个片段并再次在父活动中搜索 a时,您将“找到”前一个片段ListView(它不在屏幕上,但仍然存在于ViewPager更好的性能中)。正是在这个不可见ListView的地方设置了OnItemClickListener,但实际上你会点击ListView你在可见片段中看到的另一个(并且它没有监听器)。通过使用getListView(),您将始终正确引用片段的ListView. 还有一个ListFragment有自己的ListView项目点击回调,我建议使用它而不是自己设置监听器。

于 2012-11-23T15:33:39.940 回答