Sherlock,Fragments,Tabs和MapView

时间:2013-03-20 19:07:53

标签: android android-fragments actionbarsherlock android-mapview actionbarsherlock-map


当我在标题中指出我已经在里面实施了ActionBar Sherlock和Tab导航。标签是片段。在一个片段里面我有一个mapView。当我在标签之间切换时,我遇到了一些问题。两个选项卡只是列表,其中一个是带有其他视图的MapView,另一个带有一些设置信息。 我的问题是,当我改变标签时,我会得到一些闪烁......它足够长,可以用眼睛捕捉它。这是当我在常规选项卡中移动时,但是当我首先进入mapview选项卡时,我会看到一个持续时间比闪烁更长的黑屏。
是否有人对此或某些类似问题有一些问题?
已编辑的代码:

@Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        //setContentView(R.layout.sherlock);
        Log.i(TAG, "onCreate");
        settings_drawable = new StateListDrawable();
        settings_drawable.addState(new int [] {STATE_PRESSED}, getResources().getDrawable(R.drawable.jk_uma_button_settings_pressed));
        settings_drawable.addState(new int[] {}, getResources().getDrawable(R.drawable.jk_uma_button_settings_normal));

        home = new StateListDrawable();
        home.addState(new int [] {STATE_PRESSED}, getResources().getDrawable(R.drawable.jk_button_home_pressed));
        home.addState(new int[] {}, getResources().getDrawable(R.drawable.jk_button_home_normal));

        bar = getSupportActionBar();


        bar.setDisplayShowTitleEnabled(false);
        bar.setIcon(R.drawable.jk_uma_logo);

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setHomeButtonEnabled(true);

        tabs_adapter = new TabAdapter(this);

        tabs_adapter.addTab(bar.newTab().setText(" Home").setIcon(R.drawable.jk_icon_home), "Tab1", MapHome.class, null);
        tabs_adapter.addTab(bar.newTab().setText(" Explore").setIcon(R.drawable.jk_icon_explore), "Tab2", MapExplore.class, null);
        tabs_adapter.addTab(bar.newTab().setText(" My Views").setIcon(R.drawable.jk_icon_myview), "Tab3", MapMyStreams.class, null);
    }

public static class TabAdapter implements ActionBar.TabListener {

        private final Context context;
        private final ActionBar action_bar;
        private final HashMap<String, TabInfo> tabs = new HashMap<String, TabInfo>();
        private TabInfo last_tab = null;
        private TabInfo camera_info;

        private String current_camera_tab;
        private String current_fragment_tab;

        private class TabInfo {
            private String tag;
            private Class clss;
            private Bundle args;
            private Fragment fragment;
            private Fragment fragment_details;
            private String current_fragment;

            TabInfo(String tag, Class clazz, Bundle args, String curent) {
                this.tag = tag;
                this.clss = clazz;
                this.args = args;
                this.current_fragment = curent;
            }

        }

        public TabAdapter(SherlockFragmentActivity activity) {
            super();
            this.context = activity;
            this.action_bar = activity.getSupportActionBar();
        }

        public void addTab(ActionBar.Tab tab, String tag, Class<?> clss, Bundle args){
            Log.i(((Sherlock)context).TAG, "addTab");
            TabInfo info = new TabInfo(tag, clss, args, "1");
            tab.setTag(tag);
            tab.setTabListener(this);
            tabs.put(tag, info);
            action_bar.addTab(tab);
        }

        Handler h = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switchTab(String.valueOf(msg.obj));
            }
        };

        public void changeOnRuntime(String camera_no){
            Log.i("TABS ADAPTER", "=================changeOnRuntime========="+camera_no);
            Bundle args = new Bundle();
            args.putString("camera_no", camera_no);
            FragmentTransaction ft = ((Sherlock)context).getSupportFragmentManager().beginTransaction();

            String tag = (String) action_bar.getSelectedTab().getTag();
            TabInfo tab = tabs.get(tag);
                Fragment fragm = MapCamera.newInstance(camera_no);
                tab.fragment_details = fragm;
                tab.current_fragment = "2";
                ft.hide(tab.fragment);
                ft.add(android.R.id.content, tab.fragment_details, "Tab5");

            ft.commit();
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Log.i("TAB SELECTED", "===================NOT NULL=============="+ ft);
            if(ft == null){
                Log.i("TAB SELECTED", "===================NULLLLLLLLLLLL==============");
            }
            String tag = (String) tab.getTag();
            TabInfo newTab = (TabInfo) this.tabs.get(tag);
            if (last_tab != newTab) {

                    if (newTab != null) {
                        Log.i("TAB SELECTED", "===================NEW TAB==============");

                        if(newTab.current_fragment.equalsIgnoreCase("1")){
                            Log.i("TAB SELECTED", "===================NEW TAB 1==============");
                            if (newTab.fragment == null) {
                                Log.i("TAB SELECTED", "===================NEW TAB 1 NULL==============");
                                newTab.fragment = Fragment.instantiate(context,
                                        newTab.clss.getName(), newTab.args);
                                ft.add(android.R.id.content, newTab.fragment, newTab.tag);
                            }else {
                                Log.i("TAB SELECTED", "===================NEW TAB 1 NOT NULL==============");
                                ft.attach(newTab.fragment);
                            }
                        }else{
                            Log.i("TAB SELECTED", "===================NEW TAB 2==============");
                            ft.attach(newTab.fragment_details);
                        }


                    }
                    last_tab = newTab;
                    }                               
            }
        }

        public void switchTab(String tab){
            if(tab.equalsIgnoreCase("Tab1")){
                action_bar.setSelectedNavigationItem(0);
            }else if (tab.equalsIgnoreCase("Tab2")) {
                action_bar.setSelectedNavigationItem(1);
            }else if (tab.equalsIgnoreCase("Tab4")) {
                action_bar.setSelectedNavigationItem(3);
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            Log.i("TAB SELECTED", "=================== 2222 ==============");
            if (last_tab != null) {
                if (last_tab.fragment != null) {
                    if(last_tab.current_fragment.equalsIgnoreCase("1")){
                        Log.i("TAB SELECTED", "=================== detach 1 ==============");
                        ft.detach(last_tab.fragment);
                    }else{
                        Log.i("TAB SELECTED", "=================== detach 2 ==============");
                        ft.detach(last_tab.fragment_details);
                    }

                }
            }
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

    }

1 个答案:

答案 0 :(得分:0)

听起来像你正在描述的“黑色视图”是在片段之间切换时的一个已知问题,其中一个片段是v2地图。您可以在this post中详细了解相关信息。我自己也遇到过这个问题。引用文章中提出的解决方案非常简单。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

<!-- hack to fix ugly black artefact with maps v2 -->
<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@android:color/transparent" />

</FrameLayout> 

在透明的黑色视图上添加视图可消除此问题。不确定为什么,但确实如此。