簡體   English   中英

滑動刷新布局時出錯

[英]error with swipe to refresh layout

感謝您看我的問題。 我正在開發一個android應用程序,它基本上是一個基於webview的應用程序。 我已經在帶有Webview的此應用中使用了導航抽屜。 我已經使用滑動刷新布局來刷新網頁,但是我遇到了一些問題。

我在這個程序中總共有9頁。 (1)主頁(2)結果(3)考試時間表(4)座位安排(5)候選人名單(6)舊問題卷(7)教學大綱(8)防止強攻(9)關於。

我的這段代碼問題:-假設我在候選列表(5)上,而我向下滑動以刷新頁面(5),但是刷新完成后,它將帶我到主頁(1)。 即使刷新頁面后,我也希望停留在頁面(5)上。 我不知道為什么會這樣以及我的代碼出了什么問題。 請幫我解決這個問題。

該視頻可以更有效地闡述我的問題:-(看一下視頻。它可以完全解釋我的問題。) http://www.mediafire.com/file/0w4l8s4xvarrxdx/Hand.mp4

鏈接到APK文件:-http: //www.mediafire.com/file/l1lxobs43f4c4tj/Gujarat_University%5B1%5D.apk

請看一下我的源代碼:-

1)navigation.java

package com.anstrontech.gujaratuniversity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.ButtonBarLayout;
import android.util.Log;
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.view.Window;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class navigation extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    WebView webView;
    SwipeRefreshLayout swipeRefreshLayout;
    private AdView mAdView;

    final String TAG = this.getClass().getName();

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


        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.addDrawerListener(toggle);
        toggle.syncState();

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

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LoadWeb();
            }
        });
        LoadWeb();

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        MobileAds.initialize(this, "ca-app-pub-5340020809562805~5856898353");


    }

    public void LoadWeb(){
        //improve webView performance

        webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setEnableSmoothTransition(true);
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("http://www.gujaratuniversity.ac.in/web/");
        webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                swipeRefreshLayout.setRefreshing(false);
            }

        });
    }

    boolean twice;
    @Override
    public void onBackPressed() {
        Log.d(TAG, "click");
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        }
        else if (twice == true){
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
            System.exit(0);
        }
        else {
            twice = true;
            Log.d(TAG, "twice: " + twice);

            Toast.makeText(navigation.this, "Press BACK again to exit", Toast.LENGTH_SHORT).show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    twice = false;
                    Log.d(TAG, "twice: " + twice);
                }
            }, 3000);
            twice = true;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation, 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.nav_home) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/");
            setTitle("Home");
        } else if (id == R.id.nav_result) {
            webView.loadUrl("http://result.gujaratuniversity.ac.in/");
            setTitle("Result");
        } else if (id == R.id.nav_examschedule) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/custom/student/examination-schedule");
            setTitle("Exam Schedule");
        } else if (id == R.id.nav_seatingarrangement) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/custom/student/seating-arrangement");
            setTitle("Seating Arrangement");
        } else if (id == R.id.nav_candidate) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/custom/student/candidate-list");
            setTitle("Candidate List");
        } else if (id == R.id.nav_oldquestionpapers) {
            webView.loadUrl("http://sampada.inflibnet.ac.in/");
            setTitle("Old Question Papers");
        } else if (id == R.id.nav_syllabus) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/custom/student/syllabus");
            setTitle("Syllabus");
        } else if (id == R.id.nav_antiragging) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/custom/pdfs/anti-ragging");
            setTitle("Anti Ragging");
        }  else if (id == R.id.nav_about) {
            webView.loadUrl("http://www.gujaratuniversity.ac.in/web/page/overview");
            setTitle("About");
        }


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


}

2)content_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.anstrontech.gujaratuniversity.navigation"
    tools:showIn="@layout/app_bar_navigation">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-5340020809562805/5619372430">
    </com.google.android.gms.ads.AdView>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="500dp">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true">

        </WebView>


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

</RelativeLayout>

3)AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.anstrontech.gujaratuniversity">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Gujarat University"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".navigation"
            android:label="Gujarat University"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

鏈接到我的完整應用程序源代碼:-http: //www.mediafire.com/file/lwm1qj415dclytd/GujaratUniversityN.zip

發生這種情況的原因是,當您向下滑動以刷新時,將調用LoadWeb() 然后在LoadWeb() ,命令WebView加載http://www.gujaratuniversity.ac.in/web/ ,這當然會導致您看到主頁。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM