第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

發(fā)生 java.lang.IllegalArgumentException

發(fā)生 java.lang.IllegalArgumentException

BIG陽 2023-07-28 15:45:07
我編寫了一個(gè)處理程序來維護(hù)回收器視圖的自動(dòng)滾動(dòng)。工作正常。但是當(dāng)回收器視圖中只有一項(xiàng)時(shí)我遇到的問題。我的應(yīng)用程序崩潰了,當(dāng)我檢查 logcat 時(shí),我收到類似 java.lang.IllegalArgumentException: 無效目標(biāo)位置的錯(cuò)誤。這是我的自定義 LinearLayoutManager 類public class CustomLinearLayoutManager extends LinearLayoutManager {    public CustomLinearLayoutManager (Context context) {        super(context);    }    public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {        super(context, orientation, reverseLayout);    }    public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }    @Override    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {        final LinearSmoothScroller linearSmoothScroller =                new LinearSmoothScroller(recyclerView.getContext()) {                    private static final float MILLISECONDS_PER_INCH = 100f;                    @Override                    public PointF computeScrollVectorForPosition(int targetPosition) {                        return CustomLinearLayoutManager.this                                .computeScrollVectorForPosition(targetPosition);                    }                    @Override                    protected float calculateSpeedPerPixel                            (DisplayMetrics displayMetrics) {                        return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;                    }                };        linearSmoothScroller.setTargetPosition(position);        startSmoothScroll(linearSmoothScroller);    }}這是該行的一個(gè)獲取錯(cuò)誤:startSmoothScroll(linearSmoothScroller);錯(cuò)誤是 - CustomLinearLayoutManager.smoothScrollToPosition java.lang.IllegalArgumentException:目標(biāo)位置無效這是該行的一個(gè)獲取錯(cuò)誤:recyclerViewHeaderSlider.smoothScrollToPosition(count);錯(cuò)誤是 - java.lang.IllegalArgumentException:目標(biāo)位置無效
查看完整描述

3 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

該錯(cuò)誤是由于負(fù)索引 (-1) 造成的。


看這段代碼:


if (count == headerSliderAdapter.getItemCount() - 1) {

    flag = false;

} else if (count == 0) {

    flag = true;

}

如果您的項(xiàng)目數(shù)為 1,那么第一個(gè)項(xiàng)目if將true在 時(shí)出現(xiàn)count == 0。1 - 1 = 0 所以flag = false。


然后,當(dāng)你到達(dá)第二個(gè)時(shí)if:


if (flag) count++;

else count--;

flag是false這樣你的代碼將執(zhí)行count--但count已經(jīng)是 0,因此你得到count == -1.


然后你嘗試滾動(dòng)到負(fù)位置,這是不允許的。


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

不要使用非線程安全的延遲后方法。


    private fun scrollToLastItem(view: View) {

      //pos.coerceAtLeast(0) // Use this 

       view.recycler_view.smoothScrollToPosition(pos.coerceAtLeast(0))

    }

RCA:在 ScrollLayoutManager startSmoothPendingScroll 方法崩潰之前,當(dāng)前位置為 -1。


查看完整回答
反對(duì) 回復(fù) 2023-07-28
?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

它也可以(在最后一個(gè)位置的情況下):


private fun scrollToLastItem(view: View) {

    adapter.itemCount.takeIf { it > 0 }?.let {

        view.recycler_view.smoothScrollToPosition(it - 1)

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-28
  • 3 回答
  • 0 關(guān)注
  • 195 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)