简体   繁体   中英

Overwriting WebView with progress change of SeekBar

I created a WebView which loads google.com as test. And at the bottom of the Layout ist a SeekBar. If the Seekbar is changed to progress of 2 it shall for example load another page like stackoverflow.com. If its progress is changed to 3,it shall load another page ( android.com)

My existing code is the following. But I dont know how to fill the listener now. Also with existing solutions it did not work.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <WebView android:id="@+id/Viewing"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <SeekBar
        android:id="@+id/seitenSwitcher"
        android:layout_above="@+id/seitenSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:max="15"
        android:progress="0"
        android:paddingLeft="4dip"
        android:paddingRight="4dip"

        />
</RelativeLayout>

main.java

package testprojekt.homepageapp.application;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.SeekBar;

public class main extends Activity {

    private WebView webv;


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


        webv = (WebView)findViewById(R.id.Viewing);
        webv.setWebViewClient(new WebViewClient());
        webv.getSettings().setJavaScriptEnabled(true);
        webv.loadUrl("http://www.google.de");
    }


    public void onProgressChanged(SeekBar seitenSwitcher, int progress, boolean true) {
        seitenSwitcher = (SeekBar) findViewById(R.id.seitenSwitcher);
        seitenSwitcher.setOnSeekBarChangeListener(new sbListener());

    }

}

sbListener.java

package testprojekt.homepageapp.application;

import android.widget.SeekBar;


public class sbListener implements SeekBar.OnSeekBarChangeListener {

   ???

}

You need a reference to webv in the listener. You can pass it into the constructor of sbListener , or you can remove the sbListener class and do something like:

public class main extends Activity {

    private WebView webv;
    private SeekBar seitenSwitcher;
    private String[] websites = { "http://www.google.de", "http://stackoverflow.com", "http://www.android.com" };

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

        webv = (WebView)findViewById(R.id.Viewing);
        webv.getSettings().setJavaScriptEnabled(true);
        webv.loadUrl(websites[0]);

        seitenSwitcher = (SeekBar) findViewById(R.id.seitenSwitcher);
        seitenSwitcher.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 0 && progress < websites.length) {
                    webv.loadUrl(websites[progress]);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

}

Note, to get this to work you will require the following permissions in your manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I also modified the layout of xml slightly, and changed fill_parent to match_parent ( see here for why ):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/Viewing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/seitenSwitcher"/>
    <SeekBar
        android:id="@+id/seitenSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:max="15"
        android:progress="0"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        />
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM