日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android item弹出popupwindow recycleview_Android实现RecycleView嵌套RecycleView中的item自动循环滚动功能...

發(fā)布時(shí)間:2025/3/15 Android 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android item弹出popupwindow recycleview_Android实现RecycleView嵌套RecycleView中的item自动循环滚动功能... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
利用RecyclerView實(shí)現(xiàn)無限循環(huán)滾動(dòng)

一、先上效果圖

二、GitHub地址

GitHub地址 https://github.com/MNXP/AutomaticRolling?

三、思路

RecycleView實(shí)現(xiàn),內(nèi)部實(shí)現(xiàn)Runnable滾動(dòng)

四、實(shí)現(xiàn)

1)內(nèi)部實(shí)現(xiàn)Runnable

static class AutoPollTask implements Runnable { private final WeakReference mReference; /** * 使用弱引用持有外部類引用,防止內(nèi)存泄漏 */ private AutoPollTask(AutoPollRecyclerView reference) { this.mReference = new WeakReference<>(reference); } @Override public void run() { AutoPollRecyclerView recyclerView = mReference.get(); if (recyclerView != null && recyclerView.running) { recyclerView.scrollBy(2, 2);//每次滾動(dòng)距離 /** * 判斷是否為無限循環(huán) */ if (recyclerView.canRun){ /** * 判斷是否觸底 */ if (isSlideToBottom(recyclerView)) { /**跳至頂部*/ recyclerView.smoothScrollToPosition(0); } } recyclerView.postDelayed(recyclerView.autoPollTask,TIME_AUTO_POLL); } } } /**判斷Recycler是否滑動(dòng)至最底部 是返回true 不是返回false*/ public static boolean isSlideToBottom(RecyclerView recyclerView) { if (recyclerView == null) return false; if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()) return true; return false; }

2)開啟滾動(dòng)

//開啟 public void start() { if (running){ return; } canRun = true; running = true; postDelayed(autoPollTask,TIME_AUTO_POLL); } public void setCanRun(boolean canRun){ this.canRun = canRun; }

3)設(shè)置最大高度(沒用到可以去掉)

@Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); int heightMode = MeasureSpec.getMode(heightSpec); int heightSize = MeasureSpec.getSize(heightSpec); if (heightMode == MeasureSpec.EXACTLY) { heightSize = heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight; } if (heightMode == MeasureSpec.UNSPECIFIED) { heightSize = heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight; } if (heightMode == MeasureSpec.AT_MOST) { heightSize = heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight; } int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode); super.onMeasure(widthSpec, maxHeightMeasureSpec); }

4)自身Adapter設(shè)置

@Override public int getItemCount() { if (mData!=null){ if (mData.size()<=3) return mData.size(); else return mData.size()*20;//return Integer.MAX_VALUE; }else { return 0; }????}

5)引用

autoPollRecyclerView = (AutoPollRecyclerView) itemView.findViewById(R.id.main_recycler);autoPollRecyclerView.start();autoPollRecyclerView.setNestedScrollingEnabled(false);autoPollRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL, false));((SimpleItemAnimator) autoPollRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);/*** 我的是超過3項(xiàng)開始滾動(dòng)*/if (item.list.size() > 0) { if (item.list.size() <= 3) { viewHolder.autoPollRecyclerView.setCanRun(false); } else { viewHolder.autoPollRecyclerView.setCanRun(true); }}

6)自動(dòng)滾動(dòng)RecycleView完整代碼

public class AutoPollRecyclerView extends RecyclerView { private static final long TIME_AUTO_POLL = 30; private AutoPollTask autoPollTask; private boolean running; //是否正在自動(dòng)輪詢 private boolean canRun;//是否可以自動(dòng)輪詢,可在不需要的是否置false private int mMaxHeight;//最大高度 public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); autoPollTask = new AutoPollTask(this); mMaxHeight = dip2px(context,100); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { return false; } static class AutoPollTask implements Runnable { private final WeakReference mReference; /** * 使用弱引用持有外部類引用,防止內(nèi)存泄漏 */ private AutoPollTask(AutoPollRecyclerView reference) { this.mReference = new WeakReference<>(reference); } @Override public void run() { AutoPollRecyclerView recyclerView = mReference.get(); if (recyclerView != null && recyclerView.running) { recyclerView.scrollBy(2, 2);//每次滾動(dòng)距離 /** * 判斷是否為無限循環(huán) */ if (recyclerView.canRun){ /** * 判斷是否觸底 */ if (isSlideToBottom(recyclerView)) { /**跳至頂部*/ recyclerView.smoothScrollToPosition(0); } } recyclerView.postDelayed(recyclerView.autoPollTask,TIME_AUTO_POLL); } } } /**判斷Recycler是否滑動(dòng)至最底部 是返回true 不是返回false*/ public static boolean isSlideToBottom(RecyclerView recyclerView) { if (recyclerView == null) return false; if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()) return true; return false; } @Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); int heightMode = MeasureSpec.getMode(heightSpec); int heightSize = MeasureSpec.getSize(heightSpec); if (heightMode == MeasureSpec.EXACTLY) { heightSize = heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight; } if (heightMode == MeasureSpec.UNSPECIFIED) { heightSize = heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight; } if (heightMode == MeasureSpec.AT_MOST) { heightSize = heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight; } int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode); super.onMeasure(widthSpec, maxHeightMeasureSpec); } //開啟 public void start() { if (running){ return; } canRun = true; running = true; postDelayed(autoPollTask,TIME_AUTO_POLL); } public void setCanRun(boolean canRun){ this.canRun = canRun; } /** * 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素) */ public int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f);????}}

7)小小的建議

RecycleView實(shí)現(xiàn),首頁列表,加載圖片有卡頓 卡頓優(yōu)化可以使用vLayout(阿里開源庫)處理挺好的,它只要是用于處理列表不同布局 vLayout地址 (https://github.com/alibaba/vlayout)到這里就結(jié)束啦.往期精彩回顧:
  • Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填充功能

  • Android仿echo精美彈幕功能

  • Android實(shí)現(xiàn)頭像重疊排列功能

  • Android仿QQ個(gè)性標(biāo)簽功能

  • Android仿QQ側(cè)滑刪除的功能

總結(jié)

以上是生活随笔為你收集整理的android item弹出popupwindow recycleview_Android实现RecycleView嵌套RecycleView中的item自动循环滚动功能...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。