Banner轮播图----从接口获取图片

activity_main


MainActivity

package wanghuiqi.bawie.com.whq_yuekao;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.List;

public class ThreeActivity extends AppCompatActivity {

    private String imageUrl="http://www.xieast.com/api/banner.php";
    private Banner banner;

    @SuppressLint("StaticFieldLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);

//获取控件
        banner = findViewById(R.id.banner);

   //获取请求网络
        new AsyncTask(){
            @Override
            protected String doInBackground(String... strings) {
                return HttpUtils.getFromString(strings[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                BannerBean bannerBean = gson.fromJson(s, BannerBean.class);
                List data = bannerBean.getData();
                banner.setImageLoader(new GlideImageLoader());
                banner.setImages(data);
                banner.start();
            }
        }.execute(imageUrl);
    }


    private class GlideImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(((BannerBean.DataBean)path).getImg())
                    .into(imageView);
        }
    }
}

依赖

implementation 'com.youth.banner:banner:1.4.10'
implementation 'com.github.bumptech.glide:glide:3.8.0'

你可能感兴趣的:(Banner轮播图----从接口获取图片)