欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

轮播图

程序员文章站 2023-12-30 15:55:40
...

主布局

<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=".MainActivity">

    <cn.bingoogolapple.bgabanner.BGABanner
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        />

</LinearLayout>

主页面

public class MainActivity extends AppCompatActivity {
    //定义两个数组
    private String[] url = {
            "https://img.huxiucdn.com/article/cover/201804/11/142701198959.jpg?imageView2/1/w/710/h/400/|imageMogr2/strip/interlace/1/quality/85/format/jpg",
            "https://img.huxiucdn.com/article/cover/201809/10/195039242422.jpg?imageView2/1/w/710/h/400/|imageMogr2/strip/interlace/1/quality/85/format/jpg",
            "https://img.huxiucdn.com/article/cover/201809/10/143541089263.jpg?imageView2/1/w/710/h/400/|imageMogr2/strip/interlace/1/quality/85/format/jpg"
    };
    private String[] title = {
            "资讯拼多多?趣头条不值得高估",
            "马云接班人为什么是张勇",
            "《天天德州》终下架:面对**灰产,腾讯也无能为力"
    };
    private BGABanner banner;
    private List<String> urllist;
    private List<String> titlelist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        banner = findViewById(R.id.banner);
        urllist = new ArrayList<>();
        titlelist = new ArrayList<>();
        //将数组放入集合中
        for(int i=0;i<url.length;i++){
            urllist.add(url[i]);
            titlelist.add(title[i]);
        }
        //给banner设置数据
        banner.setData(urllist,titlelist);
        BGABanner.Adapter<ImageView, String> adapter = new BGABanner.Adapter<ImageView, String>() {

            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Picasso.get().load(urllist.get(position)).into(itemView);
            }
        };
        banner.setAdapter(adapter);
    }
}

上一篇:

下一篇: