0

我使用 SherlockActionBar 选项卡创建了应用程序。我的标签之一包含 MapView (Google Maps v2)。目前我在更改选项卡时遇到问题(屏幕下方):

在此处输入链接描述

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

我的代码:

  • 我的片段.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

    公共类 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();
        }
    }
    

    }

4

1 回答 1

0

您需要一个拦截滑动的自定义查看器

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">

滑动手势应该可以工作。

于 2013-06-16T17:24:30.520 回答