我的应用程序包含带有搜索选项的滚动视图(在操作栏上),当滚动recyclelerview时没有任何关键字进行搜索而不会产生任何问题。但是当我在搜索视图中键入任何单词并尝试滚动recyclerview时,应用程序崩溃。
logcat错误: -
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 5(offset:5).state:46
Activity.java: -
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
try {
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (isSearch) {
isLastPage = false;
jobArrayList.clear();
}
if (!isLoading && !isLastPage) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= PAGE_SIZE) {
getDataFromWebServises("", start, "20", "scroll");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
void getDataFromWebServises(String search_key, String start, String count, final String type) {
if (!isNetworkAvailable()) {
textNWError.setVisibility(View.VISIBLE);
showSnackBar(getResources().getString(R.string.no_internet_connection));
} else {
textNWError.setVisibility(View.GONE);
final String url = "webservice url";
showProgressBar();
isLoading = true;
final JSONObject requestJsonObject = new JSONObject();
try {
requestJsonObject.put("user_id", UserSingleton.getInstance().getUser().getUser_id());
requestJsonObject.put("start", start);
requestJsonObject.put("count", count);
requestJsonObject.put("search_key", search_key);
} catch (JSONException e) {
e.printStackTrace();
}
CustomRequest jsonObjReq = new CustomRequest(Request.Method.POST, url, requestJsonObject,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hideProgressBar();
isLoading = false;
try {
Gson gson = new Gson();
Type listType = new TypeToken<List<job>>() {
}.getType();
if (type.equals("top"))
jobArrayList.clear();
ArrayList<job> jobs = gson.fromJson(response + "", listType);
if (jobs != null) {
jobArrayList.addAll(jobs);
if (jobs.size() >= PAGE_SIZE) {
} else {
isLastPage = true;
}
}
if (type.equals("scroll")) {
loadRecylcerview(jobArrayList);
} else if (type.equals("top")) {
setUpRecyclerView(jobArrayList);
String json = gson.toJson(jobArrayList);
pref.saveJobList(/*response.toString()*/json);
}
} catch (Exception e) {
// Log.e("execp", e + "");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error", error + "");
hideProgressBar();
isLoading = false;
showSnackBarNetworkError(0);
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.statusCode == 403) {
// HTTP Status Code: 403 Unauthorized
logout();
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-Token", UserSingleton.getInstance().getUser().getUser_token());
headers.put("User-Id", UserSingleton.getInstance().getUser().getUser_id());
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 10, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MyVolleySingleton.getInstance(this).addToRequestQueue(jsonObjReq, url);
}
}
private void loadRecylcerview(ArrayList<job> arrayList) {
start = String.valueOf(arrayList.size());
mAdapter.notifyDataSetChanged();
recyclerView.addOnScrollListener(recyclerViewOnScrollListener);
}
private void setUpRecyclerView(ArrayList<job> arrayList) {
if (arrayList.size() == 0) {
textEmpty.setVisibility(View.VISIBLE);
} else {
textEmpty.setVisibility(View.INVISIBLE);
}
start = String.valueOf(arrayList.size());
mAdapter = new JobAdapter(arrayList, this);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
// Pagination
if(!isSearch){
if (isNetworkAvailable())
recyclerView.addOnScrollListener(recyclerViewOnScrollListener);
}
}
任何人都可以帮我解决问题。???