我使用名为“tabbed activity”的模板启动了一个项目,其中包含三个选项卡,我添加了第四个选项卡,我想在每个选项卡中使用webView加载不同的URL。我是android studio中的新手,我想我必须使用webView
来做,但是看看google,教程,当然还有stackoverflow,我看到有人用tabActivity
代替ActionBarActivity
,尽管我有最后一个Android工作室版本,看起来像谷歌的模板使用已弃用的类我发现它真的很奇怪......我真的是新手与android工作室,但我正在尝试使用已弃用的类以模板的方式执行此操作。如果有人能够对我上面提到的那些方面有所了解,那就太好了。我的主要问题是为什么当我运行应用程序时,它会在每次加载相同的webView内容或如何为每个选项卡设置webView内容时加载。它总是在所有选项卡中加载最后一个选项卡URL。
这就是我在mainActivity.java中所做的:
package com.example.alfredo.guiaswebtabapp;
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
private WebView myWebView;
private WebView myWebView2;
private WebView myWebView3;
private WebView myWebView4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
setContentView(R.layout.fragment_main);
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
switch (i) {
case 0: {
// web view
this.myWebView = (WebView) findViewById(R.id.activity_main_webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://example.com/1");
}
case 1: {
this.myWebView2 = (WebView) findViewById(R.id.activity_main_webview);
myWebView2.setWebViewClient(new WebViewClient());
WebSettings webSettings2 = myWebView2.getSettings();
webSettings2.setJavaScriptEnabled(true);
myWebView2.loadUrl("http://example.com/2");
}
case 2:
this.myWebView3 = (WebView) findViewById(R.id.activity_main_webview);
myWebView3.setWebViewClient(new WebViewClient());
WebSettings webSettings3 = myWebView3.getSettings();
webSettings3.setJavaScriptEnabled(true);
myWebView3.loadUrl("http://example.com/3");
case 3:
this.myWebView4 = (WebView) findViewById(R.id.activity_main_webview);
myWebView4.setWebViewClient(new WebViewClient());
WebSettings webSettings4 = myWebView4.getSettings();
webSettings4.setJavaScriptEnabled(true);
myWebView4.loadUrl("http://example.com/4");
}
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this)
);
}
}
答案 0 :(得分:0)
我终于对我的代码进行了一些改进,基本上我在onCreate()
方法之后将四个webView加载到四个不同的webView中,并将属性设置为隐藏,除了第一个,当我切换时到另一个标签我将隐藏的属性设置为可见。
希望这段代码可以帮助别人并抱歉这个新手贴吧,干杯!
private WebView myWebView;
private WebView myWebView2;
private WebView myWebView3;
private WebView myWebView4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.myWebView = (WebView) findViewById(R.id.activity_main_webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://example.com/1");
this.myWebView2 = (WebView) findViewById(R.id.activity_main_webview2);
myWebView2.setWebViewClient(new WebViewClient());
WebSettings webSettings2 = myWebView2.getSettings();
webSettings2.setJavaScriptEnabled(true);
myWebView2.loadUrl("http://example.com/2");
myWebView2.setVisibility(View.INVISIBLE);
this.myWebView3 = (WebView) findViewById(R.id.activity_main_webview3);
myWebView3.setWebViewClient(new WebViewClient());
WebSettings webSettings3 = myWebView3.getSettings();
webSettings3.setJavaScriptEnabled(true);
myWebView3.loadUrl("http://example.com/3");
myWebView3.setVisibility(View.INVISIBLE);
this.myWebView4 = (WebView) findViewById(R.id.activity_main_webview4);
myWebView4.setWebViewClient(new WebViewClient());
WebSettings webSettings4 = myWebView4.getSettings();
webSettings4.setJavaScriptEnabled(true);
myWebView4.loadUrl("http://example.com/4");
myWebView4.setVisibility(View.INVISIBLE);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
switch (position) {
case 0: {
// web view
myWebView.setVisibility(View.VISIBLE);
myWebView2.setVisibility(View.INVISIBLE);
myWebView3.setVisibility(View.INVISIBLE);
myWebView4.setVisibility(View.INVISIBLE);
break;
//setContentView(R.layout.activity_main);
}
case 1: {
myWebView.setVisibility(View.INVISIBLE);
myWebView2.setVisibility(View.VISIBLE);
myWebView3.setVisibility(View.INVISIBLE);
myWebView4.setVisibility(View.INVISIBLE);
break;
}
case 2: {
myWebView.setVisibility(View.INVISIBLE);
myWebView2.setVisibility(View.INVISIBLE);
myWebView3.setVisibility(View.VISIBLE);
myWebView4.setVisibility(View.INVISIBLE);
break;
}
case 3: {
myWebView.setVisibility(View.INVISIBLE);
myWebView2.setVisibility(View.INVISIBLE);
myWebView3.setVisibility(View.INVISIBLE);
myWebView4.setVisibility(View.VISIBLE);
break;
}
}
}
});