Android轮播图api,GitHub - xingda920813/LunboViewPager: Android 轮播 ViewPager: 实现 Banner 轮播图效果. 页码指示, 左右无...

本文介绍了一款名为LunboViewPager的库,它扩展了ViewPager并集成了圆环指示器。支持自动切换、无限循环和用户触控暂停。适配于Activity/Fragment、RecyclerView及自定义页签,提供了设置圆点颜色、间距等高级功能。通过简单的配置和自定义适配器实现流畅轮播效果。

LunboViewPager

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d3939362e6963752d7265642e737667

An enhancement to ViewPager and ViewPagerIndicator, implements banner effert.

Your banner can be used in Activity / Fragment, or an item in RecyclerView.

Page indicator, infinite loop pages, automatical switch to next page, pause auto-switch on user touch.

Provides CirclePageIndicator (round point) indicator.

ff5f5b71c7cfac4a492583300dd89699.png

Import

1.Add binary

In your build.gradle, add

compile 'com.xdandroid:lunboviewpager:+'

2.Layout XML (Activity/Fragment/Item in RecyclerView)

Place the ViewPager in parallel with the CirclePageIndicator element.

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/white">

android:id="@+id/vp_lunbo"

android:layout_width="352dp"

android:layout_height="176dp"/>

android:id="@+id/indicator_lunbo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|right"

android:layout_marginBottom="6dp"

android:layout_marginRight="2dp"/>

4. Layout XML (Each page in ViewPager, usually composed of an ImageView / DraweeView and some other widgets)

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/iv_lunbo"

android:scaleType="centerCrop"

android:layout_height="176dp"

android:layout_width="350dp"/>

For usage, please refer to the sample.

For use in Activity / Fragment

1.Set necessary custom attributes to ViewPager and indicator(OnPageChangeListener, Fill color of indicator, etc.)

For custom attributes available in indicator, you can refer to the API provided in JakeWharton/ViewPagerIndicator.

Supports wrap_content while JakeWharton's version not;

Adds 2 APIs to set and get the ratio of distance between circles to circle radius:

void CirclePageIndicator.setDistanceBetweenCircles(double timesToRadius_multiplier);

double CirclePageIndicator.getDistanceBetweenCirclesMultiplier()

//Set OnPageChangeListener

vp_lunbo.setOnPageChangeListener(onPageChangeListener);

//Set fill color for the circle currently chosen

indicator_lunbo.setFillColor(getResources().getColor(R.color.colorAccent));

//Set circle radius

indicator_lunbo.setRadius(UIUtils.dp2px(this, 3.25f));

//set the ratio of distance between circles to circle radius

indicator_lunbo.setDistanceBetweenCircles(3.0);

2.Instantiate Adapter

public Adapter(Context context);

You need to implement 4 methods in Adapter:

protected abstract int getViewPagerItemLayoutResId();//Set layout resource id for one page in ViewPager

protected abstract View showImage(View inflatedLayout, int position);

inflatedLayout: inflated View object from the resource id provided before.

Developers should use findViewById to get the ImageView / DraweeView object, then load image into widget using UIL, Picasso, Fresco, etc..., finally return the ImageView / DraweeView.

protected abstract int getImageCount();//Page count

protected abstract void onImageClick(View view, int position);//OnClickListener

Example:

new Adapter(MainActivity.this) {

protected View showImage(View inflatedLayout, int position) {

ImageView imageView = (ImageView) inflatedLayout.findViewById(R.id.iv_lunbo);

Picasso.with(MainActivity.this).load(list.get(position).getImageResId()).into(imageView);

return imageView;

}

protected int getViewPagerItemLayoutResId() {return R.layout.item_in_viewpager;}

protected int getImageCount() {return list.size();}

protected void onImageClick(View view, int position) {

Snackbar.make(view,list.get(position).getMessage(),Snackbar.LENGTH_SHORT).show();

}

}

3.Instantiate Proxy

public Proxy(List list, int interval, Adapter adapter);

interval: The time interval for automatically switch to next page.

4.Start infinite looping

void Proxy.onBindView(ViewPager viewPager,View indicator);

For use in an item of RecyclerView

1.Instantiate com.xdandroid.lunboviewpager.Adapter in the constructor of RecyclerView.Adapter

public com.xdandroid.lunboviewpager.Adapter(Context context);

You need to implement 4 methods in com.xdandroid.lunboviewpager.Adapter:

protected abstract int getViewPagerItemLayoutResId();//Set layout resource id for one page in ViewPager

protected abstract View showImage(View inflatedLayout, int position);

inflatedLayout: inflated View object from the resource id provided before.

Developers should use findViewById to get the ImageView / DraweeView object, then load image into widget using UIL, Picasso, Fresco, etc..., finally return the ImageView / DraweeView.

protected abstract int getImageCount();//Page count

protected abstract void onImageClick(View view, int position);//OnClickListener

Example:

new com.xdandroid.lunboviewpager.Adapter(context) {

protected View showImage(View inflatedLayout, int position) {

ImageView imageView = (ImageView) inflatedLayout.findViewById(R.id.iv_lunbo);

Picasso.with(context).load(list.get(position).getImageResId()).into(imageView);

return imageView;

}

protected int getViewPagerItemLayoutResId() {return R.layout.item_in_viewpager;}

protected int getImageCount() {return list.size();}

protected void onImageClick(View view, int position) {

Snackbar.make(view,list.get(position).getMessage(),Snackbar.LENGTH_SHORT).show();

}

}

2.Instantiate Proxy in the constructor of RecyclerView.Adapter

public Proxy(List list, int interval, com.xdandroid.lunboviewpager.Adapter adapter);

interval: The time interval for automatically switch to next page.

3.onBindViewHolder

Set necessary custom attributes to ViewPager and indicator(OnPageChangeListener, Fill color of indicator, etc.)

For custom attributes available in indicator, you can refer to the API provided in JakeWharton/ViewPagerIndicator.

Supports wrap_content while JakeWharton's version not;

Adds 2 APIs to set and get the ratio of distance between circles to circle radius:

void CirclePageIndicator.setDistanceBetweenCircles(double timesToRadius_multiplier);

double CirclePageIndicator.getDistanceBetweenCirclesMultiplier()

Finally, Add proxy.onBindView(holder.viewPager,holder.indicator);

Example:

@Override

public void onBindViewHolder(final MainAdapter.ViewHolder holder, int position) {

//Set fill color for the circle currently chosen

holder.indicator_lunbo.setFillColor(context.getResources().getColor(R.color.colorAccent));

//Set circle radius

holder.indicator_lunbo.setRadius(UIUtils.dp2px(holder.indicator_lunbo.getContext(), 3.25f));

//set the ratio of distance between circles to circle radius

holder.indicator_lunbo.setDistanceBetweenCircles(3.0);

proxy.onBindView(holder.vp_lunbo,holder.indicator_lunbo);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值