前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android实现类似中奖信息自动滚动效果

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

作者头像
SoullessCoder
修改2021-02-03 09:53:23
3.3K0
修改2021-02-03 09:53:23
举报
文章被收录于专栏:CodeCode

最近需要实现抽奖功能,就需要把中奖的信息,以垂直循环滚动的形式向用户进行展示,这篇主要是使用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源码的童鞋可以在底部公众号回复:"自动滚动效果" 即可获取----


小编整理了一份Android电子书籍,需要的童鞋关注公众号回复:"e_books" 即可获取哦!

在这里插入图片描述
在这里插入图片描述

欢迎关注公众号(longxuanzhigu),获得更多福利、精彩内容哦!

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档