1. 程式人生 > >FlyBanner無限輪播圖

FlyBanner無限輪播圖

新增依賴

dependencies {
	
compile 'com.recker.flybanner:flybanner:1.3'

} 

MainActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.recker.flybanner.FlyBanner;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private FlyBanner mBannerLocal;//載入本地圖片
    private FlyBanner mBannerNet;//載入網路圖片
    private String[] mImagesUrl = {
            "http://img4.imgtn.bdimg.com/it/u=2430963138,1300578556&fm=23&gp=0.jpg",
            "http://img1.imgtn.bdimg.com/it/u=2755648979,3568014048&fm=23&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=2272739960,4287902102&fm=23&gp=0.jpg",
            "http://img3.imgtn.bdimg.com/it/u=1078051055,1310741362&fm=23&gp=0.jpg"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initLocalBanner();
        initNetBanner();
    }

    //載入本地圖片
    private void initLocalBanner() {
        mBannerLocal = findViewById(R.id.banner_1);
        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.img_nature1);
        images.add(R.drawable.img_nature2);
        images.add(R.drawable.img_nature3);
        images.add(R.drawable.img_nature4);
        images.add(R.drawable.img_nature5);
        mBannerLocal.setImages(images);
        //mBannerLocal.setPoinstPosition(FlyBanner.RIGHT);
        mBannerLocal.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                toast("點選了第" + position + "張圖片");
            }
        });

    }

    /**
     * 載入網頁圖片
     */
    private void initNetBanner() {
        mBannerNet = (FlyBanner) findViewById(R.id.banner_2);


        List<String> imgesUrl = new ArrayList<>();
        for (int i = 0; i < mImagesUrl.length; i++) {
            imgesUrl.add(mImagesUrl[i]);
        }
        mBannerNet.setImagesUrl(imgesUrl);


        mBannerNet.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                toast("點選了第" + position + "張圖片");
            }
        });
    }
    private void toast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}

佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.recker.flybanner.FlyBanner
        android:id="@+id/banner_1"
        android:layout_width="match_parent"
        android:layout_height="180dp"></com.recker.flybanner.FlyBanner>

    <com.recker.flybanner.FlyBanner
        android:id="@+id/banner_2"
        android:layout_width="match_parent"
        android:layout_height="180dp" />
</LinearLayout>