安卓:当页面swipped不会触发事件OnItemClickListener(FragmentActivity和ListFragment) [英] Android: OnItemClickListener event not fired when page is swipped (FragmentActivity and ListFragment)

查看:290
本文介绍了安卓:当页面swipped不会触发事件OnItemClickListener(FragmentActivity和ListFragment)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含6个片段一个片段的活动。每一个片段是同一类(TalkListFragment - 延伸ListFragment)的实例。现在,在第一个页面被点击时一个项目,然后在事件被触发。只要另一页是刷卡然后单击项目没有任何反应。在XML文件中和同为好;我做了 listView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS)。

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.

下面是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);
    }

这里的TalkListClass:

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);
        }
    }

现在的程序表现良好的第一页上,但是当网页被刷卡然后ItemOnClick永远不会奏效。上有许多ItemClickListener问题,但我找不到这个特殊问题的解决方案。

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.

感谢您。

推荐答案

替换行:

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

listView = getListView();

使用$ C $的第一线c您在父活动要搜索的当前片段的的ListView 。这适用于第一个片段因为在初始时刻第一个碎片的的ListView 正确发现并监听使用。当你滑动到第二个片段并为的ListView 在父活动,你会寻找previous片段的的ListView再次搜索(这是不是在屏幕上,但仍然在 ViewPager 存在更好的性能)。正是在这个无形的的ListView 您所设置的 OnItemClickListener ,但实际上你会点击一个不同的的ListView 您在可见光片段看到(和不具有一个监听)之一。通过使用 getListView()你总是有一个正确的参考片段的的ListView
此外<一个href=\"http://developer.android.com/reference/android/app/ListFragment.html#onListItemClick%28android.widget.ListView,%20android.view.View,%20int,%20long%29\"相对=nofollow>在 ListFragment 都有自己的回调的ListView 项的点击次数与我会建议使用的是自己,而不是设置监听器。

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.

这篇关于安卓:当页面swipped不会触发事件OnItemClickListener(FragmentActivity和ListFragment)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆