<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android實(shí)現(xiàn)獲獎(jiǎng)名單滾動(dòng)效果

          共 2774字,需瀏覽 6分鐘

           ·

          2021-03-01 13:15

          最近公司項(xiàng)目需要一個(gè)展示獲獎(jiǎng)名單的效果,做完之后在此分享出來(lái)。


          效果展示



          思路分析

          獲獎(jiǎng)名單是一個(gè)無(wú)限滾動(dòng)的效果,因此我們可以想到通過(guò)使用RecyclerView來(lái)加載無(wú)限的條目,然后通過(guò)不斷的自動(dòng)向下滑動(dòng)RecyclerView來(lái)實(shí)現(xiàn)這個(gè)效果,但是獲獎(jiǎng)名單不能手動(dòng)滑動(dòng)因此我們需要將其滑動(dòng)功能屏蔽掉。


          實(shí)現(xiàn)禁止滑動(dòng)的RecyclerView

          public class MyRecyclerView extends RecyclerView {    public MyRecyclerView(Context context) {        this(context,null);    }
          public MyRecyclerView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); }
          public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } //禁止手動(dòng)滑動(dòng) @Override public boolean dispatchTouchEvent(MotionEvent ev) { return true; }}


          Adapter編寫(xiě)(實(shí)現(xiàn)加載無(wú)限重復(fù)數(shù)據(jù)的關(guān)鍵)

          public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{    //這里的WinnListBean是我模擬的中獎(jiǎng)數(shù)據(jù)    private List<WinnListBean> mDatas;    private Context mContext;
          public MyAdapter(List<WinnListBean> mDatas, Context mContext) { this.mDatas = mDatas; this.mContext = mContext; }
          @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_winnlist,null,false); return new MyViewHolder(view); }
          @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { WinnListBean winnListBean = mDatas.get(i % mDatas.size());//獲取重復(fù)數(shù)據(jù) myViewHolder.itemWinnlistTvPhone.setText(winnListBean.getPhone()); myViewHolder.itemWinnlistTvPrize.setText(winnListBean.getPrize()); } @Override public int getItemCount() {// 本案例一頁(yè)最多顯示6條數(shù)據(jù),因此設(shè)置如果數(shù)據(jù)數(shù)量大于6等于6才開(kāi)始滾動(dòng) if(mDatas.size()>5){// 展示無(wú)限數(shù)據(jù) return Integer.MAX_VALUE; }else { return mDatas.size(); } } static class MyViewHolder extends RecyclerView.ViewHolder{ private TextView itemWinnlistTvPhone; private TextView itemWinnlistTvPrize; public MyViewHolder(@NonNull View itemView) { super(itemView); itemWinnlistTvPhone = (TextView) itemView.findViewById(R.id.item_winnlist_tv_phone); itemWinnlistTvPrize = (TextView) itemView.findViewById(R.id.item_winnlist_tv_prize); } }}


          實(shí)現(xiàn)自動(dòng)滾動(dòng)

          這里的自動(dòng)滾動(dòng)是使用的RecyclerView的smoothScrollBy()方法結(jié)合Handler來(lái)實(shí)現(xiàn)的。

          mHandler=new Handler();        mRunnable = new Runnable() {            @Override            public void run() {                //RecyclerView每隔200毫秒向下滾動(dòng)一次                mRecyclerView.smoothScrollBy(0,5);                mHandler.postDelayed(this,200);            }        };mHandler.post(mRunnable);


          源碼地址:

          https://gitee.com/itfitness/WinnListDemo


          到這里就結(jié)束啦


          點(diǎn)擊這里留言交流哦


          瀏覽 109
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  二区三区免费视频 | 免费观看一级一片 | 免费成人先锋影音中出片 | 7799视频| 日韩2024无码 |