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 9146211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:41:46+00:00 2026-06-17T10:41:46+00:00

I’m new to Android programming . I have a list of items, when the

  • 0

I’m new to Android programming.

I have a list of items, when the user clicks on an item it takes them to a screen with that item details.

I want the user to have the ability to swipe right and left to view other items’ details in the list, instead of going back to the list and choosing another item.

I read that I need to use ViewPager for the ability to swipe right and left, so I did that.
ViewPager works fine, but my problem when I click any item on the list I always get to the first page in the ViewPager.

I don’t want that, what I want is if I click on item 4 on the list it takes me to page 4 in the view pager and still have the ability to swipe right and left to view the details of other items.
I know how to set the page in viewpager by using mPager.setCurrentItem(0)
But I don’t know how to set it according to which item was selected from the list (i.e which item launched the activity).

Here is my code:

Main activity which contains the listview:

    public class Main extends SherlockListActivity implements OnItemClickListener {

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);  

            ListView mylist = (ListView) findViewById(android.R.id.list);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_reda_1, R.id.list_content, getResources().getStringArray(R.array.items_list_array) );
            mylist.setAdapter(adapter);

            mylist.setOnItemClickListener(new OnItemClickListener() 
            {
                public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) 
                {
                    Intent n = null; 
                    switch (position){
                        case 0: 
                            n = new Intent(getApplicationContext(), ViewPagerClass.class);
                            break;
                        case 1: 
                            n = new Intent(getApplicationContext(), ViewPagerClass.class);
                            break;
                        case 2: 
                            n = new Intent(getApplicationContext(), ViewPagerClass.class);
                            break;
                        case 3: 
                            n = new Intent(getApplicationContext(), ViewPagerClass.class);
                            break;
                    }

                    if(null!=n)
                        startActivity(n);
                }
            });     
        }
    }

ViewPagerClass

    public class ViewPagerClass extends SherlockFragmentActivity{

        static final int NUM_ITEMS = 4;
        MyAdapter mAdapter;
        ViewPager mPager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.viewpager_layout);

            mAdapter = new MyAdapter(getSupportFragmentManager());
            mPager = (ViewPager) findViewById(R.id.viewpager);
            mPager.setAdapter(mAdapter);
            //mPager.setCurrentItem(2);

                final ActionBar ab = getSupportActionBar();
                ab.setDisplayHomeAsUpEnabled(true);
                ab.setDisplayUseLogoEnabled(false);
                ab.setDisplayShowHomeEnabled(false);


    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {

            switch(position){
            case 0: return FirstPageFragment.newInstance();

            case 1: return SecondPageFragment.newInstance();

            case 2: return ThirdPageFragment.newInstance();

            case 3: return FourthPageFragment.newInstance();

            }
            return null;
        }
    }



    public static class FirstPageFragment extends Fragment {

        public static FirstPageFragment newInstance() {
            FirstPageFragment f = new FirstPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment1, container, false);
            return V;

        }
    }

    public static class SecondPageFragment extends Fragment {

        public static SecondPageFragment newInstance() {
            SecondPageFragment f = new SecondPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment2, container, false);
            return V;

        }
    }

    public static class ThirdPageFragment extends Fragment {

        public static ThirdPageFragment newInstance() {
            ThirdPageFragment f = new ThirdPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment3, container, false);
            return V;

        }
    }


    public static class FourthPageFragment extends Fragment {

        public static ThirdPageFragment newInstance() {
            ThirdPageFragment f = new ThirdPageFragment();
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View V = inflater.inflate(R.layout.fragment4, container, false);
            return V;

        }
    }

Finally viewpager_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>

<android.support.v4.view.ViewPager
android:id="@+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

So in short What I want is:
If I click on item 3 in the list I go to screen with the details on item 3, and if I swipe to the right I get to item 4, and if I swipe to the left I get to item 2.

  • 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-17T10:41:47+00:00Added an answer on June 17, 2026 at 10:41 am

    In the onItemClickListener() you need to add an extra to the intent so you can get it when that activity launches.

    n.putExtra("POSITION_KEY", position);
    

    In the ViewPagerClass onCreate() using the

    int position = getIntent().getIntExtra("POSITION_KEY", 0);
    mPager.setCurrentItem(position);
    

    That should do what you are wanting to do.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.