recyclerView适配器中的特定视图支架无法投射到视图支架

时间:2017-12-10 11:57:17

标签: android android-recyclerview adapter

我使用下面的代码,但它有一个错误: -

  

EventAdapter $ MainViewHolder无法强制转换为EventAdapter $ ProfileViewHolder

但每个viewHolder都从MainViewHolder扩展, 这段代码中的问题在哪里?

谢谢你们,我是堆栈溢出的新人!

public class NavDrawerAdapter extends RecyclerView.Adapter<NavDrawerAdapter.MainViewHolder> {
List<MainOption> mainOptionlist;
Context context;
private static final int TYPE_PROFILE = 1;
private static final int TYPE_OPTION_MENU = 2;


public NavDrawerAdapter(Context context){
    this.mainOptionlist = MainOption.getDrawableDataList();
    this.context = context;
}

@Override
public int getItemViewType(int position) {
    return (position == 0? TYPE_PROFILE : TYPE_OPTION_MENU);
}

@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType){}
}

@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
    if(holder.getItemViewType() == TYPE_PROFILE){
        ProfileViewHolder mholder = (ProfileViewHolder) holder;
    }
    else {
        MyViewHolder mHolder = (MyViewHolder) holder;
        MainOption mo = mainOptionlist.get(position);
        mHolder.tv_title.setText(mo.title);
    }
}

@Override
public int getItemCount() {
    return mainOptionlist.size();
}

public class MyViewHolder extends MainViewHolder{
    public MyViewHolder(View v){
        super(v);
    }
}
public class ProfileViewHolder extends MainViewHolder{
    public ProfileViewHolder(View v){
        super(v);
    }
}

public class MainViewHolder extends  RecyclerView.ViewHolder {
    public MainViewHolder(View v) {
        super(v);
    }
}

1 个答案:

答案 0 :(得分:0)

public class ChaptersAdapter扩展了BaseAdapter {

private static final int TYPE_CHAPTER = 0;
private static final int TYPE_PART = 1;

private Context mContext;
private ArrayList<Chapter> mChapters;
private LayoutInflater mInflater;
private ItemClickListener mItemClickListener;
private TreeSet<Integer> mSectionNumber = new TreeSet<Integer>();
private boolean mIsPaid;

public ChaptersAdapter(Context context, ArrayList<Chapter> chapters, ItemClickListener itemClickListener, boolean isPaid) {
    mContext = context;
    mChapters = chapters;
    mItemClickListener = itemClickListener;
    mInflater = LayoutInflater.from(context);
    mIsPaid = isPaid;
    saveSectionNum();
}

@Override
public int getCount() {
    int n = 0;
    for (int i = 0; i < mChapters.size(); i++) {
        n += mChapters.get(i).mParts.size();
    }
    n += mChapters.size();
    if (n < 0)
        return 0;
    return n;
}

private void saveSectionNum() {
    int n = 0;
    for (int i = 0; i < mChapters.size(); i++) {
        mSectionNumber.add(n);
        n += mChapters.get(i).mParts.size();
        n++;
    }
}

private ChapterPart getChapterPart(int position) {
    int n = 0;
    int chapterNum = 0;
    int partNum = -1;

    while ( n < position ) {
        n++;
        n += mChapters.get(chapterNum).mParts.size();
        chapterNum++;
    }

    if ( n > position ) {
        chapterNum--;
        n = n - mChapters.get(chapterNum).mParts.size();
        partNum = position - n;
    } else if ( n == position) {
        partNum = -1;
    }
    return new ChapterPart(chapterNum, partNum);
}

@Override
public int getItemViewType(int position) {
    return mSectionNumber.contains(position) ? TYPE_CHAPTER : TYPE_PART;
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public Object getItem(int position) {
    ChapterPart chapterPart = getChapterPart(position);
    if (-1 == chapterPart.mPartNum) {
        return mChapters.get(chapterPart.mChapterNum);
    }
    return mChapters.get(chapterPart.mChapterNum).mParts.get(chapterPart.mPartNum);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    int rowType = getItemViewType(position);

    if (convertView == null) {

        switch (rowType) {
            case TYPE_CHAPTER:
                convertView = mInflater.inflate(R.layout.listitem_chapter, null);
                ChapterTag tag1 = new ChapterTag(convertView);
                convertView.setTag(tag1);
                break;
            case TYPE_PART:
                convertView = mInflater.inflate(R.layout.listitem_part, null);
                PartTag tag2 = new PartTag(convertView);
                convertView.setTag(tag2);
                break;
        }
    }

    switch (rowType) {
        case TYPE_CHAPTER:
            ChapterTag tag1 = (ChapterTag)(convertView.getTag());
            Chapter chapter = (Chapter)getItem(position);
            tag1.setData(position, chapter);
            break;
        case TYPE_PART:
            PartTag tag2 = (PartTag)(convertView.getTag());
            Part part = (Part)getItem(position);
            tag2.setData(position, part);
            break;
    }
    return convertView;

}

class ChapterTag {
    View view;
    TextView txtChapter;

    Chapter chapter;

    public ChapterTag(final View view) {
        this.view = view;
        txtChapter = (TextView) view.findViewById(R.id.txt_chapter);
    }

    public void setData(int position, Chapter chapter) {
        this.chapter = chapter;
        txtChapter.setText(chapter.mName);
    }
}

class PartTag {
    View view;
    TextView txtPart;
    FancyButton btnVideo;
    FancyButton btnAudio;

    Part part;

    public PartTag(final View view) {
        this.view = view;
        txtPart = (TextView) view.findViewById(R.id.txt_part);

        btnVideo = (FancyButton) view.findViewById(R.id.btn_video);

        UiUtil.applyButtonEffect(btnVideo, new Runnable() {
            @Override
            public void run() {
                mItemClickListener.onVideoClick(part.mVideoURL);
            }
        });

        btnAudio = (FancyButton) view.findViewById(R.id.btn_audio);

        UiUtil.applyButtonEffect(btnAudio, new Runnable() {
            @Override
            public void run() {
                mItemClickListener.onAudioClick(part.mAudioURL);
            }
        });
    }

    public void setData(int position, Part part) {
        this.part = part;
        txtPart.setText(part.mName);
        if (part.mVideoURL.equals("")) {
            btnVideo.setEnabled(false);
        }
        if (part.mAudioURL.equals("")) {
            btnAudio.setEnabled(false);
        }
        ChapterPart chapterpart = getChapterPart(position);
        if (position > 3 + chapterpart.mChapterNum && mIsPaid == false) {
            btnVideo.setEnabled(false);
            btnAudio.setEnabled(false);
        }
    }
}

class ChapterPart {
    int mChapterNum;
    int mPartNum;

    ChapterPart(int chapterNum, int partNum) {
        mChapterNum = chapterNum;
        mPartNum = partNum;
    }
}

}