问题描述
我需要做到的是,在每一个选项卡我想显示不同的网站,像
标签1:http://www.stackoverflow.com
表2:HTTP://www.google..com
标签3. http://www.developers.android.com
我的问题是我无法访问web视图对象为使用loadURL()
要澄清我的问题,这是我的code:
FragmentTabsPagerActivity.java
公共类FragmentTabsPagerActivity扩展FragmentActivity实现TabHost.OnTabChangeListener,ViewPager.OnPageChangeListener {
私人TabHost mTabHost;
私人ViewPager mViewPager;
私人的HashMap<字符串,TabInfo> mapTabInfo =新的HashMap<字符串,FragmentTabsPagerActivity.TabInfo>();
私人PagerAdapter mPagerAdapter;
...
私人无效initialiseTabHost(捆绑的args){
mTabHost =(TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = NULL;
捆绑bundle1 =新包();
捆绑bundle2中=新包();
捆绑软件包3 =新包();
bundle1.putInt(身份证,1);
bundle2.putInt(ID,2);
bundle3.putInt(ID,3);
FragmentTabsPagerActivity.addTab(此,this.mTabHost,this.mTabHost.newTabSpec(TAB1)setIndicator(标签1),(tabInfo =新TabInfo(TAB1,TabsFragment.class,bundle1))。);
FragmentTabsPagerActivity.addTab(此,this.mTabHost,this.mTabHost.newTabSpec(TAB2)setIndicator(标签2),(tabInfo =新TabInfo(TAB2,TabsFragment.class,bundle2中))。);
FragmentTabsPagerActivity.addTab(这一点,this.mTabHost,this.mTabHost.newTabSpec(TAB3)setIndicator(标签3),(tabInfo =新TabInfo(TAB3,TabsFragment.class,软件包3)));
this.mapTabInfo.put(tabInfo.tag,tabInfo);
mTabHost.setOnTabChangedListener(本);
}
}
TabsFragment.java
公共类TabsFragment扩展片段{
的LinearLayout布局;
的WebView mWebView;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
布局=(的LinearLayout)inflater.inflate(R.layout.fragments_layout,集装箱,假);
mWebView =(web视图)layout.findViewById(R.id.webView1);
WebSettings设定值= mWebView.getSettings();
mWebView.setWebViewClient(新WebViewClient(){
公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
返回true;
}
});
如果(集装箱== NULL){
返回null;
}
返回布局;
}
公共无效setWebUrl(字符串URL){
mWebView.loadUrl(URL);
}
}
PagerAdapter.java
公共类PagerAdapter扩展FragmentPagerAdapter {
私人列表<片断>片段;
公共PagerAdapter(FragmentManager FM,名单,其中,片断>片段){
超(FM);
this.fragments =片段;
}
@覆盖
公共片段的getItem(INT位置){
返回this.fragments.get(位置);
}
@覆盖
公众诠释getCount将(){
返回this.fragments.size();
}
}
编辑:
fragments_layout.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /布局1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
>
<的WebView
机器人:ID =@ + ID / webView1
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< / LinearLayout中>
我解决了这个问题。我用下面的code做了我的标签动态。
在我的 TabsFragment.java 我添加的 ID 并获得设定的 ID 按 ID = getArguments()。调用getInt(ID);
公共类TabsFragment扩展片段{
的LinearLayout布局;
的WebView mWebView;
INT ID;
公共静态TabsFragment的newInstance(INT ID){
TabsFragment选项卡=新TabsFragment();
捆绑的args =新包();
args.putInt(身份证,身份证);
tab.setArguments(参数);
返回标签;
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
布局=(的LinearLayout)inflater.inflate(R.layout.fragments_layout,集装箱,假);
mWebView =(web视图)layout.findViewById(R.id.webView1);
WebSettings设定值= mWebView.getSettings();
mWebView.setWebViewClient(新WebViewClient(){
公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
返回true;
}
});
如果(集装箱== NULL){
返回null;
}
返回布局;
}
@覆盖
公共无效onActivityCreated(包savedInstanceState){
// TODO自动生成方法存根
super.onActivityCreated(savedInstanceState);
如果(savedInstanceState!= NULL){
ID = savedInstanceState.getInt(ID);
}其他{
。ID = getArguments()调用getInt(ID);
}
Log.w(ID,ID +);
开关(ID){
情况1:
setWebUrl(http://www.stackoverflow.com);
打破;
案例3:
setWebUrl(HTTP://www.google..com);
打破;
案例2:
setWebUrl(http://www.developers.android.com);
打破;
默认:
打破;
}
}
公共无效setWebUrl(字符串URL){
mWebView.loadUrl(URL);
}
}
而在 PagerAdapter.java *的方法的getItem(INT位置)的*我创建的 TabsFragment 类的新实例,并返回新的实例
@覆盖
公共片段的getItem(INT位置){
TabsFragment标签= TabsFragment.newInstance(位置+ 1);
//返回this.fragments.get(位置);
返回标签;
}
它完美。
What I need to achieve is that in every Tab I want to display different websites, like
Tab 1: http://www.stackoverflow.com
Tab 2: http://www.google..com
Tab 3. http://www.developers.android.com
My problem is I can't access the WebView object to a loadUrl()
To clarify my question here's my code:
FragmentTabsPagerActivity.java
public class FragmentTabsPagerActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, FragmentTabsPagerActivity.TabInfo>();
private PagerAdapter mPagerAdapter;
...
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
Bundle bundle1 = new Bundle();
Bundle bundle2 = new Bundle();
Bundle bundle3 = new Bundle();
bundle1.putInt("id", 1);
bundle2.putInt("id", 2);
bundle3.putInt("id", 3);
FragmentTabsPagerActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), (tabInfo = new TabInfo("Tab1", TabsFragment.class, bundle1)));
FragmentTabsPagerActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), (tabInfo = new TabInfo("Tab2", TabsFragment.class, bundle2)));
FragmentTabsPagerActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), (tabInfo = new TabInfo("Tab3", TabsFragment.class, bundle3)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.setOnTabChangedListener(this);
}
}
TabsFragment.java
public class TabsFragment extends Fragment{
LinearLayout layout;
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layout = (LinearLayout)inflater.inflate(R.layout.fragments_layout, container, false);
mWebView = (WebView) layout.findViewById(R.id.webView1);
WebSettings setting =mWebView.getSettings();
mWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
if(container == null){
return null;
}
return layout;
}
public void setWebUrl(String url){
mWebView.loadUrl(url);
}
}
PagerAdapter.java
public class PagerAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
EDIT:
fragments_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I solved this problem. I made the my Tab dynamic using the code below.
In my TabsFragment.java I add an id and get set id by id = getArguments().getInt("id");
public class TabsFragment extends Fragment{
LinearLayout layout;
WebView mWebView;
int id;
public static TabsFragment newInstance(int id){
TabsFragment tab = new TabsFragment();
Bundle args = new Bundle();
args.putInt("id", id);
tab.setArguments(args);
return tab;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
layout = (LinearLayout)inflater.inflate(R.layout.fragments_layout, container, false);
mWebView = (WebView) layout.findViewById(R.id.webView1);
WebSettings setting =mWebView.getSettings();
mWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
if(container == null){
return null;
}
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null){
id = savedInstanceState.getInt("id");
}else{
id = getArguments().getInt("id");
}
Log.w("id", id+"");
switch (id) {
case 1:
setWebUrl("http://www.stackoverflow.com");
break;
case 3:
setWebUrl("http://www.google..com");
break;
case 2:
setWebUrl("http://www.developers.android.com");
break;
default:
break;
}
}
public void setWebUrl(String url){
mWebView.loadUrl(url);
}
}
And in PagerAdapter.java *method getItem(int position)* I create a new instance of TabsFragment class and return the new instance.
@Override
public Fragment getItem(int position) {
TabsFragment tab = TabsFragment.newInstance(position+1);
//return this.fragments.get(position);
return tab;
}
It works perfectly.
这篇关于设置的WebView使用loadURL编程是在片段类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!