我想在回收視圖中顯示不同的項目/佈局。所以這是我如何定義這些項目:RecyclerView的onBindViewHolder中的ClassCastException
public class SearchItem {
public static final int TYPE_DEFAULT = 0;
public static final int TYPE_LOADING = 1;
public int type;
public SearchItem(int type) {
this.type = type;
}
public static class DefaultItem extends SearchItem {
public String title;
public DefaultItem(String title) {
super(TYPE_DEFAULT);
this.title = title;
}
}
public static class LoadingItem extends SearchItem {
public String icon, title;
public LoadingItem(String icon, String title) {
super(TYPE_LOADING);
this.icon = icon;
this.title = title;
}
}
}
所以基本上我有兩種類型。在適配器的適當的佈局將選擇每個項目:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.BaseViewHolder>{
public ArrayList<SearchItem> items = new ArrayList<>();
@Override
public int getItemCount() {
return items.size();
}
@Override
public int getItemViewType(int position) {
return items.get(position).type;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case SearchItem.TYPE_LOADING:
ViewGroup vLoading = (ViewGroup) mInflater.inflate(R.layout.search_item_loading, parent, false);
return new LoadingViewHolder(vLoading);
case SearchItem.TYPE_DEFAULT:default:
ViewGroup vDefault = (ViewGroup) mInflater.inflate(R.layout.search_item_default, parent, false);
return new DefaultViewHolder(vDefault);
}
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
SearchItem item = items.get(position);
switch (holder.getItemViewType()) {
case SearchItem.TYPE_DEFAULT:
DefaultViewHolder defaultViewHolder = (DefaultViewHolder) holder;
defaultViewHolder.mTextViewTitle.setText(((SearchItem.DefaultItem)item).title);
case SearchItem.TYPE_LOADING:
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.mTextViewIcon.setText(((SearchItem.LoadingItem)item).icon);
loadingViewHolder.mTextViewTitle.setText(((SearchItem.LoadingItem)item).title);
}
}
/**
* Base view holder.
*/
public static class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(View view) {
super(view);
}
}
public static class DefaultViewHolder extends BaseViewHolder {
@Bind(R.id.mTextViewTitle)
TextView mTextViewTitle;
public DefaultViewHolder(View mView) {
super(mView);
ButterKnife.bind(this, mView);
}
}
public static class LoadingViewHolder extends BaseViewHolder {
@Bind(R.id.mTextViewIcon)
TextView mTextViewIcon;
@Bind(R.id.mTextViewTitle)
TextView mTextViewTitle;
public LoadingViewHolder(View mView) {
super(mView);
ButterKnife.bind(this, mView);
}
}
}
在onBindViewHolder
在
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
機器人工作室已經給了我鑄造holder
到LoadingViewHolder
的時候,我可能會得到一個異常的警告。運行項目時,我得到:
SearchAdapter$DefaultViewHolder cannot be cast to SearchAdapter$LoadingViewHolder
因此不知何故DefaultViewHolder被轉移到LoadingViewHolder,但它不應該。相反,BaseViewHolder應該被鑄造。你看到最新的錯誤?
'holder.getItemViewType()'似乎不存在。 – CommonsWare