Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:24:46+00:00 2026-06-14T18:24:46+00:00

I have a Fragment activity that contains 6 fragments. Every fragment is instance of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:24:48+00:00Added an answer on June 14, 2026 at 6:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent Fragment Activity that has a ViewPager which contains a child
I have a parent SherlockFragmentActivity class that contains ViewPager with 4 Fragments inside. One
I have an activity that contains a linear layout and a fragment placeholder. <RelativeLayout
So, my application have one activity ProjectActivity as a home, which contains 3 fragments
I have a Fragment activity that has a ListFragment on the left and a
I have a ViewPager within a Fragment which contains three Fragments we'll call V1,
I have set of Foo objects. I have an activity which contains a ListFragment
I have Activity with ViewPager that contains several Fragments. I am also using drop-down
Say I have an activity that has fragments added programmatically: private void animateToFragment(Fragment newFragment,
I have an activity(say, MyActivity) that contains fragments, and i am using fragmentTransaction.replace(XX,YY) for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.