导航抽屉打开不同的webview

时间:2014-10-03 15:23:15

标签: java android android-webview navigation-drawer

使用Android我遇到了这个问题:如何让导航抽屉菜单列表为每个列表项打开一个webview以及如何在没有列出项目列表的情况下加载默认的webview?

我已经构建了应用程序的布局。我有抽屉,我有操作栏,我有必须为我加载不同的URL(由抽屉列表项管理)的webview。

在Main.java(主要活动java文件)中,我提出了类似的内容:

    String myUrl = "http://www.mywebsite.com";

    WebView myWebView = (WebView) this.findViewById(R.id.webView);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(myUrl);

我看到的是什么:(

此外,我很少有关于如何让单个drwer列表项更改webview中的URL的线索。 或者我不应该这样做?

任何帮助,建议,教程都将受到高度赞赏。 谢谢。

2 个答案:

答案 0 :(得分:1)

  

我必须为webview添加不同的内容   URL(由抽屉列表项管理)。

浏览NavigationDrawer的培训部分,它看起来像这样:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>  

如您所见,实际的滑动菜单是ListView。所以在你的MainActivity中你会做两件事:

  1. 为此ListView
  2. 设置适配器
  3. 设置onItemClickListener
  4. 每当点击某个项目时,您都会更改WebView中加载的网址。

      

    如果没有列表项,则如何加载默认webview   挖?

    MainActivity中,只需加载默认网址即可。

答案 1 :(得分:0)

在android开发者网站Creating a Navigation Drawer上有一个非常简单实用的例子。请仔细阅读示例代码。在该代码中,他们根据导航抽屉列表的项目点击加载不同的行星图像。据我所知,您需要完全相同的功能。只需要代替行星图像,您需要加载不同的网址。

相关问题