SoFunction
Updated on 2025-03-10

How to make android carousel diagram components

This article shares the production method of Android carousel diagram components for your reference. The specific content is as follows

BannerLayout

package com.coral3.common_module.components;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import com.coral3.common_module.R;
import com.coral3.common_module.;
import com.coral3.common_module.;
import ;
import ;
import ;

public class BannerLayout extends LinearLayout {

    private Context mContext;
    private View view;
    private ChildViewPager viewPager;
    private ImageView indicator;
    private ImageView[] indicators;
    private Boolean isContinue = true;
    private ViewGroup group;
    private AtomicInteger index = new AtomicInteger();
    private Handler handler = new Handler(new (){

        @Override
        public boolean handleMessage(Message message) {
            ();
            return false;
        }
    });

    public BannerLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
        initListener();
    }

    private void initView(){
        view = (mContext).inflate(.layout_banner, this);
        group = (.view_indicators);
        viewPager = (.view_banners);
        // Add pictures dynamically        List<View> listPics = new ArrayList<>();
        ImageView img1 = new ImageView(mContext);
        (.banner1);
        (img1);
        ImageView img2 = new ImageView(mContext);
        (.banner2);
        (img2);
        ImageView img3 = new ImageView(mContext);
        (.banner3);
        (img3);
        ImageView img4 = new ImageView(mContext);
        (.banner4);
        (img4);
        ImageView img5 = new ImageView(mContext);
        (.banner4);
        (0, img5);
        ImageView img0 = new ImageView(mContext);
        (.banner1);
        (img0);
        // Dynamically add indicator        indicators = new ImageView[()];
        for(int i = 0; i < ; i++){
            indicator = new ImageView(mContext);
             layoutParams = new (15, 15);
            (0, 0, 10, 0);
            (layoutParams);
            indicators[i] = indicator;
            if(i == 1){
                indicators[i].setBackgroundResource(.shape_banner_checked);
            }else{
                indicators[i].setBackgroundResource(.shape_banner_unchecked);
            }
            if(i == 0 || i == 5){
                indicators[i].setVisibility();
            }
            (indicators[i]);
        }
        (new MyPagerAdapter(listPics));
        ();
        // Carousel        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true){
                    if(isContinue){
                        (());
                        whatOption();
                    }
                }
            }
        }).start();
    }

    private void initListener(){
        // Set up the listener        (new () {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                (positionOffset + "-" + positionOffsetPixels);
                //Seamless scrolling and smooth//                if(positionOffset == 0.0){
//                    (position + "");
//                    if(position == 5) {
//                        (1, false);
//                    }
//                    if(position == 0) {
//                        (4, false);
//                    }
//                }
            }

            @Override
            public void onPageSelected(int position) {
                (position);
                if(position == 5) {
                    (1, false);
                }
                if(position == 0) {
                    (4, false);
                }
                for(int i = 0; i < ; i++){
                    if(i == ()){
                        indicators[i].setBackgroundResource(.shape_banner_checked);
                    }else{
                        indicators[i].setBackgroundResource(.shape_banner_unchecked);
                    }
                }
                if(position == 0) indicators[4].setBackgroundResource(.shape_banner_checked);
                if(position == 5) indicators[1].setBackgroundResource(.shape_banner_checked);
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
        // Set the stop timing when touched        (new () {

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (()){
                    case MotionEvent.ACTION_DOWN:
                        isContinue = false;
                        break;
                    case MotionEvent.ACTION_UP:
                        isContinue = true;
                        break;
                }
                return false;
            }
        });
    }

    class MyPagerAdapter extends PagerAdapter {

        private List<View> listView;

        @Override
        public int getCount() {
            return ();
        }

        public MyPagerAdapter(List<View> listView){
             = listView;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ((position));
            return (position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            ((position));
        }
    }

    private void whatOption(){
        ();
        if(() >  - 2){
            (-4);
        }
        try {
            (3000);
        } catch (InterruptedException e) {
            ();
        }
    }
}

layout_banner

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:andro
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <com.coral3.common_module.
            android:
            android:layout_width="match_parent"
            android:layout_height="200dp"/>
        <LinearLayout
            android:
            android:layout_below="@+id/view_banners"
            android:gravity="center"
            android:layout_marginTop="-15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />
    </RelativeLayout>
</LinearLayout>

ChildViewPager

package com.coral3.common_module.viewPager;

import ;
import ;
import ;
import ;
import ;

public class ChildViewPager extends ViewPager {
    /** The point pressed when touched **/
    PointF downP = new PointF();
    /** The current point when touched **/
    PointF curP = new PointF();
    public ChildViewPager(Context context) {
        super(context);
    }

    public ChildViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private static final String TAG = "ChildViewpager";
    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        //Every time the onTouch event is performed, the current pressed coordinates are recorded.        if(getChildCount()&lt;=1)
        {
            return (arg0);
        }
         = ();
         = ();

        if(() == MotionEvent.ACTION_DOWN)
        {

            //Record coordinates when pressed            //Remember that downP = curP is not available, so when curP is changed, downP will also change.             = ();
             = ();
            //This sentence is to inform his parent ViewPager that the operation of this control is now not to interfere with my operation.            getParent().requestDisallowInterceptTouchEvent(true);
        }

        if(() == MotionEvent.ACTION_MOVE){
            //This sentence is to inform his parent ViewPager that the operation of this control is now not to interfere with my operation.            getParent().requestDisallowInterceptTouchEvent(true);
        }

        if(() == MotionEvent.ACTION_UP || () == MotionEvent.ACTION_CANCEL){
            //When up, determine whether the coordinates of pressing and releasing are the same as the point            //If it is a point, the click event will be executed. This is the click event I wrote myself, not onclick            getParent().requestDisallowInterceptTouchEvent(false);
            if(== &amp;&amp; ==){
                return true;
            }
        }
        (arg0); //Note that this sentence cannot return (arg0); otherwise, parent slide will be triggered.        return true;
    }
}

use

<com.coral3.common_module.
            android:
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.