- 自定义Banner继承RelativeLayout
public class Banner extends RelativeLayout implements ViewPager.OnPageChangeListener { RelativeLayout container; ViewPager pager; LinearLayout points; private int duration, currentIndex; private List<ImageView> indicators = new ArrayList<>(); public Banner(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view= inflate(context, R.layout.common_view_banner, this); container = view.findViewById(R.id.common_view_banner_container); pager= view.findViewById(R.id.common_view_banner_pager); points= view.findViewById(R.id.common_view_banner_points); //获取自定义的属性 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Banner); duration = array.getInt(R.styleable.Banner_banner_duration, 3000);//间隔 ViewGroup.LayoutParams layoutParams = container.getLayoutParams(); layoutParams.height = DimensUtil.dip2px(array.getInt(R.styleable.Banner_banner_height, 160));//高度 container.setLayoutParams(layoutParams); setClickable(true); array.recycle(); FixedSpeedScroller.setViewPagerDuration(pager, 100); } @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { currentIndex = i; for (ImageView indicator : indicators) indicator.setPressed(false); indicators.get(currentIndex % indicators.size()).setPressed(true); } @Override public void onPageScrollStateChanged(int i) { switch (i) { case ViewPager.SCROLL_STATE_IDLE: //完全空闲状态 start(); break; default: stop(); break; } } //用的Fastjson public void initData(JSONArray array) { if (array == null || array.size() == 0) { throw new IllegalArgumentException("传入参数不能为空"); } LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(10, 10, 10, 10); for (int i = 0; i < array.size(); i++) { ImageView indicator = new ImageView(ContextHolder.context); indicator.setImageResource(R.drawable.common_indicator_point); indicator.setLayoutParams(params); indicator.setPressed(i == 0); points.addView(indicator); indicators.add(indicator); } pager.setAdapter(new BannerPagerAdapter(array)); pager.setPageTransformer(true, new GalleryTransformer()); pager.addOnPageChangeListener(this); } public void start() { handler.sendEmptyMessageDelayed(0, duration); } public void stop() { handler.removeMessages(0); } private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { pager.setCurrentItem(currentIndex + 1, true); handler.sendEmptyMessageDelayed(0, duration); return false; } }); }
-
banner的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/common_view_banner_container" android:layout_width="match_parent" android:layout_height="@dimen/common_banner_height" android:background="@color/common_white"> <android.support.v4.view.ViewPager android:id="@+id/common_view_banner_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/common_view_banner_points" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:orientation="horizontal" android:padding="@dimen/common_padding_5" /> </RelativeLayout>
-
styleable文件
<declare-styleable name="Banner" tools:ignore="ResourceName"> <attr name="banner_duration" format="integer" /> <attr name="banner_height" format="integer" /> </declare-styleable>
-
ViewPager的差值修改
public class FixedSpeedScroller extends Scroller { private int duration = 500; public FixedSpeedScroller(Context context, Interpolator interpolator) { super(context, interpolator); } @Override public void startScroll(int startX, int startY, int dx, int dy, int duration) { super.startScroll(startX, startY, dx, dy, this.duration); } @Override public void startScroll(int startX, int startY, int dx, int dy) { super.startScroll(startX, startY, dx, dy, duration); } public void setDuration(int duration) { this.duration = duration; } public static void setViewPagerDuration(ViewPager pager, int duration) { try { Field field = ViewPager.class.getDeclaredField("mScroller"); field.setAccessible(true); FixedSpeedScroller scroller = new FixedSpeedScroller(pager.getContext(), new AccelerateInterpolator()); scroller.setDuration(300); field.set(pager, scroller); } catch (Exception e) { e.printStackTrace(); } } }