使用Android中的Firebase在单个RecyclerView中获取两个不同的视图

前端之家收集整理的这篇文章主要介绍了使用Android中的Firebase在单个RecyclerView中获取两个不同的视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开发了一个应用程序,其中有两个帖子

>使用毕加索的图片文章
>使用Youtube Api的视频帖子.

我已经使用RecyclerView和Firebase recyclerAdapter实现了这两个问题,但问题是我不知道如何区分两个视图,如果我使用Youtube Api使用视频视图它附加了图像视图并且还复制到recyclerView中的所有项目,然后所有项目中只播放一个视频.我想要的是,如果我想要显示图像,那么该项目只能获取图像,如果我想显示视频,那么reyclerView项目只能获得视频而不重复.喜欢Instagram.我搜索了很多论坛,但没有得到任何帮助.请帮忙.我使用Firebase作为我的数据库,ViewHolders就是根据这个.

图片和视频帖子的类文件

FirebaseRecyclerAdapter <post,postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post,postViewHolder>(
    post.class,R.layout.post_row_recycle_home,postViewHolder.class,mDatabaseReference
) {
    @Override
    protected void populateViewHolder(postViewHolder viewHolder,post model,int position) {
        viewHolder.setimage(getApplicationContext(),model.getImage());
        viewHolder.setYoutube(model.getYoutube()); 
    }
};

mrecyclerView.setAdapter(firebaseRecyclerAdapter);

持有人:

public static class postViewHolder extends RecyclerViewPager.ViewHolder{

    View mView;

    public postViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setYoutube(final String youtube){
        final YouTubePlayerView youPlay = (YouTubePlayerView) mView.findViewById(R.id.youtuber);
        youPlay.initialize("SOME KEY",new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer youTubePlayer,boolean b) {

                    youTubePlayer.cueVideo(youtube);
                }
                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider,YouTubeInitializationResult youTubeInitializationResult) {

            }
        });
    }

    public void setimage(final Context ctx,final String image){
        final ImageView post_image = (ImageView)mView.findViewById(R.id.post_image);

         Picasso.with(ctx).load(image)
            .networkPolicy(NetworkPolicy.OFFLINE).into(post_image,new Callback() {

            @Override
            public void onSuccess() {
            }
            @Override
            public void onError() {
                Picasso.with(ctx).load(image).into(post_image);
            }
        });
    }
}

截屏示例:

解决方法

您可以决定使用适配器getItemViewType方法显示哪种类型的视图.在firebaseRecyclerAdapter里面添加以下内容
private static final int TYPE_YOUTUBE = 1111;
private static final int TYPE_IMAGE = 2222;

@Override
public int getItemViewType(int position) {
    //note: classes should start with uppercase
    Post model = getItem(position);
    if (model.isYoutube()) { //note: you'll have to define this method
        return TYPE_YOUTUBE; 
    } else {
        return TYPE_IMAGE;
    }
}

然后在onCreateViewHolder中检查要创建的类型:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
    RecyclerView.ViewHolder holder;
    switch (viewType) {
        case TYPE_YOUTUBE:
                //note: save an inflater when you construct the adapter
                return new YtViewHolder(inflater.inflate(R.layout.item_youtube,parent,false));
        case TYPE_IMAGE:
                return new ImgViewHolder(inflater.inflate(R.layout.item_image,false));
        }

    }

分开你的VH模型:

public abstract class PostViewHolder extends RecyclerView.ViewHolder { //note: extends changed
    public abstract void bind(final String input){
}

public static class YtViewHolder extends PostViewHolder { //note: extends changed

    private YouTubePlayerView mView;

    public YtViewHolder(View itemView) {
        super(itemView);
        mView = (YouTubePlayerView) itemView.findViewById(R.id.youtuber);
    }

    public void bind(final String youtube){
        mView.initialize("SOME KEY",boolean b) {
                    youTubePlayer.cueVideo(youtube);
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider,YouTubeInitializationResult youTubeInitializationResult) {

                }
        });
    }

}

public static class ImgViewHolder extends PostViewHolder {

    private ImageView mView;

    public ImageView(View itemView) {
        super(itemView);
        mView = (ImageView) itemView.findViewById(R.id.post_image);
    }

    public void bind(final String image){
        Picasso.with(mView.getContext()).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(mView,new Callback() {

            @Override
            public void onSuccess() {
            }
            @Override
            public void onError() {
                Picasso.with(ctx).load(image).into(mView);
            }
        });
    }

}

您的populate方法可以简化如下:

@Override
protected void populateViewHolder(PostViewHolder viewHolder,int position) {
    viewHolder.bind(model.isYoutube()? model.getYoutube() : model.getImage());
}

不同的项目:

then there is only one video playing in all items

这有点令人担忧.如果我理解正确,列表中的每个项目都具有相同的图像和视频.这可能意味着您将相同的模型返回到每个位置.确保如果实现getItem,则为每个位置返回不同的项目(Firebase适配器默认情况下会执行此操作).还要确保model.getYoutube等不返回静态数据.

原文链接:https://www.f2er.com/android/309213.html

猜你在找的Android相关文章