阅读355 返回首页    go 阿里云 go 技术社区[云栖]


Android: ViewPager and Fragments

本文转自:https://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, andFragments.


    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 is instantiated 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. 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 is instantiated.

    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 keep track of the fragment pages in the FragmentStatePagerAdapter, 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 the MyAdapter's getFragment(int) method looks like this:

    public MyFragment 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.

最后更新:2017-04-02 16:47:43

  上一篇:go Struts文件下载
  下一篇:go Java 笔记02