Android实现类似中奖信息自动滚动效果

最近需要实现抽奖功能,就需要把中奖的信息,以垂直循环滚动的形式向用户进行展示,这篇主要是使用RecyclerView实现垂直滚动效果,九宫格抽奖功能会在后期写,那现在就来看看实现的过程吧。

实现步骤: 1.效果图展示 2.自定义实现滚动效果RecyclerView 3.适配器Adapter实现 4.适配器布局文件 5.主程序调用过程 6.主布局文件 7.总结

实现过程: 1.效果图展示

在这里插入图片描述

2.自定义实现滚动效果RecyclerView

public class AutoPollRecyclerView extends RecyclerView {
    private static final long TIME_AUTO_POLL = 16;
    AutoPollTask autoPollTask;
    private boolean running; //标示是否正在自动轮询
    private boolean canRun;//标示是否可以自动轮询,可在不需要的是否置false

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        autoPollTask = new AutoPollTask(this);
    }

    static class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;

        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(AutoPollRecyclerView reference) {
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }

        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollBy(2, 2);
                recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
            }
        }
    }

    //开启:如果正在运行,先停止->再开启
    public void start() {
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }

    public void stop() {
        running = false;
        removeCallbacks(autoPollTask);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (running)
                    stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        //return  false,注释掉onTouchEvent()方法里面的stop和start方法,则列表自动滚动且不可触摸
        return super.onTouchEvent(e);}
}

3.适配器Adapter实现

public class AutoPollAdapter extends RecyclerView.Adapter<AutoPollAdapter.BaseViewHolder> {
    private final Context mContext;
    private final List<AutoScrollLuckyListReq.DatasBean> mData;

    public AutoPollAdapter(Context context, List<AutoScrollLuckyListReq.DatasBean> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.auto_list_item, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        AutoScrollLuckyListReq.DatasBean datasBean = mData.get(position % mData.size());
        holder.content.setText(datasBean.getPhone() + " 获得 " + datasBean.getGiftName());
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView content;

        public BaseViewHolder(View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.content);
        }
    }
}

4.适配器布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fffcf5"
    android:orientation="vertical">

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="135xxxx8888 获得 8积分"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:textColor="#797762"
        android:textSize="14dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#797762"/>
</LinearLayout>

5.主程序调用过程

public class MainActivity extends AppCompatActivity {

    private AutoPollRecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        recyclerView = findViewById(R.id.recyclerview);

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));//设置LinearLayoutManager.HORIZONTAL  则水平滚动

    }

    private void initData() {
        String reponse = "{\n" +
                "    \"datas\": [\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"三花便签(20枚)\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"三花便签(20枚)\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"三花便签(20枚)\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"三花便签(20枚)\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"满10减1元\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"giftName\": \"20个积分\",\n" +
                "            \"phone\": \"****\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"msg\": \"success\",\n" +
                "    \"ret\": 0\n" +
                "}";


        AutoScrollLuckyListReq autoScrollLuckyListReq = new Gson().fromJson(reponse, AutoScrollLuckyListReq.class);
        //0为请求成功
        if (autoScrollLuckyListReq.getRet() == 0) {
            AutoPollAdapter autoPollAdapter = new AutoPollAdapter(getApplicationContext(), autoScrollLuckyListReq.getDatas());
            recyclerView.setAdapter(autoPollAdapter);
            //启动滚动
            recyclerView.start();
        }
    }

    private void initListener() {

    }
}

6.主布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_centerInParent="true"
        android:layout_marginRight="15dp"
        android:background="@drawable/luckyer_bg">

        <RelativeLayout
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="23dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:background="#fff6ea">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:src="@drawable/zhongjiangzhemingdan" />
        </RelativeLayout>

        <com.showly.autopollrecyclerviewdemo.view.AutoPollRecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp" />
    </LinearLayout>
</RelativeLayout>

7.总结

类似中奖信息自动滚动效果到这里就实现了,实现过程还是相对比较简单的,重点在于自定义RecyclerView的实现处理。

需要Demo源码的童鞋可以在底部公众号回复:"自动滚动效果" 即可获取


本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Flutter笔记

Flutter AnimatedList 源码分析

可以看到和普通的没什么区别,那我们再来找一下怎么添加/删除item以及添加/删除时是如何设置动画的。

10120
来自专栏微卡智享

Android里用AsyncTask后的接口回调

AsyncTask,即异步任务,是Android给我们提供的一个处理异步任务的类.通过此类,可以实现UI线程和后台线程进行通讯,后台线程执行异步任务,并把结果返...

12240
来自专栏Android相关

ActivityManagerService--startActivity的流程

该流程图为:当Activity目标进程不存在时,启动Activity的流程。若进程相同的话,也只是省略了启动进程的步骤。

28420
来自专栏烟草的香味

23种设计模式之责任链模式

定义: 使多个对象都有机会处理请求, 从而避免了请求的发送者和接受者之间的耦合关系. 将这些对象连成一条链, 并沿着这条链传递该请求,直到有对象处理它为止

7220
来自专栏微卡智享

Android NDK编程(一)---NDK介绍及环境搭建

NDK全称为Native Development Kit,是Android的一个工具开发包,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应...

12020
来自专栏微卡智享

Android使用Tesseract-ocr进行文字识别

Tessseract是一款由HP实验室开发由 Google 维护的开源 OCR(Optical Character Recognition , 光学字符识别)引...

85830
来自专栏Flutter笔记

Flutter Wrap & Chip

一般来讲是一个 RecyclerView + 自动换行的 layoutManager + 自定义的background。

9420
来自专栏微卡智享

Android利用SurfaceView显示Camera图像爬坑记(六) -- 用OpenCV进行Canny边缘检测

上一篇《Android利用SurfaceView显示Camera图像爬坑记(五) -- 在现有项目中加入NDK配置》中我们已经把NDK加入到SurfaceVie...

17620
来自专栏Flutter笔记

Flutter SafeArea - 异形屏适配利器

现在的手机已经不是方方正正的屏幕了,所以我们在写一些UI的时候可能会出现如下问题:

60820
来自专栏Flutter笔记

Flutter Spacer 灵活配置你的Row/Column

我们平时在写 Row/Column 的时候,一般会配置一下子widget 的排列方式。

14840

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励