以下是onCreateViewHolder()
方法的代码。我在这里检查列表是否为空。
我有searchView
来搜索联系人。如果没有包含搜索名称的联系人,则会显示“no_contacts
”布局。
但是当我按退格键删除搜索字符串时,它会抛出异常:
java.lang.IndexOutOfBoundsException:检测到不一致。无效 view holder adapter positionViewHolder {528a9274 position = 12 id = -1, oldPos = 0,pLpos:0 scrap [attachedScrap] tmpDetached no parent}
请帮我解决这个问题。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch(viewType) {
case VIEW_TYPE_EMPTY_LIST_PLACEHOLDER:
// return view holder for your placeholder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_contacts, parent, false);
EmptyViewHolder evh = new EmptyViewHolder(v);
return evh;
case VIEW_TYPE_OBJECT_VIEW:
// return view holder for your normal list item
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
return null;
}
这是完整的代码
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
private static final int VIEW_TYPE_OBJECT_VIEW = 1;
List<MyModel> myModels = new ArrayList<>();
Context context;
public MyAdapter(Context context, List<MyModel> mModels) {
myModels = new ArrayList<>(mModels);
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch(viewType) {
case VIEW_TYPE_EMPTY_LIST_PLACEHOLDER:
// return view holder for your placeholder
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_contacts, parent, false);
EmptyViewHolder evh = new EmptyViewHolder(v);
return evh;
case VIEW_TYPE_OBJECT_VIEW:
// return view holder for your normal list item
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_row, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
try {
if (holder instanceof ViewHolder) {
ViewHolder vh = (ViewHolder) holder;
MyModel model = myModels.get(position);
vh.bind(model);
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return myModels.size() > 0 ? myModels.size() : 1;
}
@Override
public int getItemViewType(int position) {
if (myModels.isEmpty()) {
return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
} else {
return VIEW_TYPE_OBJECT_VIEW;
}
}
public void animateTo(List<MyModel> models) {
applyAndAnimateRemovals(models);
applyAndAnimateAdditions(models);
applyAndAnimateMovedItems(models);
}
private void applyAndAnimateRemovals(List<MyModel> newModels) {
for (int i = myModels.size() - 1; i >= 0; i--) {
final MyModel model = myModels.get(i);
if (!newModels.contains(model))
removeItem(i);
}
}
private void applyAndAnimateAdditions(List<MyModel> newModels) {
for (int i = 0, count = newModels.size(); i < count; i++) {
final MyModel model = newModels.get(i);
if (!myModels.contains(model)) {
addItem(i, model);
}
}
}
private void applyAndAnimateMovedItems(List<MyModel> newModels) {
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
final MyModel model = newModels.get(toPosition);
final int fromPosition = myModels.indexOf(model);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public MyModel removeItem(int position) {
final MyModel model = myModels.remove(position);
notifyItemRemoved(position);
return model;
}
public void addItem(int position, MyModel model) {
myModels.add(position, model);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final MyModel model = myModels.remove(fromPosition);
myModels.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
public class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvPhoneNumber;
ImageView ivImage;
public ViewHolder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tv_name);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.phone_number);
ivImage = (ImageView) itemView.findViewById(R.id.ivPic);
}
public void bind(MyModel model) {
if (myModels.size() > 0) {
Log.e("Name ", model.getName());
tvName.setText(model.getName());
tvPhoneNumber.setText(model.getPhoneNumber());
Log.e("Image getImagePath()", model.getImagePath());
Log.e("Image URI()", "" + Uri.parse(model.getImagePath()));
if (model.getImagePath().startsWith("/")) {
ivImage.setImageURI(Uri.parse(model.getImagePath()));
} else {
ivImage.setImageResource(R.drawable.default_contact_pic);
}
} else {
}
}
}