欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android轮播图组件的制作方法

程序员文章站 2022-03-16 20:41:11
本文实例为大家分享了android轮播图组件的制作方法,供大家参考,具体内容如下bannerlayoutpackage com.coral3.common_module.components;impo...

本文实例为大家分享了android轮播图组件的制作方法,供大家参考,具体内容如下

bannerlayout

package com.coral3.common_module.components;

import android.content.context;
import android.os.handler;
import android.os.message;
import android.util.attributeset;
import android.view.layoutinflater;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;
import android.widget.linearlayout;
import androidx.annotation.nonnull;
import androidx.annotation.nullable;
import androidx.viewpager.widget.pageradapter;
import androidx.viewpager.widget.viewpager;
import com.coral3.common_module.r;
import com.coral3.common_module.utils.logutil;
import com.coral3.common_module.viewpager.childviewpager;
import java.util.arraylist;
import java.util.list;
import java.util.concurrent.atomic.atomicinteger;

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 handler.callback(){

        @override
        public boolean handlemessage(message message) {
            viewpager.setcurrentitem(message.what);
            return false;
        }
    });

    public bannerlayout(context context, @nullable attributeset attrs) {
        super(context, attrs);
        mcontext = context;
        initview();
        initlistener();
    }

    private void initview(){
        view = layoutinflater.from(mcontext).inflate(r.layout.layout_banner, this);
        group = view.findviewbyid(r.id.view_indicators);
        viewpager = view.findviewbyid(r.id.view_banners);
        // 动态加入图片
        list<view> listpics = new arraylist<>();
        imageview img1 = new imageview(mcontext);
        img1.setbackgroundresource(r.drawable.banner1);
        listpics.add(img1);
        imageview img2 = new imageview(mcontext);
        img2.setbackgroundresource(r.drawable.banner2);
        listpics.add(img2);
        imageview img3 = new imageview(mcontext);
        img3.setbackgroundresource(r.drawable.banner3);
        listpics.add(img3);
        imageview img4 = new imageview(mcontext);
        img4.setbackgroundresource(r.drawable.banner4);
        listpics.add(img4);
        imageview img5 = new imageview(mcontext);
        img5.setbackgroundresource(r.drawable.banner4);
        listpics.add(0, img5);
        imageview img0 = new imageview(mcontext);
        img0.setbackgroundresource(r.drawable.banner1);
        listpics.add(img0);
        //动态加入指示器
        indicators = new imageview[listpics.size()];
        for(int i = 0; i < indicators.length; i++){
            indicator = new imageview(mcontext);
            linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(15, 15);
            layoutparams.setmargins(0, 0, 10, 0);
            indicator.setlayoutparams(layoutparams);
            indicators[i] = indicator;
            if(i == 1){
                indicators[i].setbackgroundresource(r.drawable.shape_banner_checked);
            }else{
                indicators[i].setbackgroundresource(r.drawable.shape_banner_unchecked);
            }
            if(i == 0 || i == 5){
                indicators[i].setvisibility(view.invisible);
            }
            group.addview(indicators[i]);
        }
        viewpager.setadapter(new mypageradapter(listpics));
        index.incrementandget();
        // 轮播
        new thread(new runnable() {

            @override
            public void run() {
                while (true){
                    if(iscontinue){
                        handler.sendemptymessage(index.get());
                        whatoption();
                    }
                }
            }
        }).start();
    }

    private void initlistener(){
        // 设置监听器
        viewpager.addonpagechangelistener(new viewpager.onpagechangelistener() {

            @override
            public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {
                logutil.d(positionoffset + "-" + positionoffsetpixels);
                // 无缝滚动均滑
//                if(positionoffset == 0.0){
//                    logutil.d(position + "");
//                    if(position == 5) {
//                        viewpager.setcurrentitem(1, false);
//                    }
//                    if(position == 0) {
//                        viewpager.setcurrentitem(4, false);
//                    }
//                }
            }

            @override
            public void onpageselected(int position) {
                index.getandset(position);
                if(position == 5) {
                    viewpager.setcurrentitem(1, false);
                }
                if(position == 0) {
                    viewpager.setcurrentitem(4, false);
                }
                for(int i = 0; i < indicators.length; i++){
                    if(i == index.get()){
                        indicators[i].setbackgroundresource(r.drawable.shape_banner_checked);
                    }else{
                        indicators[i].setbackgroundresource(r.drawable.shape_banner_unchecked);
                    }
                }
                if(position == 0) indicators[4].setbackgroundresource(r.drawable.shape_banner_checked);
                if(position == 5) indicators[1].setbackgroundresource(r.drawable.shape_banner_checked);
            }

            @override
            public void onpagescrollstatechanged(int state) {}
        });
        // 设置触摸时停止定时
        viewpager.setontouchlistener(new view.ontouchlistener() {

            @override
            public boolean ontouch(view view, motionevent motionevent) {
                switch (motionevent.getaction()){
                    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 listview.size();
        }

        public mypageradapter(list<view> listview){
            this.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) {
            container.addview(listview.get(position));
            return listview.get(position);
        }

        @override
        public void destroyitem(@nonnull viewgroup container, int position, @nonnull object object) {
            container.removeview(listview.get(position));
        }
    }

    private void whatoption(){
        index.incrementandget();
        if(index.get() > indicators.length - 2){
            index.getandadd(-4);
        }
        try {
            thread.sleep(3000);
        } catch (interruptedexception e) {
            e.printstacktrace();
        }
    }
}

layout_banner

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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.viewpager.childviewpager
            android:id="@+id/view_banners"
            android:layout_width="match_parent"
            android:layout_height="200dp"/>
        <linearlayout
            android:id="@+id/view_indicators"
            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 android.content.context;
import android.graphics.pointf;
import android.util.attributeset;
import android.view.motionevent;
import androidx.viewpager.widget.viewpager;

public class childviewpager extends viewpager {
    /** 触摸时按下的点 **/
    pointf downp = new pointf();
    /** 触摸时当前的点 **/
    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) {
        //每次进行ontouch事件都记录当前的按下的坐标
        if(getchildcount()<=1)
        {
            return super.ontouchevent(arg0);
        }
        curp.x = arg0.getx();
        curp.y = arg0.gety();

        if(arg0.getaction() == motionevent.action_down)
        {

            //记录按下时候的坐标
            //切记不可用 downp = curp ,这样在改变curp的时候,downp也会改变
            downp.x = arg0.getx();
            downp.y = arg0.gety();
            //此句代码是为了通知他的父viewpager现在进行的是本控件的操作,不要对我的操作进行干扰
            getparent().requestdisallowintercepttouchevent(true);
        }

        if(arg0.getaction() == motionevent.action_move){
            //此句代码是为了通知他的父viewpager现在进行的是本控件的操作,不要对我的操作进行干扰
            getparent().requestdisallowintercepttouchevent(true);
        }

        if(arg0.getaction() == motionevent.action_up || arg0.getaction() == motionevent.action_cancel){
            //在up时判断是否按下和松手的坐标为一个点
            //如果是一个点,将执行点击事件,这是我自己写的点击事件,而不是onclick
            getparent().requestdisallowintercepttouchevent(false);
            if(downp.x==curp.x && downp.y==curp.y){
                return true;
            }
        }
        super.ontouchevent(arg0); //注意这句不能 return super.ontouchevent(arg0); 否则触发parent滑动
        return true;
    }
}

使用

<com.coral3.common_module.components.bannerlayout
            android:id="@+id/home_banner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: android 轮播图