2013-10-21 89 views
0

我已经实现了带有片段的标签布局,并且一切正常。 我不喜欢的唯一的事情是在选项卡中的网页视图(每当同一个网站加载时) 每当我离开标签页然后再回到它时加载网站。 有什么办法让网页视图加载一次? 我真的不知道从哪里开始...在带有片段的标签布局中的android web视图

下面是类TabInfo(辅助类),呼吁所有选项卡首次添加标签方法的代码,并且标签改变事件处理程序

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

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

     } 

private void addTab(String tag, String title, TabInfo tabInfo) { 
     TabSpec ts = getTabSpec(tag, title); 
     // Attach a Tab view factory to the spec 
     ts.setContent(new TabFactory(this)); 
     // String tag = tabSpec.getTag(); 

     // Check to see if we already have a fragment for this tab, probably 
     // from a previously saved state. If so, deactivate it, because our 
     // initial state is that a tab isn't shown. 
     tabInfo.fragment = getSupportFragmentManager().findFragmentByTag(tag); 
     if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) { 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.detach(tabInfo.fragment); 
      ft.commit(); 
      getSupportFragmentManager().executePendingTransactions(); 
     } 

     mTabHost.addTab(ts); 
    } 


    public void onTabChanged(String tag) { 

      try { 
       TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag); 
       if (mLastTab != newTab) { 
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); 
        if (mLastTab != null) { 

         if (mLastTab.fragment != null) { 
          ft.detach(mLastTab.fragment); 
         } 
        } 
        if (newTab != null) { 

         if (newTab.fragment == null) { 
          newTab.fragment = Fragment.instantiate(this, newTab.clss.getName(), newTab.args); 
          ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); 
         } else { 
          ft.attach(newTab.fragment); 
         } 

        } 

        mLastTab = newTab; 
        ft.commit(); 
        // this.getSupportFragmentManager().executePendingTransactions(); 
       } 
       // fillCommonData(); 
      } catch (Exception e) { 
       LogHelper.WriteLogError("error in onTabChanged function", e); 
      } 
     } 

感谢

+0

定制类展示你的代码请。只是我的想法,我想你使用片段和动作栏的标签。对?如果没有,你可以不读更多...在代码中,你每次调用替换片段和传递新的Fragment实例,这种方式onCreateView和onActivityCreated调用每次我想你的webView.loadUrl内的这些方法之一,这就是为什么你的webview reaload每次。在调用片段事务的tabClickListener中,您应该尝试通过标记查找片段,如果片段存在,则将其传递给带有标记的事务,如果不是,则创建它 –

+0

hello,我添加了代码 – Apperside

回答

0

这是一个猜测,但覆盖shouldOverrideUrlLoading(),并返回true,当你不想重装。

shouldOverrideUrlLoading()是WebViewClient 的方法,在您的网页流量,来电setWebViewClient并将它传递实现shouldOverrideUrlLoading

mWebView.setWebViewClient(new WebViewClient(){ 

    public boolean shouldOverrideUrlLoading(WebView webview, String url){ 
     if(/* code to determine if already loaded/tab selected */) return true; 
     return false; 
    } 

});