在单个webview中打开多个链接

时间:2016-06-09 08:17:36

标签: android

我正在创建一个包含多个网站链接的导航抽屉的项目。我想在单个webview中显示所有网站链接,以便它们在应用程序内部打开。我尝试了所有发布的解决方案,但没有任何效果。请任何人帮助我.. !!

activity_mail.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

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

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:indeterminate="false"
android:layout_gravity="center_horizontal" />

</LinearLayout>

nav_header_main.xml
<?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="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/grovi" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="GROVEUS"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.groveus.com" />

</LinearLayout>

Inside Menu Folder: activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item android:title="Academics">
        <menu>
            <item
                android:id="@+id/classmaster"
                android:icon="@drawable/iconred"
                android:title="Class Master"
            />
            <item

                android:id="@+id/admission"
                android:icon="@drawable/iconred"
                android:title="Admission"
            />
            <item

                android:id="@+id/uploadmarksheet"
                android:icon="@drawable/iconred"
                android:title="Upload Marksheet CSV"
                />
            <item

                android:id="@+id/createexam"
                android:icon="@drawable/iconred"
                android:title="Create Exam"
                />
            <item

                android:id="@+id/subjectassign"
                android:icon="@drawable/iconred"
                android:title="Subject Assign"
                />
            <item

                android:id="@+id/facultymaster"
                android:icon="@drawable/iconred"
                android:title="Faculty Master"
                />
        </menu>
        </item>
        <item android:title="SMS Master">
            <menu>
                <item
                    android:id="@+id/events"
                    android:icon="@drawable/iconred"
                    android:title="Events"
                    />
                <item

                    android:id="@+id/sendsms"
                    android:icon="@drawable/iconred"
                    android:title="Send SMS"
                    />
                <item

                    android:id="@+id/delivery"
                    android:icon="@drawable/iconred"
                    android:title="Delivery Reports"
                    />
                <item

                    android:id="@+id/schedule"
                    android:icon="@drawable/iconred"
                    android:title="Schedule SMS"
                    />
                <item

                    android:id="@+id/settings"
                    android:icon="@drawable/iconred"
                    android:title="SMS Settings"
                    />
                <item

                    android:id="@+id/freesms"
                    android:icon="@drawable/iconred"
                    android:title="Free SMS Excel"
                    />
            </menu>
        </item>
        <item android:title="Administration">
            <menu>
                <item
                    android:id="@+id/userpriviledges"
                    android:icon="@drawable/iconred"
                    android:title="User Priviledges"
                    />
                <item

                    android:id="@+id/usermaster"
                    android:icon="@drawable/iconred"
                    android:title="User Master"
                    />
                <item

                    android:id="@+id/accountdetails"
                    android:icon="@drawable/iconred"
                    android:title="Account Details"
                    />
                <item

                    android:id="@+id/thememaster"
                    android:icon="@drawable/iconred"
                    android:title="Theme Master"
                    />
            </menu>
        </item>
    </group>

</menu>


MainActivity.java
package com.example.piku.groveus;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.pushbots.push.Pushbots;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener
{


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Pushbots.sharedInstance().init(this);
        Pushbots.sharedInstance().setCustomHandler(customHandler.class);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed()
    {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.

        int id = item.getItemId();



        if (id == R.id.classmaster)
        {
            Intent b = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/class_master"));
            startActivity(b);
        }
        else if (id == R.id.admission)
        {
            Intent b = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/admission"));
            startActivity(b);
        }
        else if (id == R.id.uploadmarksheet)
        {
            Intent c = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/upload _csv"));
            startActivity(c);
        }
        else if (id == R.id.createexam)
        {
            Intent d = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/exam_master"));
            startActivity(d);
        }
        else if(id == R.id.subjectassign)
        {
            Intent e = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/subject_assign"));
            startActivity(e);
        }
        else if(id == R.id.facultymaster)
        {
            Intent f = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/faculty_master"));
            startActivity(f);
        }
        else if(id == R.id.events)
        {
            Intent g = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/events"));
            startActivity(g);
        }
        else if(id == R.id.sendsms)
        {
            Intent h = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/sms"));
            startActivity(h);
        }
        else if(id == R.id.delivery)
        {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/sms_reports"));
            startActivity(i);
        }
        else if(id == R.id.schedule)
        {
            Intent j = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/sms_log"));
            startActivity(j);
        }
        else if(id == R.id.settings)
        {
            Intent k = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/sms_alerts"));
            startActivity(k);
        }
        else if(id == R.id.freesms)
        {
            Intent l = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/fees_sms"));
            startActivity(l);
        }
        else if(id == R.id.userpriviledges)
        {
            Intent m = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/user_privileges"));
            startActivity(m);
        }
        else if(id == R.id.usermaster)
        {
            Intent n = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/user_master"));
            startActivity(n);
        }
        else if(id == R.id.accountdetails)
        {
            Intent o = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/change_pwd"));
            startActivity(o);
        }
        else if(id == R.id.thememaster)
        {
            Intent p = new Intent(Intent.ACTION_VIEW, Uri.parse("http://app.schoolbios.com/main#/theme_master"));
            startActivity(p);
        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}


MyAppWebViewClient.java
package com.example.piku.groveus;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyAppWebViewClient extends WebViewClient
{

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        if(Uri.parse(url).getHost().endsWith("schoolbios.com"))
        {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以创建自己的Web视图活动,并在选择导航项时启动活动,如下所示。

  Intent startWebViewIntent=new Intent(this,SurahWebviewActivity.class);
   startWebViewIntent.putExtra("PrayerTime",1);
   startActivity(startWebViewIntent);

但是在您的代码中,您可以创建一个方法来使用链接加载Web视图,并在选择导航项时调用该方法。它可能类似于跟随。

 public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.

        int id = item.getItemId();

        if (id == R.id.classmaster)
        {
            URI uri=Uri.parse("http://app.schoolbios.com/main#/class_master"));
            populateWebView(uri);
        }
}

private void populateWebView(URI uri){
    thisWebView.loadUrl(uri);
}