Basim Sherif
Basim Sherif

Reputation: 5440

Supportmapfragment inside viewpager

Can anyone provide best working example to use supportmapfragment inside viewpager? I have tried many tutorials but I am not able to implement it. I just need the fragment, and its layout. I know how to setup viewpager. Thanks in advance.

Upvotes: 0

Views: 1246

Answers (3)

Atish Agrawal
Atish Agrawal

Reputation: 2877

I was searching for some help on supportmapfrgment over stackoverflow, where upon i landed here.

I am currently working on a project like that.

Please feel free to contact me if you still need it now.

Upvotes: -1

bofredo
bofredo

Reputation: 2348

in my onCreate i do the following:

mViewPager = new CustomViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);

final FragmentManager fm = getSupportFragmentManager();

mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {

            @Override
            public int getCount() {
                return mContactList.size();
            }

            @Override
            public Fragment getItem(int pos) {

                    Contact mContact = mContactList.get(pos);
                    int mCurrentPosition = mContact.getPosition();
                    return ContactDetailsFragment.newInstance(mCurrentPosition);                 
            }
        });

  //loads the proper fragment on its meant-to-be position into the ViewPager
  mViewPager.setCurrentItem(position);

in my ContactDetailFragment i have this method:

public static ContactDetailsFragment newInstance(int position) {
    Bundle args = new Bundle();
    args.putInt(EXTRA_CONTACT_ID, position);

    Log.i("ContactDetailsFragment","EXTRA_CONTACT_ID = "+EXTRA_CONTACT_ID);

    ContactDetailsFragment mCdf = new ContactDetailsFragment();
    mCdf.setArguments(args);

    return mCdf;
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007349

"Nothing seems to work" is a useless description of your symptoms. In the future, please feel free to actually explain what is going wrong.

That being said, here is a sample project showing the use of SupportMapFragment in a ViewPager. Note that you need to use a subclass of ViewPager, overriding canScroll() to ensure that the maps can still be swiped horizontally.

public class MapAwarePager extends ViewPager {
  public MapAwarePager(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected boolean canScroll(View v, boolean checkV, int dx, int x,
                              int y) {
    if (v instanceof SurfaceView || v instanceof PagerTabStrip) {
      return(true);
    }

    return(super.canScroll(v, checkV, dx, x, y));
  }
}

Upvotes: 2

Related Questions