我有一个带有一些数据的RecyclerView。当它滚动到最底部位置时,下面会显示一个进度条并插入其他一些项目。但是当它向上滚动多次时从最底部位置我得到< strong> IndexOutOfBounds 异常。任何人都可以找到问题所在?
代码:
RecyclerFrag.java:
public class Recycler_Frag extends Fragment {
public RecyclerView recyclerView;
Intent intent;
ArrayList arrayList;
RelativeLayout relativeLayout;
int previouslength;
int counter = 1;
ProgressBar progressBar;
boolean isScrolledup = false;
private View view;
private BroadcastReceiver broadcastReceiver;
private int progressstatus;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.recycler_lyt, container, false);
arrayList = new ArrayList<Integer>();
for (counter = 0; counter < 20; counter++) {
arrayList.add(counter);
}
previouslength = arrayList.size();
progressBar = view.findViewById(R.id.progress);
relativeLayout = view.findViewById(R.id.rel);
recyclerView = view.findViewById(R.id.recycler);
recyclerView.setAdapter(new RecyclerAdapter());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
try {
if (!recyclerView.canScrollVertically(1)) {
if (!isScrolledup) {
Log.e("U", "U");
isScrolledup = true;
new Thread(new Runnable() {
@Override
public void run() {
//Layout which has the progressbar
relativeLayout.post(new Runnable() {
@Override
public void run() {
relativeLayout.setVisibility(View.VISIBLE);
}
});
while (counter < 30) {
counter++;
arrayList.add(counter);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
relativeLayout.post(new Runnable() {
@Override
public void run() {
relativeLayout.setVisibility(View.GONE);
}
});
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.getAdapter().notifyItemRangeInserted(previouslength, arrayList.size() - previouslength);
Log.e("S", "S");
recyclerView.scrollToPosition(arrayList.size() - 1);
Log.e("G", "G");
}
});
}
}).start();
Toast.makeText(getContext(), "last", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
return view;
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder> {
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,
parent, false));
}
@Override
public void onBindViewHolder(RecyclerHolder holder, int position) {
holder.textView.setText(Integer.toString((Integer) arrayList.get(position)));
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class RecyclerHolder extends RecyclerView.ViewHolder {
TextView textView;
public RecyclerHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
}
}
}
}
例外:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.globemaster.samples, PID: 861
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{65444e5 position=30 id=-1, oldPos=20, pLpos:20 scrap [attachedScrap] tmpDetached no parent} android.support.v7.widget.RecyclerView{2c8745b VFED..... ......I. 0,0-720,1024 #7f08010a app:id/recycler}, adapter:com.globemaster.samples.Recycler_Frag$RecyclerAdapter@c2f74f8, layout:android.support.v7.widget.LinearLayoutManager@5a88dd1, context:com.globemaster.samples.MainActivity@ef647f9
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5421)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5603)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3644)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3408)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3175)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2759)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1528)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7530)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:622)
at android.view.Choreogra