我想在回收者视图中显示不同的项目/布局。所以这就是我定义这些项目的方式:

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;


android studio已经给我警告,将holder转换为LoadingViewHolder时可能会出现异常。运行项目时,我得到:

SearchAdapter$DefaultViewHolder cannot be cast to SearchAdapter$LoadingViewHolder


因此,以某种方式将DefaultViewHolder强制转换为LoadingViewHolder,但不应这样做。相反,应该强制转换BaseViewHolder。你看到错了吗?

最佳答案

根据您的要求和要达到的目的,您编写的代码是错误的。如我所见,您正在扩展的实现不是通用RecyclerView Holder,即它将基础视图持有人作为单个持有人

extends RecyclerView.Adapter<RecyclerView.ViewHolder>


而不是

extends RecyclerView.Adapter<SearchAdapter.BaseViewHolder>


这里的整个代码该怎么做



https://gist.github.com/umesh0492/e65d5195d8962b1a3049

10-05 17:44