1. 程式人生 > >輪播 網頁 結合使用.......

輪播 網頁 結合使用.......

<佈局>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.DataActivity"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="200dp"></android.support.v4.view.ViewPager>

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>

</LinearLayout>

<主介面>

public class DataActivity extends BaseActivity implements NetCallback {
    //介面
    private String BannerApi = "https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/1";
    private String url;
    private ViewPager banner;
    private WebView webView;
    private List<ImageView> bannerImages = new ArrayList<>();

    @Override
    protected void findView() {
        //初始化控制元件
        url = getIntent().getStringExtra("url");
        banner = findViewById(R.id.banner);
        webView = findViewById(R.id.web_view);

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initFinish() {
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl(url);

        //設定banner
        loadBannerData();
    }

    //載入輪播資料
    private void loadBannerData() {
        utils.getDataFromService(BannerApi,this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_data;
    }

    @Override
    public void onSuccess(String result) {
        //解析資料
        Gson gson = new Gson();
        BannerBean bannerBean = gson.fromJson(result,BannerBean.class);

        //遍歷資料  載入圖片
        for (BannerBean.BodyBean.ResultBean resultBean: bannerBean.getBody().getResult()){
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageLoader.getInstance().displayImage(resultBean.getBarnerPic(),imageView);
            bannerImages.add(imageView);
        }

        //設定介面卡
        banner.setAdapter(new BannerAdapter(bannerImages));

        //輪播
        handler.sendEmptyMessageDelayed(1,2000);

    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    int currentItem = banner.getCurrentItem();
                    currentItem++;
                    banner.setCurrentItem(currentItem);
                    sendEmptyMessageDelayed(1,2000);
                    break;
            }
        }
    };
    @Override
    public void onError(String errorMsg) {

    }
}