2个视图支架在Recycler适配器

时间:2018-06-03 06:37:47

标签: android android-recyclerview adapter android-viewholder

我是Android开发的新手。很抱歉,如果这是无用的问题。我RecyclerView想要在Firebase中显示来自RecyclerView的图片和视频。{/ p>

我有两个ViewHolder。我想要的是,适配器检查帖子“类型”。如果是  1)"type"=photo。我想夸大PhotoViewHolder  2)如果“type”= video我想要膨胀VideoHolder

我怎样才能做到这一点?。请帮帮我!

take a look at this pic

我已经谷歌搜索了几个小时,我得到了这个代码,它只会膨胀VideoHolder

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //findViewById...
    mContext = parent.getContext();
    switch (viewType) {
        case VIDEO_TYPE:
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_type, parent, false);
            return new VideoHolder(v);
        case IMAGE_TYPE:
            View i = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_list, parent, false);
            return new PhotoHolder(i);

    }
    return null;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
    mHolder = holder;
    photo = moviesList.get(position);
    //  final VideoHolder viewHolder2 = (VideoHolder)holder;
    int viewType = getItemViewType(position);
    switch ( viewType ) {
        case IMAGE_TYPE:
            PhotoHolder photoview = (PhotoHolder) holder;
            final ImageLoader imageLoader =  ImageLoader.getInstance();
            imageLoader.displayImage(getItem(position).getImage_path(),photoview.image);
            break;
        case VIDEO_TYPE:
            final VideoHolder viewHolder2 = (VideoHolder)holder;
            //viewHolder2.users = new StringBuilder();
            viewHolder2.bind(Uri.parse(photo.getImage_path()),moviesList);
            break;
    }


}

@Override
public int getItemCount() {
    return moviesList.size();
}
public Photo getItem(int position) {
    return moviesList.get(position);
}
@Override
public int getItemViewType ( int position ) {
    int viewType;
    if if (moviesList.get(position).getType().equals("Photo")) {
        viewType = IMAGE_TYPE;
    } else{
        viewType = VIDEO_TYPE;
    }
    return viewType;
}

1 个答案:

答案 0 :(得分:0)

将.equals()更改为.contains()

 @Override
public int getItemViewType ( int position ) {
    int viewType;
    if (moviesList.get(position).getType_post().contains("Photo")) {
        viewType = IMAGE_TYPE;
    } else{
        viewType = VIDEO_TYPE;
    }
    return viewType;
}