带有MapView的ActionBar选项卡

时间:2013-06-16 17:18:47

标签: actionbarsherlock android-mapview

我使用SherlockActionBar标签创建了应用。我的标签之一包含MapView(谷歌地图v2)。目前我在更改标签时遇到问题(屏幕如下):

enter link description here

下一个标签应包含ListView。有时上下文第二个标签正确加载我也有刷我的MapView的问题。目前我只能向上和向下滑动方向。我希望有人帮助我。

我的代码:

  • myfragment.xml

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />
    

  • TabFragment_Map.java

    public class TabFragment_Map扩展了SherlockFragment {

    MapView mapView;
    GoogleMap map;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.map_fragment, container, false);
    
        mapView = (MapView) view.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
    
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);
    
        try {
            MapsInitializer.initialize(this.getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    
        return view;
    }
    
    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }
    
    @Override
    public void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }
    
    @Override
    public void onLowMemory() {
        mapView.onLowMemory();
        super.onLowMemory();
    }
    

    }

  • MainActivity.java

    公共类MainActivity扩展了SherlockFragmentActivity {

    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;
    
    String TabFragment_Cafes;
    String TabFragment_Details;
    
    //Settery & gettery używane w mechanizmie przesyłania informacji pomiędzy fragmentami
    public String getTabFragment_Cafes() {
        return TabFragment_Cafes;
    }
    
    public void setTabFragment_Cafes(String tabFragment_Cafes) {
        TabFragment_Cafes = tabFragment_Cafes;
    }
    
    public String getTabFragment_Details() {
        return TabFragment_Details;
    }
    
    public void setTabFragment_Details(String tabFragment_Details) {
        TabFragment_Details = tabFragment_Details;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
    
        final ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.DISPLAY_SHOW_HOME);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    
        mTabsAdapter = new TabsAdapter(this, mViewPager);
        mTabsAdapter.addTab(bar.newTab().setText("Mapa"), TabFragment_Map.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Kawiarnie"), TabFragment_Cafes.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Szczegóły"), TabFragment_Details.class, null);
    
        if (savedInstanceState != null)
            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
        outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
    }
    
    public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    
        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;
    
            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }
    
        public TabsAdapter(FragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = ((SherlockFragmentActivity)activity).getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }
    
        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }
    
        public void onPageScrollStateChanged(int arg0) {}
    
        public void onPageScrolled(int arg0, float arg1, int arg2) {}
    
        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }
    
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag)
                    mViewPager.setCurrentItem(i);
            }
        }
    
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
    
        public void onTabReselected(Tab tab, FragmentTransaction ft) {}
    
        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }
    
        @Override
        public int getCount() {
            return mTabs.size();
        }
    }
    

    }

1 个答案:

答案 0 :(得分:0)

您需要一个截取滑动的自定义viewpager

package com.ecs.google.maps.v2.component;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {

        // When using Maps V1       
//        if(v instanceof MapView){
//            return true;
//        }
//        return super.canScroll(v, checkV, dx, x, y);

        // When using Maps V2
        if (v.getClass().getPackage().getName().startsWith("maps.")) {
            return true;
        }
        return super.canScroll(v, checkV, dx, x, y);        
    }

}

将它放在您的布局中:

<com.ecs.google.maps.v2.component.CustomViewPager
        android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tabs"
    tools:context=".MainActivity">

滑动手势应该有效。