我正在尝试将CustomListView
替换为RecyclerView
,但我正面临一个问题IndexOutOfBoundsException: Inconsistency detected
。
我使用应用程序服务,通过从服务器获取数据,每10秒更新/更改整个列表项。但是我的RecyclerView每次都会崩溃,当服务请求,更新内容和同时滚动时。
我知道这个问题已被提出,但我仍然没有得到解决问题的正确答案。所以在我回到CustomListView
之前,我决定再次请求你们帮忙。
我已经在this和this帖子上尝试了几乎每一个答案,但仍然没有成功。
这是我的适配器:
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.MyViewHolder> {
private List<EventModel> eventList = new ArrayList<>();
private Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView timeView, titleView;
public MyViewHolder(View view) {
super(view);
titleView = (TextView) view.findViewById(R.id.txtListTitle);
timeView = (TextView) view.findViewById(R.id.txtListTime);
}
}
public EventAdapter(Context context, List<EventModel> eventList) {
this.eventList = eventList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_event_form, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
EventModel event = eventList.get(position);
holder.titleView.setText(event.getTitle());
holder.timeView.setText(event.getEndTime());
}
/*public void update(ArrayList<EventModel> list) {
if (eventList != null) {
eventList.clear();
eventList.addAll(0,list);
}
else {
eventList = list;
}
notifyDataSetChanged(); // this does not work
}*/
@Override
public int getItemCount() {
return eventList.size();
}
}
这是我使用RecyclerView的地方:
public class EventListFragment extends Fragment {
private View view;
private RecyclerView recyclerView;
private LinearLayoutManager mLayoutManager;
private EventAdapter eventAdapter;
private ArrayList<EventModel> eventList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_event_list, container, false);
return view;
}
@Override
public void onResume() {
super.onResume();
setView();
setControl();
}
private void setView() {
recyclerView = (RecyclerView) view.findViewById(R.id.frg_recycler_view);
}
private void setControl() {
if (eventAdapter == null && mLayoutManager == null) {
eventList = new ArrayList<>();
eventAdapter = new EventAdapter(getActivity().getApplicationContext(), eventList);
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(eventAdapter);
}
public void eventToList() { // this will be called by service every 10 secs
final RequestData requestData = new RequestData(getActivity());
eventList.clear(); // clean data, before inserting new data
requestData.getEventToday(new RequestData.VolleyCallback() {
@Override
public void onSuccess(JSONObject result) {
for (int i = 0; i < result.length(); i++) {
try {
JSONObject item = result.getJSONObject(Integer.toString(i));
EventModel eventModel = new EventModel();
String title = item.getString("title");
String date = item.getString("date");
eventModel.setTitle(title);
eventModel.setDate(date);
eventList.add(eventModel);
} catch (JSONException e) {
Log.d(TAG, "error" + e.toString());
e.printStackTrace();
}
}
// Log.d(TAG, "onSuccess: Update Currentdate");
eventAdapter.notifyDataSetChanged();
}
});
}
}
这就是我所拥有的:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.longluong.mybbs, PID: 10469
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{1b43c6c position=10 id=-1, oldPos=-1, pLpos:-1 no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5249)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5431)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5392)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5388)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2149)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1533)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1496)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1313)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1058)
at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1680)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2837)
at android.view.View.dispatchTouchEvent(View.java:9302)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2241)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2805)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9522)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInput