点击选项webview后再次重新加载

时间:2016-07-05 04:31:25

标签: java android android-layout webview

我使用webview创建了一个webapp。在这里,我有一个左侧边框,在同一网页视图中打开不同的链接。我面临的问题是,只要我点击抽屉中的任何选项,网页视图就会打开,并且会一次又一次地重新加载。在这里,我将xml和java代码粘贴到我的应用程序中。

  

主要活动文件

package com.example.dr.app3;
import android.app.ProgressDialog;
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.KeyEvent;
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 java.net.URI;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
// intialize web view
private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    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);
    //webview control content
    webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    //improve WebProformance
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//load cahe resources if requried otherwise load from network
    webView.getSettings().setAppCacheEnabled(true);//enable catch
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//makes all coloms no wider then screen
    webSettings.setUseWideViewPort(true);//
    webSettings.setSavePassword(true);//save username password
    webSettings.setSaveFormData(true);//save date and time
    webSettings.setEnableSmoothTransition(true);// smooth transition

    webView.loadUrl("http://school.techjunctionplace.in");
    //Force webview to open link in itself
    webView.setWebViewClient(new MyWebViewClient());


}

@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.dashboard) {
        // Handle the camera action
        webView.loadUrl("http://www.school.techjunctionplace.in/#/");
                } else if (id == R.id.message) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/messages");
    } else if (id == R.id.calender) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/calender");
    } else if (id == R.id.classes_schedule) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/classschedule");
    } else if (id == R.id.attendance) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/attendance");
    } else if (id == R.id.attendance_stats) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/attendanceStats");
    }else if (id == R.id.staff) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/staffAttendance");
    }else if (id == R.id.hostel) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/hostel");
    }else if (id == R.id.hostel_category) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/hostelCat");
    }else if (id == R.id.library) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/library");
    }else if (id == R.id.media_center) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/media");
    }else if (id == R.id.teachers) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/teachers");
    }else if (id == R.id.students) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/students");
    }else if (id == R.id.parents) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/parents");
    }else if (id == R.id.grade_level) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/gradeLevels");
    }else if (id == R.id.study_material) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/materials");
    }else if (id == R.id.assignments) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/assignments");
    }else if (id == R.id.exam_list) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/examsList");
    }else if (id == R.id.online_exams) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/onlineExams");
    }else if (id == R.id.news_board) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/newsboard");
    }else if (id == R.id.events) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/events");
    }else if (id == R.id.fee_types) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/feeType");
    }else if (id == R.id.fee_allocation) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/feeAllocation");
    }else if (id == R.id.payments) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/payments");
    }else if (id == R.id.expenses) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/expenses");
    }else if (id == R.id.transportation) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/transports");
    }else if (id == R.id.classes) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/classes");
    }else if (id == R.id.sections) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/sections");
    }else if (id == R.id.subjects) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/subjects");
    }else if (id == R.id.reports) {
        webView.loadUrl("http://www.school.techjunctionplace.in/#/reports");
    }

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

private class MyWebViewClient extends WebViewClient {
   @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.school.techjunctionplace.in")) {
            // open url content in web view
            return false;
        }
        // here open external url in browser
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
    //ProgressDialogue
    ProgressDialog pd = null;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        pd=new ProgressDialog(MainActivity.this);
        pd.setTitle("Please Wait..");
        pd.setMessage("It's Loading..");
        pd.show();
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        pd.dismiss();
        super.onPageFinished(view, url);
    }
}

//goto previous page on back button
@Override
public boolean onKeyDown(int keycode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keycode) {
            case KeyEvent.KEYCODE_BACK:
                if (webView.canGoBack()) {
                    webView.goBack();
                } else {
                    finish();
                }
                return true;
        }
    }

    return super.onKeyDown(keycode, event);
}}
  

侧面抽屉的XML文件

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

<group android:checkableBehavior="single">
    <item
        android:id="@+id/dashboard"
        android:icon="@drawable/ic_menu_camera"
        android:title="Dashboard" />
    <item
        android:id="@+id/message"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Messages" />
    <item
        android:id="@+id/calender"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Calender" />
    <item
        android:id="@+id/classes_schedule"
        android:icon="@drawable/ic_menu_manage"
        android:title="Classes Schedule" />
</group>

<item android:title="Attendance">
<menu>
    <item
        android:id="@+id/attendance"
        android:icon="@drawable/ic_menu_share"
        android:title="Attendance" />
    <item
        android:id="@+id/attendance_stats"
        android:icon="@drawable/ic_menu_send"
        android:title="Attendance Stats" />
    <item
        android:id="@+id/staff"
        android:icon="@drawable/ic_menu_camera"
        android:title="Staff Attendance" />
</menu>
</item>





<item android:title="Hostel Management">
    <menu>
        <item
            android:id="@+id/hostel"
            android:icon="@drawable/ic_menu_share"
            android:title="Hostel" />
        <item
            android:id="@+id/hostel_category"
            android:icon="@drawable/ic_menu_send"
            android:title="Hostel Category" />
    </menu>
</item>

<item android:title="School Info">
    <menu>
    <item
        android:id="@+id/library"
        android:icon="@drawable/ic_menu_camera"
        android:title="Library" />
    <item
        android:id="@+id/media_center"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Media Center" />
    <item
        android:id="@+id/teachers"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Teachers" />
    <item
        android:id="@+id/students"
        android:icon="@drawable/ic_menu_manage"
        android:title="Students" />
    <item
        android:id="@+id/parents"
        android:icon="@drawable/ic_menu_manage"
        android:title="Parents" />
    <item
        android:id="@+id/grade_level"
        android:icon="@drawable/ic_menu_manage"
        android:title="Grade Level" />
    <item
        android:id="@+id/study_material"
        android:icon="@drawable/ic_menu_manage"
        android:title="Study Material" />
    <item
        android:id="@+id/assignments"
        android:icon="@drawable/ic_menu_manage"
        android:title="Assigenments" />
    <item
        android:id="@+id/exam_list"
        android:icon="@drawable/ic_menu_manage"
        android:title="Exam List" />
    <item
        android:id="@+id/online_exams"
        android:icon="@drawable/ic_menu_manage"
        android:title="Online Exams" />
    <item
        android:id="@+id/news_board"
        android:icon="@drawable/ic_menu_manage"
        android:title="News Board" />
    <item
        android:id="@+id/events"
        android:icon="@drawable/ic_menu_manage"
        android:title="Events" />
    </menu>
</item>

<item android:title="Accounting">
    <menu>
        <item
            android:id="@+id/fee_types"
            android:icon="@drawable/ic_menu_share"
            android:title="Fee Types" />
        <item
            android:id="@+id/fee_allocation"
            android:icon="@drawable/ic_menu_send"
            android:title="Fee Allocation" />
        <item
            android:id="@+id/payments"
            android:icon="@drawable/ic_menu_send"
            android:title="Payments" />
        <item
            android:id="@+id/expenses"
            android:icon="@drawable/ic_menu_send"
            android:title="Expenses" />
    </menu>
</item>

<item android:title="Transportation">
   <menu>
    <item
        android:id="@+id/transportation"
        android:icon="@drawable/ic_menu_camera"
        android:title="Transportation" />
    </menu>
</item>

<item android:title="Classes">
    <menu>
        <item
            android:id="@+id/classes"
            android:icon="@drawable/ic_menu_share"
            android:title="Classes" />
        <item
            android:id="@+id/sections"
            android:icon="@drawable/ic_menu_send"
            android:title="Sections" />
    </menu>
</item>
<item android:title="Reports">
    <menu>
        <item
            android:id="@+id/subjects"
            android:icon="@drawable/ic_menu_share"
            android:title="Subjects" />
        <item
            android:id="@+id/reports"
            android:icon="@drawable/ic_menu_send"
            android:title="Reports" />
    </menu>
</item>

甚至在上面的代码中     Uri.parse(URL).getHost()。等于( “www.school.techjunctionplace.in”))

也不起作用。 我是android的初学者,请帮助确定我的代码中的实际问题。

1 个答案:

答案 0 :(得分:1)

onCreate方法中的

webView.loadUrl("http://school.techjunctionplace.in");

替换为

webView.loadUrl("http://www.school.techjunctionplace.in");

在webview客户端中进行了一些更改:

private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().contains("www.school.techjunctionplace.in")) {
                // open url content in web view
                return false;
            }
            // here open external url in browser
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        //ProgressDialogue
        ProgressDialog pd = null;

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if(pd == null) {
                pd = new ProgressDialog(HomeActivity.this);
                pd.setTitle("Please Wait..");
                pd.setMessage("It's Loading..");
                pd.show();
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if(pd != null && pd.isShowing())
                pd.dismiss();
            pd = null;
            super.onPageFinished(view, url);
        }
    }