在某些国家/地区,我有一个由事件的sessios填充的recyclerView。我有一个微调器,用户可以在其中选择国家/地区,然后从api加载数据并将其显示在recyclerView中。当用户更改国家/地区时,将数据添加到recyclerView并刷新它是正常的。我想添加一个简单的代码,如果会话正在进行中,那么recyclerView必须滚动到会话的位置,但是当它如果我更改国家/地区会收到此错误消息
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{3f69195a position=3 id=-1, oldPos=-1, pLpos:-1 no parent} androidx.recyclerview.widget.RecyclerView{870f167 GFED.... ......ID 0,417-860,570 #7f0800a3 app:id/mEA_recyclerView}, adapter:is.com.raisse.companionfornite.EventSessionsRecyclerViewAdapter@1e4e5b14, layout:is.com.raisse.companionfornite.EventsActivity$1@c950fbd, context:is.com.raisse.companionfornite.EventsActivity@706582b
at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
at androidx.recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:286)
at androidx.recyclerview.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:343)
at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:359)
at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:366)
at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:397)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
滚动到活动现场,我已使用此代码
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false) {
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
Log.d("position===>", "" + mPositionEvent);
scrollRecycler();
}
});
private void scrollRecycler() {
try {
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(this) {
@Override
protected int getHorizontalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
smoothScroller.setTargetPosition(mPositionEvent);
recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
} catch (Exception ex) {
ex.printStackTrace();
}
}
变量mPositionEvent是静态的,我可以通过onBindViewHolder中的recyclerViewAdapter对其进行更改
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//...
//some code here works perfeclty
try {
Date dateStart = format.parse(eventTimeStart);
Date dateEnd = format.parse(eventTimeEnd);
Date dateCurrent = format.parse(currentTime);
long longStart = dateStart.getTime();
long longEnd = dateEnd.getTime();
long current = dateCurrent.getTime();
if (current>=longStart && current<longEnd){
holder.txtStatus.setText("Live");
holder.txtStatus.setTextColor(Color.RED);
holder.viewS.setBackground(ContextCompat.getDrawable(context,R.drawable.parallelogram_view_live));
EventsActivity.mPositionEvent = position;
}
if(current>=longEnd){
holder.txtStatus.setText("ENDED");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
当位置为0时我会注意到什么,所以没有活动直播我没有任何错误
答案 0 :(得分:0)
实际上,这是RecyclerView android中的错误。 Google尚未解决此问题,因此请尝试其他解决方案。 使用try-catch处理这种情况。扩展您的layoutManager作为包装管理器来处理崩溃。例如。考虑一下
class WrapperGridLayoutManager : GridLayoutManager {
constructor(context: Context?, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr,
defStyleRes
)
constructor(context: Context?, spanCount: Int) : super(context, spanCount)
constructor(context: Context?, spanCount: Int, orientation: Int, reverseLayout: Boolean) : super(
context,
spanCount,
orientation,
reverseLayout
)
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State) {
try {
super.onLayoutChildren(recycler, state)
} catch (e: Exception) {
e.printStackTrace()
}
}
}