Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to setCurrentPage in ViewPager according to user selection & how do I know which list item called the activity
    primarykey
    data
    text
    <p>I'm new to <code>Android programming</code>.</p> <p>I have a list of items, when the user clicks on an item it takes them to a screen with that item details. </p> <p>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. </p> <p>I read that I need to use <code>ViewPager</code> for the ability to swipe right and left, so I did that.<br> ViewPager works fine, but my problem when I click any item on the list I always get to the first page in the ViewPager. </p> <p>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.<br> I know how to set the page in <code>viewpager</code> by <code>using mPager.setCurrentItem(0)</code><br> 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). </p> <p>Here is my code:</p> <p>Main activity which contains the listview:</p> <pre><code> 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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; 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); } }); } } </code></pre> <p>ViewPagerClass</p> <pre><code> 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; } } </code></pre> <p>Finally viewpager_layout.xml</p> <pre><code>&lt;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" &gt; &lt;android.support.v4.view.ViewPager android:id="@+android:id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; </code></pre> <p></p> <p></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload