java RecyclerView在从API(OkHttp)刷新时无法滚动,导致IndexOutOfBoundsException崩溃
尝试刷新数据后滚动时,My RecyclerView因IndexOutOfBoundsException崩溃
所需功能:API请求成功填充RecyclerView一次后,我希望刷新RecyclerView,并能够在刷新时上下滚动
当前功能:如果在刷新数据时不滚动,应用程序不会崩溃。如果我在发出刷新请求后滚动,它会因IndexOutOfBoundsException崩溃
我花了几个星期的时间试图解决这个问题,但没有提出任何问题,我相信我已经尝试了足够多的潜在解决方案来证明向Stack Overflow寻求指导是合理的。这里有无数关于同一主题的问题,但不幸的是,没有一个能解决我的问题。提前感谢您的考虑
以下是其他人提出的一些解决方案:
使用适配器。notifyDataSetChanged(),但我理解这一点 在Android文档中被视为“最后手段”
到呼叫列表。在适配器之前清除。notifyDataSetChanged()
使用适配器将数据集中所有当前项的位置设置为一个称为“位置”的整数。getItemCount(),然后将其传递给适配器。NotifyItemRange已更改(位置)
设置适配器。setHasStableIds(真)
打电话给mRecyclerView。getRecycledViewPool()。clear()和mAdapter。notifyDataSetChanged()
显然,如果RecyclerView在LinearLayout中,“notify”方法不起作用(这可能与Android中的一个旧bug有关,现在可能已经修复,但我不确定)
所有这些建议都会导致“致命异常”
我的应用程序使用五个文件:
- 作业适配器(适配器)
- JobsListItem(getter和setter)
- 乔布苏特(片段)
- jobs\u recyclerview
- 作业列表项
我只包含了适配器和片段的代码,因为我确信布局文件以及getter和setter格式良好
片段:
public class JobsOut extends Fragment {
String jobId;
String jobTitle;
String jobNumber;
String jobStartTime;
String dispatchType;
@BindView(R.id.jobsOutRecyclerView) RecyclerView jobsOutRecyclerView;
@BindView(R.id.fab) FloatingActionButton refreshFab;
private List<JobsListItem> dispatch;
private RecyclerView.Adapter mJobsOutAdapter;
public RecyclerView.LayoutManager dispatchLayoutManager;
OkHttpClient client = new OkHttpClient();
Handler handler = new Handler();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.recycler_test, container, false);
ButterKnife.bind(this, rootView);
dispatch = new ArrayList<>();
jobsOutRecyclerView.setHasFixedSize(true);
dispatchLayoutManager = new LinearLayoutManager(getContext());
jobsOutRecyclerView.setLayoutManager(dispatchLayoutManager);
downloadDispatch();
refreshFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadDispatch();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
dispatch.clear();
}
});
}
});
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(this);
}
private void downloadDispatch() {
final okhttp3.Request request = new okhttp3.Request.Builder()
.url("url")
.header("X_SUBDOMAIN", "SUBDOMAIN")
.header("X-AUTH-TOKEN", "API_KEY")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
try {
String jsonData = response.body().string();
JSONObject getRootObject = new JSONObject(jsonData);
JSONObject metaObject = getRootObject.getJSONObject("meta");
final String row_count = metaObject.getString("total_row_count");
{
if (row_count.equals("0")) {
// do something for no jobs
} else {
JSONObject getArray = new JSONObject(jsonData);
JSONArray opportunitiesArray = getArray.getJSONArray("opportunities");
for (int i = 0; i < opportunitiesArray.length(); i++) {
JSONObject opportunity = opportunitiesArray.getJSONObject(i);
jobId = opportunity.getString("id");
jobTitle = opportunity.getString("subject");
jobNumber = opportunity.getString("number");
jobStartTime = opportunity.getString("starts_at");
dispatchType = opportunity.getString("customer_collecting");
// Take Strings from response and send them to JobsListItem
final JobsListItem item = new JobsListItem(jobId, jobTitle, jobNumber, jobStartTime, dispatchType);
// If the adapter hasn't been created, do this
if (mJobsOutAdapter == null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mJobsOutAdapter = new JobsAdapter(dispatch, getContext());
jobsOutRecyclerView.setAdapter(mJobsOutAdapter);
dispatch.add(item);
}
});
}
// If the adapter has been created, just do this
else if (mJobsOutAdapter != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
dispatch.add(item);
mJobsOutAdapter.notifyDataSetChanged();
}
});
}
}
}
}
} catch (IOException e) {
Log.e("TAG", "IO exception caught: ", e);
} catch (JSONException e) {
Log.e("TAG", "TAG exception caught: ", e);
}
}
});
}
适配器:
public class JobsAdapter extends RecyclerView.Adapter<JobsAdapter.ViewHolder> {
private List<JobsListItem> mJobsListItem;
private Context context;
public JobsAdapter(List<JobsListItem> mJobsListItem, Context context) {
this.mJobsListItem = mJobsListItem;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.jobs_listitem, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final JobsListItem mJobsListItemViewHolder = this.mJobsListItem.get(position);
// holders go here and do things with text and what-not
}
@Override
public int getItemCount() {
return mJobsListItem.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// BindView's with ButterKnife go here and all that jazz
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
来自崩溃的日志猫:
26404-26404 E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.plasmacat.techmate, PID: 26404
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 4(offset:4).state:16
at 安卓.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5504)
at 安卓.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at 安卓.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at 安卓.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at 安卓.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at 安卓.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at 安卓.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1325)
at 安卓.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1061)
at 安卓.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1695)
at 安卓.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2883)
at 安卓.view.View.dispatchTouchEvent(View.java:10063)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2630)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2636)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
at com.安卓.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
at com.安卓.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1819)
at 安卓.app.Activity.dispatchTouchEvent(Activity.java:3127)
at 安卓.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
at 安卓.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
at com.安卓.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
at 安卓.view.View.dispatchPointerEvent(View.java:10283)
at 安卓.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4522)
at 安卓.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4353)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3893)
at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3946)
at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3912)
at 安卓.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4039)
at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3920)
at 安卓.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4096)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3893)
at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3946)
at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3912)
at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3920)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3893)
at 安卓.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6341)
at 安卓.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6315)
at 安卓.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6265)
at
安卓.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6444)
at 安卓.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at 安卓.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at 安卓.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at 安卓.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6415)
at 安卓.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6467)
at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at 安卓.view.Choreographer.doCallbacks(Choreographer.java:686)
at 安卓.view.Choreographer.doFrame(Choreographer.java:615)
at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)
如果你有时间,我将非常感谢你的帮助
谢谢大家!
# 1 楼答案
虽然这里有很多好的建议。一个想法是,当用户与调用额外加载的按钮交互时,停止回收器滚动
# 2 楼答案
您试图做的是相当常见的,当recycler视图需要向其适配器请求数据(因为它已滚动)并且它所需的位置在Adatper中不存在时,您的索引会发生越界。例如:适配器试图获取项目编号“N”,数据包含N-1(或更少)
大多数情况下,这是由多种因素造成的:
穿线。这一切都应该在UI线程(通知等等)上处理(大部分情况下)。网络请求显然是在后台线程中发生的,我认为最终
onResponse
会回到主线程上(否则会出现其他异常)。再次检查我的测试Looper.getMainLooper() == Looper.myLooper()
(或类似)你在主线程上做了很多(不必要的)工作。您收到来自网络的响应,解析JSON并在主线程中创建对象……为什么不卸载所有工作,一旦有了项目列表,就将其传递到适配器上
你每次都在低效地调用
notifyDataSetChanged()
(这很糟糕)。为什么不使用(包含在Android中)DiffUtil
类来仅通知更改的范围?请允许我为您指出一个很好的示例,说明它是如何工作的:https://guides.codepath.com/android/using-the-recyclerview#diffing-larger-changes实现这些更改大约需要30分钟,这将使您的代码更加健壮
如果您使用RXJava使其成为流,则可获得额外积分:-)
注意:您应该创建一次适配器,然后每次有新数据时只需调用
setItems(your_list_of_items)
。DiffUtil和适配器应该知道如何处理这个问题。在你的活动/片段/网络代码中有很多不属于那里的“业务逻辑”。“onResponse”方法应该做的就是准备数据并将其传递给负责管理数据的类(适配器)。当我看到这个时,我皱起眉头。为什么这段代码要在这里创建适配器?谁来测试这个?如果你用别的东西来改变OKHttp呢?(为什么不使用改装并使其更容易?)我的意思是,你可以做很多事情来让你的程序员生活更轻松,你没有利用你可以利用的解决方案