Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setCurrentPage in ViewPager according to user selection & how do I know which list item called the activity

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.

like image 750
Reda Avatar asked Jan 16 '13 21:01

Reda


People also ask

How do I configure ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

Can I use ViewPager with views not with fragments?

yes...you can use View instead of Fragment in viewpager.

How do I stop ViewPager swiping?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.


1 Answers

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.

like image 84
draksia Avatar answered Nov 15 '22 00:11

draksia