亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

發生 java.lang.IllegalArgumentException

發生 java.lang.IllegalArgumentException

BIG陽 2023-07-28 15:45:07
我編寫了一個處理程序來維護回收器視圖的自動滾動。工作正常。但是當回收器視圖中只有一項時我遇到的問題。我的應用程序崩潰了,當我檢查 logcat 時,我收到類似 java.lang.IllegalArgumentException: 無效目標位置的錯誤。這是我的自定義 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);    }}這是該行的一個獲取錯誤:startSmoothScroll(linearSmoothScroller);錯誤是 - CustomLinearLayoutManager.smoothScrollToPosition java.lang.IllegalArgumentException:目標位置無效這是該行的一個獲取錯誤:recyclerViewHeaderSlider.smoothScrollToPosition(count);錯誤是 - java.lang.IllegalArgumentException:目標位置無效
查看完整描述

3 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

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


看這段代碼:


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

    flag = false;

} else if (count == 0) {

    flag = true;

}

如果您的項目數為 1,那么第一個項目if將true在 時出現count == 0。1 - 1 = 0 所以flag = false。


然后,當你到達第二個時if:


if (flag) count++;

else count--;

flag是false這樣你的代碼將執行count--但count已經是 0,因此你得到count == -1.


然后你嘗試滾動到負位置,這是不允許的。


查看完整回答
反對 回復 2023-07-28
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

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


    private fun scrollToLastItem(view: View) {

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

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

    }

RCA:在 ScrollLayoutManager startSmoothPendingScroll 方法崩潰之前,當前位置為 -1。


查看完整回答
反對 回復 2023-07-28
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

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


private fun scrollToLastItem(view: View) {

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

        view.recycler_view.smoothScrollToPosition(it - 1)

    }

}


查看完整回答
反對 回復 2023-07-28
  • 3 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號