Android 可手动管理并垂直滚动轮播控件

Posted 编程很难

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 可手动管理并垂直滚动轮播控件相关的知识,希望对你有一定的参考价值。

A : 如何做到滚动并手动操控轮播?
B :	当然使用ViewPager了
A : 嗯 是这样的  ViewPager2就可以垂直了呀
博主: androidx 项目开发者请自行离开 ViewPager2的确可以实现
B : 项目使用的com.android.support:appcompat怎么办呢?
A :把整体项目升级到androidx
博主:的确是个好方法 未免忒费事了吧!!!后续会出一篇《如何升级androidx项目》
B :做为开发者 代码只要跑起来就不要乱动了 升级暂不采纳 会出现很多冲突!
A : ......
B : 那自定义一个垂直的 ViewPager

垂直滚动的ViewPager

public class VerticalViewPager extends ViewPager{
    public VerticalViewPager(Context context) {
        this(context, null);
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        //设置viewpage的切换动画,真正实现垂直滑动的viewpager
        setPageTransformer(true, new VerticalTransformer());
    }

    /**
     * 拦截touch事件
     *
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = super.onInterceptTouchEvent(swapEvent(ev));
        swapEvent(ev);
        return intercept;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(swapEvent(ev));
    }

    private MotionEvent swapEvent(MotionEvent event) {
        //获取宽高
        float width = getWidth();
        float height = getHeight();
        //将Y轴的移动距离转变成X轴的移动距离
        float swappedX = (event.getY() / height) * width;
        //将X轴的移动距离转变成Y轴的移动距离
        float swappedY = (event.getX() / width) * height;
        //重设event的位置
        event.setLocation(swappedX, swappedY);
        return event;
    }
    
    public classVerticalTransformer implements ViewPager.PageTransformer {

        public static final String TAG = "classVerticalTransformer";

        @Override
        public void transformPage(View view, float position) {

            float alpha = 0;
            if (0 <= position && position <= 1) {
                alpha = 1 - position;
            } else if (-1 < position && position < 0) {
                alpha = position + 1;
            }
            view.setAlpha(alpha);
            float transX = view.getWidth() * -position;
            view.setTranslationX(transX);
            float transY = position * view.getHeight();
            view.setTranslationY(transY);
        }
    }
}
A : 你小子真有你的真的可以垂直滚动了?那轮播呢?不能每次都手动吧!
B : ......(心中在咒骂)
博主 : 可以参考我之前的一篇自定义轮播  下方快捷入口

快捷入口

public class AutoTextViewPager extends FrameLayout {

    public Context mContext;
    public View inflate;
    public VerticalViewPager pager;
    private PagerAdapter adapter;

    private boolean canAuto=true;
    public ViewPager.OnPageChangeListener pageChangeListener;

    public void setPageChangeListener(ViewPager.OnPageChangeListener pageChangeListener) {
        this.pageChangeListener = pageChangeListener;
        if (pager != null) {
            pager.addOnPageChangeListener(pageChangeListener);
        }
    }

    public boolean isCanAuto() {
        return canAuto;
    }

    public void setCanAuto(boolean canAuto) {
        this.canAuto = canAuto;
        if (!canAuto){
            stop();
        }
    }

    AutoHandler mHandler;

    public PagerAdapter getAdapter() {
        return adapter;
    }

    public void setAdapter(PagerAdapter adapter) {
        this.adapter = adapter;
        pager.setAdapter(adapter);

    }

    public AutoTextViewPager(@NonNull Context context) {
        this(context, null);
    }

    public AutoTextViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public AutoTextViewPager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inti(context, attrs, defStyleAttr);
    }


    private void inti(Context context, AttributeSet attrs, int defStyleAttr) {
        mContext = context;
        inflate = LayoutInflater.from(context).inflate(R.layout.auto_viewpager1, this, true);

        pager = ((VerticalViewPager) inflate.findViewById(R.id.pager));

        mHandler = new AutoHandler(pager);
        if (canAuto) {
            start();
        }

        pager.addOnPageChangeListener(pageChangeListener);
    }

    public void start(){
        mHandler.start();
    }

    public void stop(){
        mHandler.stop();
    }


    public interface PagerItemClickListener {
        public void onItemClick(View view, int pos);
    }

    public abstract static class AutoAdapter<T> extends PagerAdapter implements OnClickListener,PagerItemClickListener {
        private List<T> datas;
        private Context context;

        public abstract String getImageUrl(T data);
        public abstract int getLayout();
        public abstract void handleItem(View view, int pos);

        @Override
        public void onItemClick(View view, int pos) {

        }

        public AutoAdapter(Context context, List<T> datas) {
            this.context = context;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            if (datas != null) {
                return datas.size();
            }
            return 0;
        }

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

            if (getLayout()>0) {
                View inflate = LayoutInflater.from(context).inflate(getLayout(), null);
                container.addView(inflate);
                handleItem(inflate,position);
                inflate.setTag(R.id.auto_id, position);
                inflate.setOnClickListener(this);
                return inflate;
            }else {
                ImageView img = new ImageView(context);
                container.addView(img);

                Glide.with(context).load(getImageUrl(datas.get(position)))
                        .apply(new RequestOptions().centerCrop())
                        .into(img);
                img.setTag(R.id.auto_id, position);
                img.setOnClickListener(this);
                return img;
            }
        }

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

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

        @Override
        public void onClick(View v) {
            int tag = (int) v.getTag(R.id.auto_id);

            onItemClick(v,tag);

        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }
    }


}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.***.***.VerticalViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>
public class  AutoHandler extends Handler {

    public static int AUTO_TIME = 1000 * 5;

    private ViewPager pager;
    private boolean stopHandle;


    public AutoHandler(ViewPager pager) {
        this.pager = pager;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 100:

                if (!stopHandle) {
                    sendEmptyMessageDelayed(100, AUTO_TIME);

                    int currentItem = pager.getCurrentItem() + 1;

                    if (currentItem >= pager.getAdapter().getCount()) {
                        currentItem = 0;
                    }
                    pager.setCurrentItem(currentItem);
                }else {
                    removeMessages(100);
                }
                break;
            default:
                removeMessages(100);
                break;
        }
    }


    public void start(){
        stopHandle=false;
        if (!hasMessages(100)) {
            sendEmptyMessageDelayed(100, AutoHandler.AUTO_TIME);
        }
    }

    public void stop(){
        stopHandle=true;
        while (hasMessages(100)) {
            removeMessages(100);
        }
    }

}

博主 : 到这就可以实现垂直轮播并手动控制了 就不配图了 大家自己试试看吧
博主 : 觉得还不错记得给点个赞 评个论哦

以上是关于Android 可手动管理并垂直滚动轮播控件的主要内容,如果未能解决你的问题,请参考以下文章

Android 可手动管理并垂直滚动轮播控件

Android Studio TextView上下滚动轮播

Android Studio TextView上下滚动轮播

js原生选项(包含无缝滚动轮播图)一

如何在 Android 中创建可滚动的轮播页面?

js 实现横向滚动轮播并中间暂停下