使用ViewPager和GoogleMap V2 - 即使在地图上滑动(如果禁用地图滚动),如何启用页面滑动以更改页面

时间:2013-12-22 22:11:08

标签: android google-maps android-viewpager

所以我在地图上禁用滚动但同时,当我尝试向左和向右滚动以在ViewPager中交换页面时,在ViewPager中无法识别滑动,因为我认为它仍然认为我是试图滚动地图。我该怎么做呢?

public class VenueDetailsActivity extends SherlockFragmentActivity{
     private static final String[] CONTENT = new String[] { "Camera", "Description", "Menu", "Contact", "Specials" };

 private Venue venue;
 private CameraFragment camera = null;
 private ContactFragment contact = null;
 private DescriptionFragment desc = null;
 private MenuFragment menu = null;
 private SpecialsFragment specials = null;
 private ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.venue_details);
        initialize();
    }

    private void initialize(){

        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setHomeButtonEnabled(true);

        Intent intent = getIntent();
        Bundle b = getIntent().getExtras();

        //get the Venue that was passed.
        venue = (Venue) b.getParcelable("venue");

        actionBar.setTitle(venue.getName());

        //create a fragment adapter object
        FragmentPagerAdapter adapter = new MyAdapter(getSupportFragmentManager());
        //find the viewpager in the view venue_details
        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        //set the pager to keep total length of Content windows -1 screen fragments( this helps to not redraw the cameras every time you move thrugh tabs )
        pager.setOffscreenPageLimit(CONTENT.length-1);
        //set adapter for the pager.
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);
    }

    /* This method handles the each click on the drawable menu */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /* Adapter CLASS to help with fragments */
    class MyAdapter extends FragmentPagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            int var = position % CONTENT.length;
            switch(var){
                case 0:
                    //check to see if there is an instance already
                    if(camera == null){
                        camera = CameraFragment.newInstance(venue);
                    }
                    return camera; 
                case 1:
                    if(desc==null){
                        desc = DescriptionFragment.newInstance(venue);
                    }
                    return desc;
                case 2:
                    if(menu == null){
                        menu = MenuFragment.newInstance(venue);
                    }
                    return menu;
                case 3:
                    if(contact == null){
                        contact = ContactFragment.newInstance(venue);
                    }
                    return contact;
                case 4:
                    if(specials == null){
                        specials = SpecialsFragment.newInstance(venue);
                    }
                    return specials;    
            }
            return CameraFragment.newInstance(venue);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

      @Override
        public int getCount() {
          return CONTENT.length;
        }
    }
}

这是我要确保即使在地图上滑动也会改变我的碎片的一个片段。

public class ContactFragment extends Fragment{
    private static final String KEY_CONTENT = "ContactFragment:Content";
    private SupportMapFragment fragment;
    private GoogleMap map;
    private LatLng venueLatLng ;

    private static Venue venue;

    public static ContactFragment newInstance(Venue venuePassed) {
        ContactFragment fragment = new ContactFragment();
        venue = venuePassed;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //create a LatLng object used to send coordinates to Google Maps
        venueLatLng = new LatLng(venue.getLocationCoordinates().getLatitude(), venue.getLocationCoordinates().getLongitude());
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //get the child fragment manager
        FragmentManager fm = getChildFragmentManager();
        //find google maps's fragment that is inserted in the xml
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
        if (fragment == null) {
            //create a new instance of the support fragment
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map, fragment).commit();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (map == null) {
            map = fragment.getMap();
            Marker venueMarker = map.addMarker(new MarkerOptions().position(venueLatLng)
                    .title(venue.getName())
                    .snippet("Kiel is cool")
                    .draggable(false));
            venueMarker.showInfoWindow();
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(venueLatLng, 15));
            map.getUiSettings().setScrollGesturesEnabled(false);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contact, 
                container, false);
        TextView contact_text = (TextView)view.findViewById(R.id.contact_text);
        contact_text.setText(venue.getAddress());
        TextView phone_number = (TextView)view.findViewById(R.id.phone_number);
        phone_number.setText(venue.getPhoneNumber());
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);;
    }


}

2 个答案:

答案 0 :(得分:0)

如果您不希望ViewPager中的任何内容可点击,请尝试继承ViewPager。覆盖dispatchTouchEvent(MotionEvent e),并从中返回false以防止ViewPager的子项声明触摸事件。

答案 1 :(得分:0)

我认为您的问题来源于他们加载SupportMapFragment的方式。您应该扩展SupportMapFragment而不是Fragment,并像Map一样自定义onResume()