我正在尝试创建一个将显示TextView的简单RV。 这是我的适配器:
public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
public MyRvAdapter(String[] myDataset) {
mDataset = myDataset;
}
@NonNull
@Override
public MyRvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.text_row_item, parent, false);
TextView userNameInList= v.findViewById(R.id.display_name);
ViewHolder vh = new ViewHolder(userNameInList);
return vh;
}
@Override
public void onBindViewHolder(@NonNull MyRvAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
text_row_item只是一个内部带有TextView(“ display_name”)的FrameLayout。 这是错误的:
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)
非常感谢您!
答案 0 :(得分:13)
实际上,您的ViewHolder
期望View
膨胀为R.layout.text_row_item
,而不是后者的后代。因此,如果您通过夸张的视图,该问题将得到解决。
因此,您应该将代码更正为:
public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = v.findViewById(r.id.display_name);
}
}
public MyRvAdapter(String[] myDataset) {
mDataset = myDataset;
}
@NonNull
@Override
public MyRvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.text_row_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(@NonNull MyRvAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
答案 1 :(得分:2)
我知道为时已晚,但是我想补充一下我的答案,因为它可以帮助任何由于上述原因而面临此问题的人。
在我的案例中,我使用的是“数据绑定库”,而我的错误是我的根元素不是<layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<!-- UI layout's root element -->
<ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- By Mistake i was Making this element as Root -->
</FrameLayout>
</ConstraintLayout>
</layout>
答案 2 :(得分:1)
等到绑定之前创建视图。
从您的TextView userNameInList= v.findViewById(R.id.display_name);
中删除MyRvAdapter.ViewHolder
。
然后将ViewHolder
绑定为mTextView = v.findViewById(r.id.display_name);
。
答案 3 :(得分:1)
对于我来说,我已更改此行对其进行了修复
val view = parent.inflate(R.layout.your_item)
到
val view = parent.inflate(R.layout.your_item, false)
答案 4 :(得分:-1)
尝试
public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.MyViewHolder> {
private Context mContext;
private String[] mDataset;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.display_name);
}
}
public MyRvAdapter(Context mContext, String[] myDataset) {
this.mContext = mContext;
this.mDataset = myDataset;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_row_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.title.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}