Java 在同一活动中管理多个webview

Java 在同一活动中管理多个webview,java,android,android-layout,Java,Android,Android Layout,我是Android开发新手,我正在寻找同时管理多个webview的最佳方法 我的目标是创建一个简单的web浏览器,其中有一个小菜单,列出打开的选项卡,并用用户的选择替换布局中的内容部分。我不想在平板电脑上有像Chrome和股票浏览器这样的可视标签 管理多视图、在多视图之间切换并保持其状态的最佳方法是什么 我应该使用多个片段(每个片段包含一个webview和FragmentManager)来替换内容吗? 是否通过删除和添加选定的webview来手动管理框架布局 更新: 我今天玩了Fragment,

我是Android开发新手,我正在寻找同时管理多个webview的最佳方法

我的目标是创建一个简单的web浏览器,其中有一个小菜单,列出打开的选项卡,并用用户的选择替换布局中的内容部分。我不想在平板电脑上有像Chrome和股票浏览器这样的可视标签

管理多视图、在多视图之间切换并保持其状态的最佳方法是什么

我应该使用多个片段(每个片段包含一个webview和FragmentManager)来替换内容吗? 是否通过删除和添加选定的webview来手动管理框架布局

更新:

我今天玩了Fragment,做了一个快速测试项目。 下面是一些代码: 我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout android:id="@+id/fff"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</FrameLayout>

</RelativeLayout>
我的片段:

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

public class WebFragment extends Fragment {
    private static final String TAG = "WebFragment";
    private View v = null;
    private WebView ww;
    private String url = null;;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Log.d(TAG, "WebFragment onCreateView");

        if (v == null) {
            v = inflater.inflate(R.layout.test_layout, container, false);
            this.ww = (WebView) v.findViewById(R.id.ww);
            // WebView settings here...
            this.ww.setWebViewClient(new WebViewClient());
            if (this.url != null)
                this.ww.loadUrl(url);
        }
        return v;
    }

    public void loadUrl(String url) {
        this.url = url;
        if (this.ww != null)
            this.ww.loadUrl(url);
    }
}
通过使用我的活动菜单,我可以切换到一个或其他片段。 显然,我需要某种控制器来管理片段,并需要一个桥接器来填充活动的事件,但看起来还可以

在内存中保留多个片段(每个片段都包含这样的webview)可以吗? 我需要保留webview及其内容,但片段仅在本例中用作容器

仍然可以接受一些意见或建议


再次感谢您的帮助。

在我的一个项目中,我还必须管理多个网络视图,我通过删除和添加选定的网络视图手动管理框架布局。您好,Neevek,您每次都清除框架布局并添加网络视图吗?是的,如您在问题中所述。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
    <WebView android:id="@+id/ww"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>

public class WebFragment extends Fragment {
    private static final String TAG = "WebFragment";
    private View v = null;
    private WebView ww;
    private String url = null;;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Log.d(TAG, "WebFragment onCreateView");

        if (v == null) {
            v = inflater.inflate(R.layout.test_layout, container, false);
            this.ww = (WebView) v.findViewById(R.id.ww);
            // WebView settings here...
            this.ww.setWebViewClient(new WebViewClient());
            if (this.url != null)
                this.ww.loadUrl(url);
        }
        return v;
    }

    public void loadUrl(String url) {
        this.url = url;
        if (this.ww != null)
            this.ww.loadUrl(url);
    }
}