问题
FATAL EXCEPTION: main Process: com.zipato.android.client.v2, PID: 24966 java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:true at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5736) at android.support.v7.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:5680) at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:289) at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:336) at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:349) at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:356) at android.support.v7.widget.GapWorker.run(GapWorker.java:387) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
App keeps crashing when I scroll down..
This is what my adapter looks like:
public class WalletAdapter extends SectioningAdapter {
static final boolean USE_DEBUG_APPEARANCE = false;
private List<Transaction> transactions;
private List<Section> sections = new ArrayList<>();
public WalletAdapter() {
}
private class Section {
String alpha;
List<Transaction> transactions = new ArrayList<>();
}
public List<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
sections.clear();
String mDate = "31.12.2222";
Section currentSection = null;
for (Transaction transaction : transactions) {
String date = parseDate(transaction.getCreatedDate());
if (!date.equals(mDate)) {
if (currentSection != null) {
sections.add(currentSection);
}
currentSection = new Section();
mDate = date;
currentSection.alpha = String.valueOf(mDate);
}
if (currentSection != null) {
currentSection.transactions.add(transaction);
}
}
sections.add(currentSection);
notifyAllSectionsDataSetChanged();
}
private String parseDate(Date date) {
DateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
String formattedDate = "";
formattedDate = df.format(date);
return formattedDate;
}
@Override
public int getNumberOfSections() {
return sections.size();
}
@Override
public boolean doesSectionHaveHeader(int sectionIndex) {
return sectionIndex == 0 || !parseDate(transactions.get(sectionIndex).getCreatedDate()).equals(parseDate(transactions.get(sectionIndex - 1).getCreatedDate()));
}
@Override
public boolean doesSectionHaveFooter(int sectionIndex) {
return false;
}
@Override
public int getNumberOfItemsInSection(int sectionIndex) {
return sections.get(sectionIndex).transactions.size();
}
@Override
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemUserType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.wallet_item, parent, false);
return new ItemViewHolder(v);
}
@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerUserType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.wallet_header, parent, false);
return new HeaderViewHolder(v);
}
@Override
public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
super.onBindItemViewHolder(viewHolder, sectionIndex, itemIndex, itemType);
Section section = sections.get(sectionIndex);
ItemViewHolder holder = (ItemViewHolder) viewHolder;
Transaction transaction = section.transactions.get(itemIndex);
holder.description.setText(transaction.getDescription());
holder.ammount.setText(String.valueOf(transaction.getAmount()));
holder.time.setText(parseDate(transaction.getCreatedDate()));
holder.total.setText(String.valueOf(transaction.getNewCredits()));
}
@Override
public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) {
super.onBindHeaderViewHolder(viewHolder, sectionIndex, headerType);
Section s = sections.get(sectionIndex);
HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;
if (USE_DEBUG_APPEARANCE) {
hvh.itemView.setBackgroundColor(0x55ffffff);
hvh.dateHeader.setText(pad(sectionIndex * 2) + s.alpha);
} else {
hvh.dateHeader.setText(s.alpha);
}
}
private String pad(int spaces) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < spaces; i++) {
b.append(' ');
}
return b.toString();
}
public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder {
TextView dateHeader;
public HeaderViewHolder(View itemView) {
super(itemView);
dateHeader = (TextView) itemView.findViewById(R.id.date);
}
}
public class ItemViewHolder extends SectioningAdapter.ItemViewHolder {
TextView description;
TextView time;
TextView ammount;
TextView total;
public ItemViewHolder(View itemView) {
super(itemView);
description = (TextView) itemView.findViewById(R.id.description);
time = (TextView) itemView.findViewById(R.id.time);
ammount = (TextView) itemView.findViewById(R.id.ammount);
total = (TextView) itemView.findViewById(R.id.new_credits);
}
}
}
This is my activity's xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zipato.appv2.activities.WalletActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And the code inside activity onCreate:
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
回答1:
Had the same problem, didn't find what causes it though... seems like a bug in a support lib. You can try disabling item prefetch for layout manager:
layoutManager.setItemPrefetchEnabled(false);
Solved the issue for me!
回答2:
overwrite getItemId
method in your adapter, like below
@Override
public long getItemId(int position) {
return position;
}
来源:https://stackoverflow.com/questions/43932210/java-lang-illegalargumentexception-scrapped-or-attached-views-may-not-be-recycl