在同一活动中管理多个webview

时间:2012-12-03 01:06:11

标签: java android android-layout

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

我的目标是创建一个简单的网络浏览器,其中包含一个小菜单,列出我打开的“标签”,并根据用户的选择替换我的布局中的内容部分。我不想在平板电脑上安装Chrome和库存浏览器等可见标签。

管理多个视图,在它们之间切换并保持状态的最佳方法是什么?

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

更新

我今天和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>

我的活动:

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private WebFragment f1;
    private WebFragment f2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    f1 = new WebFragment();
    f2 = new WebFragment();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.add(R.id.fff, f1);
    fragmentTransaction.commit();

    f1.loadUrl("file:///android_asset/home.html");
    f2.loadUrl("file:///android_asset/home.html");


}

private void switchFragment(int id) {
    FragmentTransaction trx = getFragmentManager().beginTransaction();
    if (id == 1) {
        trx.replace(R.id.fff, f1);
    } else if (id == 2) {
        trx.replace(R.id.fff, f2);
    }
    trx.commit();

}
...
}

我的片段:

<?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及其内容,但片段仅用作容器。

仍然接受一些意见或建议。

再次感谢您的帮助。

0 个答案:

没有答案
相关问题