如何根据用户的选择和放大器在ViewPager到setCurrentPage;我怎么知道哪些列表项称为活动 [英] How to setCurrentPage in ViewPager according to user selection & how do I know which list item called the activity

查看:314
本文介绍了如何根据用户的选择和放大器在ViewPager到setCurrentPage;我怎么知道哪些列表项称为活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的的Andr​​oid编程

我有一个项目列表中,当用户点击一个项目需要他们与该项目的细节画面。

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.

我读,我需要使用 ViewPager 的刷卡左右的能力,所以我这样做。结果
ViewPager工作正常,但我的问题,当我点击列表中的任何项目我总是在ViewPager的第一页。

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.

我不希望这样,我想的是,如果我在项目4点击它带我到第4页视图寻呼机的名单上,还是要向右滑动的能力,并留下来观看其他项目的细节。结果
我知道如何通过使用mPager.setCurrentItem(0)结果设置在 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).

下面是我的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<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

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;

        }
    }

最后viewpager_layout.xml

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"
>

因此​​,在短期我要的是:
如果我单击列表中的项目3我去与第3项的细节画面,如果我刷到我得到第4项的权利,如果我请向左滑动,我得到第2项。

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.

推荐答案

onItemClickListener()你需要额外添加到意图,所以你可以得到它的时候该活动启动。

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

ViewPagerClass 的onCreate()使用

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

这是应该做你想要做的事。

That should do what you are wanting to do.

这篇关于如何根据用户的选择和放大器在ViewPager到setCurrentPage;我怎么知道哪些列表项称为活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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