liangbx361/banner


一个相对简洁的 Android Banner控件库

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.liangbx361:banner:0.1'
	}
	dependencies {
		implementation("com.github.liangbx361:banner:0.1")
	}
	<dependency>
	    <groupId>com.github.liangbx361</groupId>
	    <artifactId>banner</artifactId>
	    <version>0.1</version>
	</dependency>

                            
    libraryDependencies += "com.github.liangbx361" % "banner" % "0.1"
        
        

                            
    :dependencies [[com.github.liangbx361/banner "0.1"]]
        
        

Readme


Banner 控件

通用的Banner控件,通过封装ViewPager实现。

演示

特性

  • 支持循环模式
  • 支持自动播放(注:在手动滑动时会停止自动播放)
  • 支持自定义指示器
  • 支持自定义图片加载器
  • 支持脏数据校验(注:只有在脏数据的情况下才进行数据刷新)

待支持特性

  • 在不可见的情况下停止自动播放

设计

领域图

依赖

 compile 'com.github.liangbx361:banner:0.1'

用法

添加BannerView到布局文件

...
<com.liangbx.android.banner.BannerView
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>
...

在代码中设置

mBannerView = (BannerView) findViewById(R.id.banner);

//设置数据
List<BannerItem> bannerItems = new ArrayList<>();
bannerItems.add(new BannerItem("http://img1.3lian.com/img13/c3/26/d/81.jpg", "bird"));
mBannerView.setData(bannerItems);

//设置点击监听事件
mBannerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(SampleActivity.this, "click position --> " + position, Toast.LENGTH_SHORT).show();
            }
        });

BannerView 属性设置

| 名称 | 默认值 | 说明 | | ---------------------- | -------- | ---------- | | setAutoPlayMode | 默认自动播放模式 | 设置自动播放模式 | | setIntervalTime | 默认间隔3秒 | 设置自动播放间隔时间 | | setCycleMode | 默认循环模式 | 设置循环模式 | | setOnItemClickListener | 默认无 | 设置点击监听事件 | | setIndicator | 默认无 | 设置指示器 | | setImageLoader | 默认无 | 设置图片加载器 |

Indicator

考虑到每个项目的Indicator样式和动画都不相同,所以需要外部实现Indicator接口。 这里推荐一个开源项目PageIndicatorView。 示例代码如下:

public class CycleIndicator implements Indicator {

    private int mSize;
    private PageIndicatorView mIndicatorView;

    public CycleIndicator(int size) {
        mSize = size;
    }

    @Override
    public View getLayout(Context context, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.banner_indicator, parent, false);
        mIndicatorView = (PageIndicatorView) view.findViewById(R.id.indicator);
        mIndicatorView.setCount(mSize);
        return view;
    }

    @Override
    public int getGravity() {
        return Gravity.BOTTOM | Gravity.CENTER;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels, BannerItem bannerItem) {
        mIndicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    @Override
    public void onPageSelected(int position, BannerItem bannerItem) {
        mIndicatorView.onPageSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        mIndicatorView.onPageScrollStateChanged(state);
    }
}

ImageLoader

考虑到每个项目可能都有自己的图片加载库,第三方开源的库也有很多(Glide、Fresco等)。如果默认提供一个图片加载库会造成可能不是项目用的,无故增加包大小,所以不提供默认实现。交由外部实现ImageLoader接口。 这里使用Glide实现的示例:

public class GlideImageLoader implements ImageLoader {

    @Override
    public void onLoad(ImageView imageView, String imageUrl) {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .placeholder(R.mipmap.defautl_image)
                .error(R.mipmap.defautl_image)
                .centerCrop()
                .crossFade()
                .into(imageView);
    }

    @Override
    public void onUnLoad(ImageView imageView) {

    }
}

参考