Все было хорошо, пока я не обновил версию Sdk до 27. Здесь говорится, что представления ViewHolder не должны быть присоединены при создании.Но параметр attachToRoot в моем LayoutInflater уже имеет значение true.Пожалуйста, объясните мне, что могло вызвать эту проблему? Logcat :
06-02 11:40:15.669 22801-22801/com.bestworldgames.bestwordgame E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bestworldgames.bestwordgame, PID: 22801
java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6687)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
Вот мой Адаптер RecyclerView :
public class GameTableAdapter extends RecyclerView.Adapter<GameTableAdapter.ViewHolder> {
private ArrayList<Litera> literas;
private LayoutInflater mInflater;
private Map<Integer, Integer> hashMap;
private Context context;
private ArrayList<Button> adapterItems;
private View mainView;
private int itemSize;
public GameTableAdapter(Context context, ArrayList<Litera> literas, Map<Integer, Integer> hashMap, int itemSize) {
this.mInflater = LayoutInflater.from(context);
this.literas = literas;
this.hashMap = hashMap;
this.context = context;
this.adapterItems = new ArrayList<>();
this.itemSize = itemSize;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.but_item, parent, false);
parent.removeView(view);
parent.addView(view);
mainView = parent;
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
char mChar = literas.get(position).getCharValue();
holder.item.setText(String.valueOf(mChar));
if (this.hashMap.containsKey(position) && (hashMap.get(position)!=null)) {
setImg(hashMap.get(position), holder.item);
holder.item.setTextColor(context.getResources().getColor(R.color.colorWhite));
}
}
@Override
public int getItemCount() {
return literas.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
Button item;
ViewHolder(View itemView) {
super(itemView);
item = (Button) itemView.findViewById(R.id.item);
item.setLayoutParams(new RecyclerView.LayoutParams(itemSize, itemSize));
item.setTextSize(TypedValue.COMPLEX_UNIT_PX,(float)itemSize/2);
adapterItems.add(item);
}
}
public Litera getItem(int id) {
return literas.get(id);
}
public Button getButItem(int id) {
return adapterItems.get(id);
}
public View getMainView() {
return mainView;
}
public void animateItem(int id) {
adapterItems.get(id).setVisibility(View.INVISIBLE);
adapterItems.get(id).setVisibility(View.VISIBLE);
}
}
в Фрагмент :
recyclerView = (TouchableRecyclerView) rootView.findViewById(R.id.rv_chars);
...
public void setUpAdapter() {
numberOfColumns = col;
int itemSize = screenWidth / numberOfColumns;
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), numberOfColumns));
adapter = new GameTableAdapter(getActivity(), literas, hashMapAllColors, itemSize);
recyclerView.setAdapter(adapter);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setOnTouchListener(this);
}
TouchableRecyclerView.java
public class TouchableRecyclerView extends RecyclerView {
public TouchableRecyclerView(Context context) {
super(context);
}
public TouchableRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TouchableRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean performClick() {
return super.performClick();
}
}
в xml :
<com.bestworldgames.bestwordgame.widgets.TouchableRecyclerView
android:id="@+id/rv_chars"
android:layout_below="@+id/tapped_word"
android:paddingLeft="5dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:clickable="true"
android:focusable="true"
android:longClickable = "false">
</com.bestworldgames.bestwordgame.widgets.TouchableRecyclerView>
but_item.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:paddingRight="6dp"
android:paddingBottom="8dp"
android:animateLayoutChanges="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/button_color_1"
android:longClickable="false"
android:clickable="false"
android:focusable="false">
</Button>
Эта ошибка возникает после рабочего конструктора public ViewHolder(View itemView)
и перед методом onBindViewHolder(ViewHolder holder, final int position)
.Это все, что я могу сказать на данный момент.