viewpager加webview

阎彬炳
2023-12-01

//viewpager加webview 

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.example.asus.application.MyViewPager;
import com.example.asus.application.R;
import com.example.asus.application.bean.Banear;
import com.example.asus.application.utils.Utils;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

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

public class threeActivity extends AppCompatActivity implements Utils.Netcallback {
    private List<ImageView> list=new ArrayList<>();
    private ViewPager pager;
    private WebView view;
    private Utils utils;
    private String path="https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/1";
private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 1:
                int currentItem = pager.getCurrentItem();
                currentItem++;
                pager.setCurrentItem(currentItem);
                sendEmptyMessageDelayed(1,2000);
                break;
        }
    }
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
        pager=findViewById(R.id.pager);
        view=findViewById(R.id.web);

        utils=Utils.getInstance();
        utils.getSeriviceData(path);
        utils.setNetcallback(this);
        webwy();
    }

    private void webwy() {
        String  url = getIntent().getStringExtra("url");
        view.setWebViewClient(new WebViewClient());
        view.setWebChromeClient(new WebChromeClient());
        view.loadUrl(url);
    }

    @Override
    public void onccess(String s) {
        Gson gson = new Gson();
        Banear banear = gson.fromJson(s, Banear.class);
        for(Banear.BodyBean.ResultBean resultBean:banear.getBody().getResult()){
            ImageView imageView = new ImageView(threeActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageLoader.getInstance().displayImage(resultBean.getBarnerPic(),imageView);
            list.add(imageView);
        }
        pager.setAdapter(new MyViewPager(list));
        handler.sendEmptyMessageDelayed(1,2000);
    }
}

 

//Utils

import android.os.AsyncTask;

import com.google.common.io.CharStreams;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Utils {
    private static final Utils ourInstance = new Utils();

    public static Utils getInstance() {
        return ourInstance;
    }

    private Utils() {
    }
    public  interface Netcallback{

        void onccess(String s);
    }

    private  Netcallback netcallback;

    public void setNetcallback(Netcallback netcallback){

        this.netcallback=netcallback;
    }

    public void getSeriviceData(String url){

        new LotaData().execute(url);
    }

    class LotaData extends AsyncTask<String,Void,String> {

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(5000);
                connection.setReadTimeout(5000);
                connection.setRequestMethod("GET");
                if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){

                    return CharStreams.toString(new InputStreamReader(connection.getInputStream(),"utf-8"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(netcallback!=null){
                netcallback.onccess(s);
            }
        }
    }

}
 类似资料: