RecycleView 嵌套RecycleView 實(shí)現(xiàn)item自動循環(huán)滾動

利用RecyclerView實(shí)現(xiàn)無限循環(huán)滾動


一赤赊、先上效果圖

滾動

二、GitHub地址

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

三疫萤、思路

RecycleView實(shí)現(xiàn)芝囤,內(nèi)部實(shí)現(xiàn)Runnable滾動

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

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

   static class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> 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);//每次滾動距離
                /**
                 * 判斷是否為無限循環(huán)
                 */
                if (recyclerView.canRun){
                    /**
                     * 判斷是否觸底
                     */
                    if (isSlideToBottom(recyclerView)) {
                        /**跳至頂部*/
                        recyclerView.smoothScrollToPosition(0);
                    }
                }

                recyclerView.postDelayed(recyclerView.autoPollTask,TIME_AUTO_POLL);
            }
        }
    }
    
    /**判斷Recycler是否滑動至最底部  是返回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)開啟滾動

  //開啟
    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)開始滾動
*/
if (item.list.size() > 0) {
    if (item.list.size() <= 3) {
        viewHolder.autoPollRecyclerView.setCanRun(false);
    } else {
        viewHolder.autoPollRecyclerView.setCanRun(true);
    }
}

6)自動滾動RecycleView完整代碼

public class AutoPollRecyclerView extends RecyclerView {
    private static final long TIME_AUTO_POLL = 30;
    private AutoPollTask autoPollTask;
    private boolean running; //是否正在自動輪詢
    private boolean canRun;//是否可以自動輪詢,可在不需要的是否置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<AutoPollRecyclerView> 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);//每次滾動距離
                /**
                 * 判斷是否為無限循環(huán)
                 */
                if (recyclerView.canRun){
                    /**
                     * 判斷是否觸底
                     */
                    if (isSlideToBottom(recyclerView)) {
                        /**跳至頂部*/
                        recyclerView.smoothScrollToPosition(0);
                    }
                }

                recyclerView.postDelayed(recyclerView.autoPollTask,TIME_AUTO_POLL);
            }
        }
    }
    /**判斷Recycler是否滑動至最底部  是返回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)

如有意見和建議坪哄,及時溝通。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末势篡,一起剝皮案震驚了整個濱河市翩肌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌禁悠,老刑警劉巖念祭,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異碍侦,居然都是意外死亡粱坤,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門瓷产,熙熙樓的掌柜王于貴愁眉苦臉地迎上來站玄,“玉大人,你說我怎么就攤上這事濒旦≈昕酰” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長晾剖。 經(jīng)常有香客問我锉矢,道長,這世上最難降的妖魔是什么齿尽? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任沽损,我火速辦了婚禮,結(jié)果婚禮上循头,老公的妹妹穿的比我還像新娘绵估。我一直安慰自己,他們只是感情好贷岸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布壹士。 她就那樣靜靜地躺著磷雇,像睡著了一般偿警。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上唯笙,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天螟蒸,我揣著相機(jī)與錄音,去河邊找鬼崩掘。 笑死七嫌,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的苞慢。 我是一名探鬼主播诵原,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼挽放!你這毒婦竟也來了绍赛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤辑畦,失蹤者是張志新(化名)和其女友劉穎吗蚌,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纯出,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蚯妇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了暂筝。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箩言。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖焕襟,靈堂內(nèi)的尸體忽然破棺而出陨收,到底是詐尸還是另有隱情,我是刑警寧澤胧洒,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布畏吓,位于F島的核電站墨状,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏菲饼。R本人自食惡果不足惜肾砂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宏悦。 院中可真熱鬧镐确,春花似錦、人聲如沸饼煞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽砖瞧。三九已至息堂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間块促,已是汗流浹背荣堰。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留竭翠,地道東北人振坚。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像斋扰,于是被迫代替她去往敵國和親渡八。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容