A hint ViewPager can be used to display the Banner on the home page of the app.





This can be done in 2-3 lines of code by passing in a list of the ImageView you want to display, or by adding your own Transform animations to it.

        bannerView = (BannerView) findViewById(R.id.banner);
        bannerView.setViewList(viewList);
        bannerView.startLoop(true);Copy the code

The implementation principle I believe you should know, is probably too lazy to encapsulate this kind of thing.

Specific ideas:

    1. Inherit from FrameLayout and populate the bottom layer with a ViewPager
  • 2. Dynamically generate the bottom hint dot according to the ViewPager itemCount

if (mLinearPosition.getChildCount() ! = viewSize) { intdiffCnt = mLinearPosition.getChildCount() - viewSize;
                boolean needAdd = diffCnt < 0;
                diffCnt = Math.abs(diffCnt);
                for (int i = 0; i < diffCnt; i++) {
                    if (needAdd) {
                        ImageView img = new ImageView(getContext());
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        layoutParams.rightMargin = getResources().getDimensionPixelOffset(R.dimen.dimen_9dp);
                        img.setLayoutParams(layoutParams);
                        img.setBackgroundResource(R.drawable.banner_point);
                        mLinearPosition.addView(img);
                    } else {
                        mLinearPosition.removeViewAt(0);}}}Copy the code
  • 3. Use Handler to control the rotation frequency, reset the currentItem of the ViewPager and the background of the hint dot
private static class BannerHandler extends Handler {
        private WeakReference<BannerView> weakReference = null;

        public BannerHandler(BannerView bannerView) {
            super(Looper.getMainLooper());
            this.weakReference = new WeakReference<BannerView>(bannerView);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (this.weakReference == null) {
                return;
            }
            BannerView bannerView = this.weakReference.get();
            if (bannerView == null || bannerView.mViewPager == null || bannerView.mViewPager.getAdapter() == null || bannerView.mViewPager.getAdapter().getCount() <= 0) {
                sendEmptyMessageDelayed(MSG_LOOP.LOOP_INTERVAL);
                return;
            }
            int curPos = bannerView.mViewPager.getCurrentItem();
            curPos = (curPos + 1) % bannerView.mViewPager.getAdapter().getCount();
            bannerView.mViewPager.setCurrentItem(curPos);
            sendEmptyMessageDelayed(MSG_LOOP.LOOP_INTERVAL); }}Copy the code
  • 4. Details, such as the creation of infinite multicast Adapter, weak references in handler, start destroy loop, etc

Specific source code and demo address