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

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

程序员文章站 2022-06-09 23:27:26
...

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

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

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

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

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源码的童鞋可以在底部公众号回复:“自动滚动效果” 即可获取


以下是公众号(longxuanzhigu),之后发布的文章会同步到该公众号,方便交流学习Android知识及分享个人爱好文章:
Android实现类似中奖信息自动滚动效果