本文转自:http://tamsler.blogspot.co.uk/search/label/viewpager
  1. The following is a simple Android sample application that uses a ViewPager, which is part
    of the Support Package,and Fragments.


    The FragmentPagerActivity class creates the ViewPager and the associated FragmentPagerAdapter.
    import android.os.Bundle;   import android.support.v4.app.Fragment;   import android.support.v4.app.FragmentActivity;   import android.support.v4.app.FragmentManager;   import android.support.v4.app.FragmentPagerAdapter;   import android.support.v4.view.ViewPager;      public class FragmentPagerActivity extends FragmentActivity {           private static final int NUMBER_OF_PAGES = 10;           private ViewPager mViewPager;        private MyFragmentPagerAdapter mMyFragmentPagerAdapter;           public void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);             setContentView(R.layout.main);             mViewPager = (ViewPager) findViewById(R.id.viewpager);             mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());             mViewPager.setAdapter(mMyFragmentPagerAdapter);        }           private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {                public MyFragmentPagerAdapter(FragmentManager fm) {                  super(fm);             }                @Override             public Fragment getItem(int index) {                     return PageFragment.newInstance("My Message " + index);           }                @Override             public int getCount() {                     return NUMBER_OF_PAGES;             }        }   }  

    1. The PageFragment only contains a TextView. It isinstantiated in the above
      MyFragmentPagerAdapter's getItem(int index) method.
    package org.thomasamsler.android;      import android.os.Bundle;   import android.support.v4.app.Fragment;   import android.view.LayoutInflater;   import android.view.View;   import android.view.ViewGroup;   import android.widget.TextView;      public class PageFragment extends Fragment {                public static PageFragment newInstance(String title) {            PageFragment pageFragment = new PageFragment();          Bundle bundle = new Bundle();          bundle.putString("title", title);          pageFragment.setArguments(bundle);          return pageFragment;      }              @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);        }                @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {                         View view = inflater.inflate(R.layout.fragment, container, false);            TextView textView = (TextView) view.findViewById(R.id.textView1);            textView.setText(getArguments().getString("title"));          return view;        }   }     

    1. Sources:

      • Fragments
      • Horizontal View Swiping with ViewPager
      • ViewPagerExample

  1. What are the different ways to get a reference to the currently visible fragment page in a ViewPager?


    First Solution
    You can set a unique tag on each fragment page:
    getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();

    ... and then retrieve the fragment page via:
    getSupportFragmentManager().findFragmentByTag("Some Tag");

    Using this approach, you need to keep track of the string tags and associate them with all the fragment pages. You could use a map to store each tag along with the current page index, which is set at the time when the fragment page isinstantiated.
    MyFragment myFragment = MyFragment.newInstance(); mPageReferenceMap.put(index, "Some Tag"); getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();
    To get the tag for the currently visible page, you then call:
    int index = mViewPager.getCurrentItem(); String tag =mPageReferenceMap.get(index);
    ... and then get the fragment page:
    Fragment myFragment =getSupportFragmentManager().findFragmentByTag(tag);

    Second Solution
    Similar to the first solution, you keep track of all the "active" fragment pages. In this case, you keeptrack of the fragment pages in theFragmentStatePagerAdapter, which is used by the ViewPager.
    public Fragment getItem(int index) { Fragment myFragment = MyFragment.newInstance(); mPageReferenceMap.put(index, myFragment); return myFragment; }

    To avoid keeping a reference to "inactive" fragment pages, we need to implement the FragmentStatePagerAdapter's destroyItem(...) method:
    publicvoid destroyItem(View container, int position, Object object) { super.destroyItem(container, position, object); mPageReferenceMap.remove(position); }
    ... and when you need to access the currently visible page, you then call:
    int index = mViewPager.getCurrentItem(); MyAdapter adapter= ((MyAdapter)mViewPager.getAdapter()); MyFragment fragment = adapter.getFragment(index);
    ... where theMyAdapter'sgetFragment(int) method looks like this:
    publicMyFragment getFragment(int key) { returnmPageReferenceMap.get(key); }

    I am using the second solution in myproject, and it's working quite well. Here is the reference to the fullsource code.
    代码:http://download.csdn.net/detail/jdsjlzx/4395451

更多相关文章

  1. Android网格布局实现--recyclerview
  2. Android(安卓)常用代码总结 工具类
  3. Android(安卓)Studio Service 篇一
  4. Ubuntu 10.04编译Android(安卓)2.2 源代码
  5. Android代码混淆
  6. BaseAdapter
  7. Android(安卓)Spinner控件的简单应用
  8. 调用android系统自带功能
  9. 关于android的des算法代码

随机推荐

  1. 学会它再也不怕编辑数学公式
  2. 到公司做华为认证网络工程师有发展前途吗
  3. Web | 什么是 RPC ?
  4. 5G网络工程师是不是需要长期出差
  5. 【翻译】JavaScript引用是如何工作的?
  6. 如果在运营商做核心网网络工程师的话,工资
  7. 【源码实例】纯CSS水波纹效果和动画气泡
  8. 华为ie认证难不难
  9. 作为一名前端开发者,你有必要知道这些项目
  10. web开发中的长度单位(px,em,ex,rem),如何运