Skip to content

Commit

Permalink
[Usage] Fix view recycling issue in the RecyclerView
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Nov 9, 2024
1 parent 4a50598 commit 24aaddc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public ListItemViewHolder(@NonNull View itemView) {

void setDefaultList(List<PackageUsageInfo> list) {
synchronized (mAdapterList) {
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
notifyItemChanged(0);
AdapterUtils.notifyDataSetChanged(this, 1, mAdapterList, list);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Looper;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -13,7 +14,8 @@
import java.util.List;

public final class AdapterUtils {
public static <T> void notifyDataSetChanged(@NonNull RecyclerView.Adapter<?> adapter, @NonNull List<T> baseList,
public static <T> void notifyDataSetChanged(@NonNull RecyclerView.Adapter<?> adapter,
@NonNull List<T> baseList,
@Nullable Collection<T> newList) {
int previousCount = baseList.size();
baseList.clear();
Expand All @@ -24,27 +26,48 @@ public static <T> void notifyDataSetChanged(@NonNull RecyclerView.Adapter<?> ada
notifyDataSetChanged(adapter, previousCount, currentCount);
}

public static <T> void notifyDataSetChanged(@NonNull RecyclerView.Adapter<?> adapter,
@IntRange(from = 0) int startIndex,
@NonNull List<T> baseList,
@Nullable Collection<T> newList) {
int previousCount = baseList.size();
baseList.clear();
if (newList != null) {
baseList.addAll(newList);
}
int currentCount = baseList.size();
notifyDataSetChanged(adapter, startIndex, previousCount, currentCount);
}

public static void notifyDataSetChanged(@NonNull RecyclerView.Adapter<?> adapter, int previousCount,
int currentCount) {
notifyDataSetChanged(adapter, 0, previousCount, currentCount);
}


public static void notifyDataSetChanged(@NonNull RecyclerView.Adapter<?> adapter,
@IntRange(from = 0) int startIndex,
@IntRange(from = 1) int previousCount,
@IntRange(from = 1) int currentCount) {
if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
// Main thread is required
throw new RuntimeException("Must be called on the UI thread");
}
if (previousCount > currentCount) {
// Some values are removed
if (currentCount > 0) {
adapter.notifyItemRangeChanged(0, currentCount);
adapter.notifyItemRangeChanged(startIndex, currentCount);
}
adapter.notifyItemRangeRemoved(currentCount, previousCount - currentCount);
adapter.notifyItemRangeRemoved(currentCount + startIndex, previousCount - currentCount);
} else if (previousCount < currentCount) {
// Some values are added
if (previousCount > 0) {
adapter.notifyItemRangeChanged(0, previousCount);
adapter.notifyItemRangeChanged(startIndex, previousCount);
}
adapter.notifyItemRangeInserted(previousCount, currentCount - previousCount);
adapter.notifyItemRangeInserted(previousCount + startIndex, currentCount - previousCount);
} else if (previousCount > 0) {
// No values are added or removed
adapter.notifyItemRangeChanged(0, previousCount);
adapter.notifyItemRangeChanged(startIndex, previousCount);
}
}

Expand Down

0 comments on commit 24aaddc

Please sign in to comment.