从列表视图获取位置时,Viewpager值不会更改

时间:2013-06-12 05:25:19

标签: android android-viewpager

让我们说这是用户选择他想要看的页面的起始页

    lv.setOnItemClickListener(new OnItemClickListener(
                ) {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long arg3) {
                        SharedPreferences preferences = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
                        SharedPreferences.Editor   editor = preferences.edit();
                        editor.putInt("Position_content",position);
                        editor.commit();
                        Intent newpage = new Intent(getApplicationContext(),MainActivity.class);    
                        startActivity(newpage);
                        }});

这是我的Mainactivity.class

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fm = getSupportFragmentManager();
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
        pager.setAdapter(pagerAdapter);
        sharedPref = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        content_selection_page = sharedPref.getInt("Position_content",0);
        SharedPreferences preferences = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor   editor = preferences.edit();
        editor.putInt("final_Position_content",content_selection_page);
        editor.commit();    }    

这是MyFragmentAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
    public static final String PREFS_NAME = "MyPrefsFile";
    final int PAGE_COUNT = 52;

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);  }

    @Override
    public Fragment getItem(int arg0) {     
        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment.setArguments(data);
        return myFragment;
    }
    @Override
    public int getCount() {     
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {        
        return "Page " + ( position + 1 );  
    }   

}

这是Fragment.java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle data = getArguments();
        //** Getting integer data of the key current_page from the bundle *//*
        mCurrentPage = data.getInt("current_page", 0);
        sharedPref = this.getActivity().getSharedPreferences("myprefs", Context.MODE_PRIVATE);
        content_selection_page = sharedPref.getInt("final_Position_content",0);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.myfragment_layout, container,false);             
        TextView sub = (TextView ) v.findViewById(R.id.sub1);
        TextView sub1 = (TextView ) v.findViewById(R.id.sub2);
        TextView hd = (TextView ) v.findViewById(R.id.head);
        content_selection_page++;  //doesnt work


        switch(content_selection_page){
                switch stmts.....
                }
                return v;
                } 

真正的问题现在问题是用户通过列表选择一个选项,并且位置已经到了fragment.java中的switch case,现在viewpager似乎无法工作。假设如果我单击列表项4它会将我带到第3页,当我滑动它时幻灯片它仍然在同一页面中,我如何使它如果我滑动一旦它有每次都增加页面。 Diagramatically  而不是内容2我继续得到内容1

1 个答案:

答案 0 :(得分:3)

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

该链接帮助了我:) 看起来我错过了 mPager.setCurrentItem(position);