5

我需要实现的是在每个选项卡中我想显示不同的网站,比如

Tab 1: http://www.stackoverflow.com
Tab 2: http://www.google..com
Tab 3. http://www.developers.android.com

我的问题是我无法将 WebView 对象访问到 loadUrl()

为了澄清我的问题,这是我的代码:

FragmentTabsPagerActivity.java

    public class FragmentTabsPagerActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{

        private TabHost mTabHost;
        private ViewPager mViewPager;
        private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, FragmentTabsPagerActivity.TabInfo>();
        private PagerAdapter mPagerAdapter;
        ...

        private void initialiseTabHost(Bundle args) {
            mTabHost = (TabHost)findViewById(android.R.id.tabhost);
            mTabHost.setup();

            TabInfo tabInfo = null;
            Bundle bundle1 = new Bundle();
            Bundle bundle2 = new Bundle();
            Bundle bundle3 = new Bundle();

            bundle1.putInt("id", 1);
            bundle2.putInt("id", 2);
            bundle3.putInt("id", 3);

            FragmentTabsPagerActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), (tabInfo = new TabInfo("Tab1", TabsFragment.class, bundle1)));
            FragmentTabsPagerActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), (tabInfo = new TabInfo("Tab2", TabsFragment.class, bundle2)));
            FragmentTabsPagerActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), (tabInfo = new TabInfo("Tab3", TabsFragment.class, bundle3)));

            this.mapTabInfo.put(tabInfo.tag, tabInfo);
            mTabHost.setOnTabChangedListener(this);
        }
}

TabsFragment.java

public class TabsFragment extends Fragment{
    LinearLayout layout;
    WebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        layout = (LinearLayout)inflater.inflate(R.layout.fragments_layout, container, false);
        mWebView = (WebView) layout.findViewById(R.id.webView1);
        WebSettings setting =mWebView.getSettings();

        mWebView.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return true;
            }
        });

        if(container == null){
            return null;
        }

        return layout;
        }

        public void setWebUrl(String url){
            mWebView.loadUrl(url);
        }
}

PagerAdapter.java

public class PagerAdapter extends FragmentPagerAdapter{

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

编辑:

片段布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
4

3 回答 3

2

我解决了这个问题。我使用下面的代码使我的选项卡动态化。

在我的TabsFragment.java添加一个id通过id = getArguments().getInt("id");

public class TabsFragment extends Fragment{
    LinearLayout layout;
    WebView mWebView;
    int id;

    public static TabsFragment newInstance(int id){
        TabsFragment tab = new TabsFragment();
        Bundle args = new Bundle();
        args.putInt("id", id);
        tab.setArguments(args);

        return tab;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        layout = (LinearLayout)inflater.inflate(R.layout.fragments_layout, container, false);
        mWebView = (WebView) layout.findViewById(R.id.webView1);
        WebSettings setting =mWebView.getSettings();

        mWebView.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return true;
            }
        });

        if(container == null){
            return null;
        }

        return layout;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        if(savedInstanceState != null){
            id = savedInstanceState.getInt("id");
        }else{
            id = getArguments().getInt("id");
        }

        Log.w("id", id+"");

        switch (id) {
        case 1:
            setWebUrl("http://www.stackoverflow.com");
            break;
        case 3:
            setWebUrl("http://www.google..com");
            break;
        case 2:
            setWebUrl("http://www.developers.android.com");
            break;

        default:
            break;
        }
    }

    public void setWebUrl(String url){
        mWebView.loadUrl(url);
    }
}

PagerAdapter.java *方法 getItem(int position) * 我创建了TabsFragment类的新实例并返回新实例。

@Override
    public Fragment getItem(int position) {
        TabsFragment tab = TabsFragment.newInstance(position+1);

        //return this.fragments.get(position);
        return tab;
    }

它完美地工作。

于 2012-05-16T07:25:03.960 回答
0

我认为问题出在这一行

layout = (LinearLayout)inflater.inflate(R.layout.fragments_layout, container, false);

你正在投射整个视图布局,LinearLayout而不是做这样的事情......

View v = inflater.inflate(R.layout.fragments_layout, container, false);
mWebView = (WebView) v.findViewById(R.id.webView1);
...
return v

希望有所帮助。

于 2012-05-14T05:49:29.457 回答
0

为什么必须使用 Tab?你不能用按钮代替吗?

单击不同的按钮,您使用 webview 加载相应的页面。

你应该知道,在你的应用程序中添加三个新的 webview 对你来说真的很糟糕。

于 2012-05-20T09:59:57.453 回答