本文介绍了所谓的onCreate为每一个被选中的标签页时两个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有碎片和ActionBarSherlock建立了一个标签式的应用程序。我有7个标签。这里发生了什么。

当我选择任何选项卡,为相关片段onCreate方法如预期的那样叫。问题是,onCreate方法被称为用于下一相邻标签为好。例如:


  1. 应用始于TAB1和预期的onCreate被称为

  2. TAB2的onCreate被称为好(不应该发生)

  3. -------

  4. 单击上TAB2和的onCreate被称为预期(尽管它已经被调用)

  5. TAB3的onCreate被称为好(不应该发生)

  6. -------

  7. 单击上tab6和的onCreate被称为有望

  8. tab7的onCreate被称为好(不应该发生)

  9. -------

  10. 而且很奇怪,点击tab7(最后一个标签)

  11. tab6(2日至最后一个选项卡)的onCreate被称为以及(不应发生)

我读了几个可能的问题和检查,以确保它不会发生在这里:


  1. 不使用唯一的标签为每个标签(它们是唯一的)

  2. 模拟器错误调用的onCreate两次(我得到我的ICS的设备上相同的行为)

因此​​,这不是两个previous的可能性,我会完全没了主意。该程序运行正常,但装载两个片段(这是网页视图)需要太多的时间,而不是我所期望的行为。

下面是我的code,对于它创建标签主机的主要活动的onCreate:
编辑:

 公共类SynergyWorldwideActivity扩展SherlockFragmentActivity
{
// TabHost mTab​​Host;
ViewPager mViewPager;
TabsAdapter mTab​​sAdapter;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    //设置视图寻呼机
    的setContentView(R.layout.fragment_tabs_pager);
    mViewPager =(ViewPager)findViewById(R.id.pager);    //设置操作栏
    最后的动作条酒吧= getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowTitleEnabled(真);
    //bar.setDisplayShowHomeEnabled(false);    //科瑞标签捆绑网址
    捆绑tab1Args =新包(),tab2Args =新包(),tab3Args =新包()
            tab4Args =新包(),tab5Args =新包(),tab6Args =新包(),tab7Args =新包();
    tab1Args.putString(tabURL的getString(R.string.webtab1_URL));
    tab2Args.putString(tabURL的getString(R.string.webtab2_URL));
    tab3Args.putString(tabURL的getString(R.string.webtab3_URL));
    tab4Args.putString(tabURL的getString(R.string.webtab4_URL));
    tab5Args.putString(tabURL的getString(R.string.webtab5_URL));
    tab6Args.putString(tabURL的getString(R.string.webtab6_URL));
    tab7Args.putString(tabURL的getString(R.string.webtab7_URL));    mTabsAdapter =新TabsAdapter(这一点,mViewPager);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab1_name))
            WebTabFragment.MyWebviewFragment.class,tab1Args);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab2_name))
            WebTabFragment.MyWebviewFragment.class,tab2Args);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab3_name))
            WebTabFragment.MyWebviewFragment.class,tab3Args);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab4_name))
            WebTabFragment.MyWebviewFragment.class,tab4Args);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab5_name))
            WebTabFragment.MyWebviewFragment.class,tab5Args);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab6_name))
            WebTabFragment.MyWebviewFragment.class,tab6Args);
    mTabsAdapter.addTab(bar.newTab()的setText(的getString(R.string.webtab7_name))
            WebTabFragment.MyWebviewFragment.class,tab7Args);    如果(savedInstanceState!= NULL){
        bar.setSelectedNavigationItem(savedInstanceState.getInt(标签,0));
    }
}@覆盖
保护无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    outState.putInt(标签,getActionBar()getSelectedNavigationIndex());
}/ **
 *这是实现标签和所有的管理的辅助类
 *连接ViewPager关联TabHost的细节。它依赖于
 *把戏。通常,标签主机有一个简单的API,用于提供视图或
 *意图,每个标签将显示。这是不够的,用于切换
 *页面之间。因此,不是我们做的标签主机的部分内容
 * 0dp高(它未示出)和TabsAdapter提供其自己的虚设
 *视图,以显示作为标签内容。它监听到标签的变化,并采取
 *护理开关在ViewPager正确的分页中,只要选定
 *标签的变化。
 * /
公共静态类TabsAdapter扩展FragmentPagerAdapter实现ActionBar.TabListener,ViewPager.OnPageChangeListener {
    私人最终上下文mContext;
    //私人最终TabHost mTab​​Host;
    私人最终动作条mActionBar;
    私人最终ViewPager mViewPager;
    私人最终的ArrayList< TabInfo> mTabs =新的ArrayList< TabInfo>();    静态final类TabInfo {
        私人最终级<> CLSS;
        私人最终捆绑ARGS;        TabInfo(类<> _class,捆绑_args){
            CLSS = _class;
            ARGS = _args;
        }
    }    公共TabsAdapter(FragmentActivity活动,ViewPager寻呼机){
        超级(activity.getSupportFragmentManager());
        mContext =活动;
        mActionBar =((SherlockFragmentActivity)活性).getSupportActionBar();
        mViewPager =寻呼机;
        mViewPager.setAdapter(本);
        mViewPager.setOnPageChangeListener(本);
  }    公共无效addTab(ActionBar.Tab标签,类<> CLSS,捆绑参数){
        TabInfo信息=新TabInfo(CLSS,参数);
        tab.setTag(信息);
        tab.setTabListener(本);
        mTabs.add(信息);
        mActionBar.addTab(标签);
        notifyDataSetChanged();
}    @覆盖
    公众诠释getCount将()
    {
        诠释ICOUNT = mTab​​s.size();
        返回ICOUNT;
    }    @覆盖
    公共片段的getItem(INT位置)
    {
        TabInfo信息= mTab​​s.get(位置);
        返回Fragment.instantiate(mContext,info.clss.getName(),info.args);
    }    @覆盖
    公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels)
    {
    }    @覆盖
    公共无效使用onPageSelected(INT位置)
    {
        mActionBar.setSelectedNavigationItem(位置);
    }    @覆盖
    公共无效onPageScrollStateChanged(INT状态)
    {
    }
    @覆盖
    公共无效onTabSelected(标签选项卡)
    {
        对象标记= tab.getTag();
        的for(int i = 0; I< mTab​​s.size();我++){
            如果(mTabs.get(ⅰ)==标签){
                mViewPager.setCurrentItem(ⅰ);
            }
        }
    }    @覆盖
    公共无效onTabUnselected(标签选项卡)
    {
    }    @覆盖
    公共无效onTabReselected(标签选项卡)
    {
    }}
}

这里的code的标签片段:
编辑:

 公共类WebTabFragment扩展SherlockFragmentActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    如果(savedInstanceState == NULL)
    {
       FragmentManager FM = getSupportFragmentManager();
        如果(fm.findFragmentById(android.R.id.content)== NULL){
            MyWebviewFragment myWebView =新MyWebviewFragment();
            。fm.beginTransaction()加(android.R.id.content,myWebView).commit();
        }
    }
}@覆盖
公共无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    //outState.putString(\"tabNumber,mTabNumber);
}
公共静态类MyWebviewFragment扩展SherlockFragment {
    最终静态私人字符串标记= MyWebviewFragment.class.getSimpleName();
    串mTabURL;
    私人的WebView mWebView = NULL;
    静态最终诠释REFRESH_ID = Menu.FIRST;
    私人ProgressDialog spinnerDlg;    @覆盖
    公共无效的onSaveInstanceState(捆绑outState)
    {
        如果(mWebView.saveState(outState)== NULL)
            Log.i(标签,节能状态失败!);
        其他
            Log.i(标签,保存状态成功了。);
    }    @覆盖
    公共无效onActivityCreated(捆绑savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(真);
    }    @覆盖公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
        menu.add(Menu.NONE,REFRESH_ID,0,的getString(R.string.refresh_string))
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);    }    @覆盖公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
            案例REFRESH_ID:
                如果(mWebView!= NULL)
                    mWebView.reload();
                返回true;            默认:
                返回super.onOptionsItemSelected(项目);
        }
    }    / **
     *在创建,检索它的参数此实例的数量。
     * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //告诉框架,尽量保持这个片段各地
        //配置更改时。
        setRetainInstance(真);        mTabURL = getArguments()!= NULL? getArguments()的getString(tabURL):http://www.google.com。
    }    / **
     *片段的UI仅仅是展示其简单的文本视图
     *实例号。
     * /
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){        //创建视图对​​象返回
        视图V = inflater.inflate(R.layout.webview_layout,集装箱,FALSE);        //设置的WebView对象
        如果(mWebView!= NULL){
            mWebView.destroy();
        }
        mWebView =(的WebView)v.findViewById(R.id.webview_fragment);
        mWebView.getSettings()setJavaScriptEnabled(真)。
        mWebView.setOnKeyListener(新OnKeyListener(){
            @覆盖
            公共布尔安其(视图V,INT键code,KeyEvent的事件)
            {
                如果((键code == KeyEvent.KEY code_BACK)及和放大器; mWebView.canGoBack()){
                    mWebView.goBack();
                    返回true;
                }
                返回false;
            }        });        //检查,看它是否已被保存,如果真还原
        如果(savedInstanceState!= NULL)
        {
            如果(savedInstanceState.isEmpty())
                Log.i(标签,因为捆绑是空的无法恢复的状态。);
            其他
            {
                如果(mWebView.restoreState(savedInstanceState)== NULL)
                    Log.i(标签,恢复状态失败!);
                其他
                    Log.i(标签,恢复状态成功了。);
            }        }
        其他
        {
            //加载网页
            mWebView.setWebViewClient(新MyWebViewClient());
            mWebView.getSettings()setPluginsEnabled(真)。
            。mWebView.getSettings()setBuiltInZoomControls(假);
            。mWebView.getSettings()setSupportZoom(假);
            。mWebView.getSettings()setJavaScriptCanOpenWindowsAutomatically(真);
            。mWebView.getSettings()setAllowFileAccess(真);
            mWebView.getSettings()setDomStorageEnabled(真)。
            mWebView.loadUrl(mTabURL);        }
        返回伏;
    }    @覆盖
    公共无效的onDestroy()
    {
        super.onDestroy();
    }    @覆盖
    公共无效onDestroyView()
    {
        super.onDestroyView();
    }    @覆盖
    公共无效的onPause()
    {
        super.onPause();
    }    @覆盖
    公共无效onResume()
    {
        super.onResume();
    }
    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG)
    {
        // TODO自动生成方法存根
        super.onConfigurationChanged(NEWCONFIG);
    }
    公共类MyWebViewClient扩展WebViewClient {
        @覆盖
        公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
        //的YouTube视频链接
            如果(url.startsWith(http://youtu.be))
            {
                串urlSubString = url.substring(http://youtu.be/。长度());
                字符串的newURL =的String.format(http://www.youtube.com/v/%s,urlSubString);
                startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(的newURL)));
                返回(真);
            }            返回(假);
        }        @覆盖
        公共无效onPageStarted(的WebView视图,字符串URL,位图图标){
            super.onPageStarted(查看,网址,图标);            如果(spinnerDlg == NULL)
            {
                spinnerDlg =新ProgressDialog(getActivity());
                spinnerDlg.setMessage(加载......);
                spinnerDlg.show();
            }
        }        @覆盖
        公共无效onPageFinished(的WebView视图,字符串URL){
            super.onPageFinished(查看,网址);            如果(spinnerDlg!= NULL)
            {
                spinnerDlg.dismiss();
            }
            spinnerDlg = NULL;
       }
    }
}
}


解决方案

这就是标签的属性之一。它加载第二个这样你就不会滑动到一个空标签..如果你想不客气覆盖选项卡类并处理自己。:D

I have a tabbed application built with fragments and ActionBarSherlock. I have 7 tabs. Here's what's happening.

When I select any tab, the onCreate method for the associated fragment is called as expected. The problem is that the onCreate method is called for the next adjacent tab as well. For instance:

  1. App starts in tab1 and onCreate is called as expected
  2. tab2 onCreate is called as well (should not happen)
  3. -------
  4. Click on tab2 and onCreate is called as expected (even though it's already been called)
  5. tab3 onCreate is called as well (should not happen)
  6. -------
  7. Click on tab6 and onCreate is called as expected
  8. tab7 onCreate is called as well (should not happen)
  9. -------
  10. And really weird, click on tab7 (the last tab)
  11. tab6 (2nd to last tab) onCreate is called as well (should not happen)

I've read a couple of possible problems and checked to make sure it's not happening here:

  1. Not using unique tag for each tab (they are unique)
  2. Emulator has bug that calls onCreate twice (I get the same behavior on my ICS device)

So it's not the two previous possibilities and I'll completely out of ideas. The program runs fine but loading two fragments (which are webviews) takes too much time and isn't the behavior I expected.

Here's my code for the main activity onCreate which creates the tab host:EDITED:

public class SynergyWorldwideActivity extends SherlockFragmentActivity
{
//TabHost mTabHost;
ViewPager  mViewPager;
TabsAdapter mTabsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set up the view pager
    setContentView(R.layout.fragment_tabs_pager);
    mViewPager = (ViewPager)findViewById(R.id.pager);

    // Set up action bar
    final ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowTitleEnabled(true);
    //bar.setDisplayShowHomeEnabled(false);

    // Creat tabs with bundled URLs
    Bundle  tab1Args=new Bundle(), tab2Args=new Bundle(), tab3Args=new Bundle(),
            tab4Args=new Bundle(), tab5Args=new Bundle(), tab6Args=new Bundle(),        tab7Args=new Bundle();
    tab1Args.putString("tabURL", getString(R.string.webtab1_URL));
    tab2Args.putString("tabURL", getString(R.string.webtab2_URL));
    tab3Args.putString("tabURL", getString(R.string.webtab3_URL));
    tab4Args.putString("tabURL", getString(R.string.webtab4_URL));
    tab5Args.putString("tabURL", getString(R.string.webtab5_URL));
    tab6Args.putString("tabURL", getString(R.string.webtab6_URL));
    tab7Args.putString("tabURL", getString(R.string.webtab7_URL));

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab1_name)),
            WebTabFragment.MyWebviewFragment.class, tab1Args);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab2_name)),
            WebTabFragment.MyWebviewFragment.class, tab2Args);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab3_name)),
            WebTabFragment.MyWebviewFragment.class, tab3Args);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab4_name)),
            WebTabFragment.MyWebviewFragment.class, tab4Args);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab5_name)),
            WebTabFragment.MyWebviewFragment.class, tab5Args);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab6_name)),
            WebTabFragment.MyWebviewFragment.class, tab6Args);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.webtab7_name)),
            WebTabFragment.MyWebviewFragment.class, tab7Args);

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}

/**
 * This is a helper class that implements the management of tabs and all
 * details of connecting a ViewPager with associated TabHost.  It relies on a
 * trick.  Normally a tab host has a simple API for supplying a View or
 * Intent that each tab will show.  This is not sufficient for switching
 * between pages.  So instead we make the content part of the tab host
 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
 * view to show as the tab content.  It listens to changes in tabs, and takes
 * care of switch to the correct paged in the ViewPager whenever the selected
 * tab changes.
 */
public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener{
    private final Context mContext;
    //private final TabHost mTabHost;
    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();
}

    @Override
    public int getCount()
    {
        int iCount = mTabs.size();
        return iCount;
    }

    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {
    }

    @Override
    public void onPageSelected(int position)
    {
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state)
    {
    }


    @Override
    public void onTabSelected(Tab tab)
    {
        Object tag = tab.getTag();
        for (int i=0; i<mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab)
    {
    }

    @Override
    public void onTabReselected(Tab tab)
    {
    }

}
}

Here's the code for the tab fragments:EDITED:

public class WebTabFragment extends SherlockFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState == null)
    {
       FragmentManager fm = getSupportFragmentManager();
        if (fm.findFragmentById(android.R.id.content) == null) {
            MyWebviewFragment myWebView = new MyWebviewFragment();
            fm.beginTransaction().add(android.R.id.content, myWebView).commit();
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //outState.putString("tabNumber", mTabNumber);
}


public static class MyWebviewFragment extends SherlockFragment {
    final static private String tag = MyWebviewFragment.class.getSimpleName();
    String mTabURL;
    private WebView mWebView = null;
    static final int REFRESH_ID = Menu.FIRST;
    private ProgressDialog spinnerDlg;

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        if(mWebView.saveState(outState) == null)
            Log.i(tag,"Saving state FAILED!");
        else
            Log.i(tag, "Saving state succeeded.");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(Menu.NONE, REFRESH_ID, 0, getString(R.string.refresh_string))
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    }

    @Override public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case REFRESH_ID:
                if(mWebView != null)
                    mWebView.reload();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Tell the framework to try to keep this fragment around
        // during a configuration change.
        setRetainInstance(true);

        mTabURL = getArguments() != null ? getArguments().getString("tabURL") : "http://www.google.com";
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Create view object to return
        View v = inflater.inflate(R.layout.webview_layout, container, false);

        // Set up webview object
        if (mWebView != null) {
            mWebView.destroy();
        }
        mWebView = (WebView)v.findViewById(R.id.webview_fragment);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setOnKeyListener(new OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                    mWebView.goBack();
                    return true;
                }
                return false;
            }

        });

        // Check to see if it has been saved and restore it if true
        if(savedInstanceState != null)
        {
            if (savedInstanceState.isEmpty())
                Log.i(tag, "Can't restore state because bundle is empty.");
            else
            {
                if (mWebView.restoreState(savedInstanceState) == null)
                    Log.i(tag, "Restoring state FAILED!");
                else
                    Log.i(tag, "Restoring state succeeded.");
            }

        }
        else
        {
            // Load web page
            mWebView.setWebViewClient(new MyWebViewClient());
            mWebView.getSettings().setPluginsEnabled(true);
            mWebView.getSettings().setBuiltInZoomControls(false);
            mWebView.getSettings().setSupportZoom(false);
            mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebView.getSettings().setAllowFileAccess(true);
            mWebView.getSettings().setDomStorageEnabled(true);
            mWebView.loadUrl(mTabURL);

        }
        return v;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
    }

    @Override
    public void onPause()
    {
        super.onPause();
    }

    @Override
    public void onResume()
    {
        super.onResume();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }


    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // YouTube video link
            if (url.startsWith("http://youtu.be"))
            {
                String urlSubString = url.substring("http://youtu.be/".length());
                String newURL = String.format("http://www.youtube.com/v/%s", urlSubString);
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(newURL)));
                return (true);
            }

            return (false);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            if(spinnerDlg == null)
            {
                spinnerDlg = new ProgressDialog(getActivity());
                spinnerDlg.setMessage("Loading....");
                spinnerDlg.show();
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            if(spinnerDlg != null)
            {
                spinnerDlg.dismiss();
            }
            spinnerDlg = null;
       }
    }
}
}
解决方案

Thats one of the properties of tabs. It loads the second one so you aren't swiping to a null tab.. You're welcome to override the tab class and handle that yourself if you want :D.

这篇关于所谓的onCreate为每一个被选中的标签页时两个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 20:05