From 1284b7c64e3354a84dd5a7277b700b37f80dbb57 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 15 Jul 2017 15:43:02 +0300 Subject: [PATCH] Fixes for inconsistency detected bug --- .../activities/board/BoardActivity.java | 3 -- .../activities/topic/TopicActivity.java | 18 ++++++++- .../mthmmy/activities/topic/TopicAdapter.java | 6 ++- .../utils/CustomLinearLayoutManager.java | 38 +++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/CustomLinearLayoutManager.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java index d25a2340..565b587a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java @@ -303,8 +303,5 @@ public class BoardActivity extends BaseActivity implements BoardAdapter.OnLoadMo boardAdapter.notifyDataSetChanged(); isLoadingMore = false; } - - } - } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index ba559876..8cb77d90 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -10,7 +10,6 @@ import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AlertDialog; -import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.text.Html; @@ -49,6 +48,7 @@ import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyPage; +import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; import gr.thmmy.mthmmy.utils.ParseHelpers; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.MultipartBody; @@ -168,7 +168,17 @@ public class TopicActivity extends BaseActivity { recyclerView = (RecyclerView) findViewById(R.id.topic_recycler_view); recyclerView.setHasFixedSize(true); - LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); + recyclerView.setOnTouchListener( + new View.OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + return topicTask != null && topicTask.getStatus() == AsyncTask.Status.RUNNING; + } + } + ); + //LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); + CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager( + getApplicationContext(), loadedPageUrl); recyclerView.setLayoutManager(layoutManager); topicAdapter = new TopicAdapter(this, postsList, topicTask); recyclerView.setAdapter(topicAdapter); @@ -551,6 +561,7 @@ public class TopicActivity extends BaseActivity { } progressBar.setVisibility(ProgressBar.INVISIBLE); + recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug if (replyPageUrl == null) { replyFAB.hide(); topicAdapter.customNotifyDataSetChanged(new TopicTask(), false); @@ -636,6 +647,9 @@ public class TopicActivity extends BaseActivity { } postsList.clear(); + int oldSize = postsList.size(); + topicAdapter.notifyItemRangeRemoved(0, oldSize); + recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug postsList.addAll(TopicParser.parseTopic(topic, language)); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 4a5f5798..b37ec5e4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -172,7 +172,8 @@ class TopicAdapter extends RecyclerView.Adapter { @SuppressLint({"SetJavaScriptEnabled", "SetTextI18n"}) @Override - public void onBindViewHolder(final RecyclerView.ViewHolder currentHolder, final int position) { + public void onBindViewHolder(final RecyclerView.ViewHolder currentHolder, + final int position) { if (currentHolder instanceof PostViewHolder) { final Post currentPost = postsList.get(position); final PostViewHolder holder = (PostViewHolder) currentHolder; @@ -478,7 +479,8 @@ class TopicAdapter extends RecyclerView.Adapter { //Initializes properties, array's values will be false by default viewProperties.add(new boolean[3]); } - notifyDataSetChanged(); + notifyItemRangeInserted(0, postsList.size()); + //notifyDataSetChanged(); } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/CustomLinearLayoutManager.java b/app/src/main/java/gr/thmmy/mthmmy/utils/CustomLinearLayoutManager.java new file mode 100644 index 00000000..cc03225c --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/CustomLinearLayoutManager.java @@ -0,0 +1,38 @@ +package gr.thmmy.mthmmy.utils; + +import android.content.Context; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.Log; + +import timber.log.Timber; + +public class CustomLinearLayoutManager extends LinearLayoutManager { + private String pageUrl; + + public CustomLinearLayoutManager(Context context, String pageUrl) { + super(context); + this.pageUrl = pageUrl; + } + + @Override + public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { + try { + super.onLayoutChildren(recycler, state); + } catch (IndexOutOfBoundsException e) { + Timber.wtf(e, "Inconsistency detected: topic_requested = \"" + pageUrl + "\""); + Log.d("CustomLinearLayoutMan", "Inconsistency detected: topic_requested = \"" + + pageUrl + "\"", e); + } + } + + /** + * Disable predictive animations. There is a bug in RecyclerView which causes views that + * are being reloaded to pull invalid ViewHolders from the internal recycler stack if the + * adapter size has decreased since the ViewHolder was recycled. + */ + @Override + public boolean supportsPredictiveItemAnimations() { + return false; + } +} \ No newline at end of file