qq_22615877
神奇的易大师
2015-04-19 13:50

Android如何在viewpager中添加WebView?

  • webview
  • viewpager

我写了一个viewpager,共有5个界面。想在viewpager界面上显示webview,写完之后代码没有提示错误,但一直运行不了,求解决,贴上源代码。
package com.example.young;

import java.util.ArrayList;
import java.util.List;

import com.example.young.utils.WebView1;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class TabyixueActivity extends Activity{
//存放页面的容器
List listViews;
//上下文
Context context;
@SuppressWarnings("deprecation")
LocalActivityManager manager = null;
//实现翻页的组件
private ViewPager pager = null;
//生成webview数组
private WebView webView[];

//private List<View> webView;

private void InitWebView() { 
    //加载webview


    webView[0]=(WebView1)findViewById(R.id.webView21);
    //webView[1]=(WebView1)findViewById(R.id.webView22);
    //webView[2]=(WebView1)findViewById(R.id.webView23);
    //webView[3]=(WebView1)findViewById(R.id.webView24);
    //webView[4]=(WebView1)findViewById(R.id.webView25);
    //加载网页
    webView[0].loadUrl("http://www.baidu.com");
    //webView[1].loadUrl("http://www.sohu.com");
    //webView[2].loadUrl("http://www.sina.com");
    //webView[3].loadUrl("http://www.qq.com");
    //webView[4].loadUrl("http://www.360.com");

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_yixue);
    LayoutInflater inflater = LayoutInflater.from(this);
    manager = new LocalActivityManager(this, true);
    manager.dispatchCreate(savedInstanceState);
    context = TabyixueActivity.this;

    // 呈现页面滑动效果

    pager = (ViewPager) findViewById(R.id.yixuetext);
    listViews = new ArrayList<View>();

    View view1 = inflater.inflate(R.layout.yixue_1, null);
    View view2 = inflater.inflate(R.layout.yixue_2, null);
    View view3 = inflater.inflate(R.layout.yixue_3, null);
    View view4 = inflater.inflate(R.layout.yixue_4, null);
    View view5 = inflater.inflate(R.layout.yixue_5, null);

    listViews.add(view1);
    listViews.add(view2);
    listViews.add(view3);
    listViews.add(view4);
    listViews.add(view5);

    //滑页适配器
    pager.setAdapter(pagerAdapter);
    pager.setOnPageChangeListener(new OnPageChangeListener(){
        @Override
        public void onPageSelected(int position){
            //stetDots();
        }
        @Override
        public void onPageScrolled(int arg0,float arg1,int arg2){}
        @Override
        public void onPageScrollStateChanged(int arg0){}
    });
     InitWebView();



}
private void addView(List<View> view,String url){
    WebView webView =new WebView(this);
    webView.loadUrl(url);
    view.add(webView);
}

private PagerAdapter pagerAdapter=new PagerAdapter(){
    @Override
    //这里获得当前页卡
    public Object instantiateItem(ViewGroup view,int position){
        view.addView(listViews.get(position));
        return listViews.get(position);
    }



    @Override
    //得到所有页卡的个数
    public int getCount() {

        return listViews.size();
    }

    @Override
    //判断当前显示页卡是否匹配
    public boolean isViewFromObject(View arg0, Object arg1) {

        return arg0==arg1;
    }

    @Override
    //销毁页卡
    public void destroyItem(ViewGroup view,int position,Object arg2){
        ViewPager pViewPager=((ViewPager)view);
        pViewPager.removeView(listViews.get(position));
    }

};

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐