在recylerview中加载图像和视频

时间:2018-06-01 09:36:44

标签: android firebase android-recyclerview

我有一个适配器类,我可以从Firebase加载所有视频。
但问题是我无法在一个RecyclerView

中同时加载图像和视频

看看我做了什么:

我正在使用toro库自动播放视频,如果您想知道并ExoPlayer播放视频

这是我的适配器类

public class MainFeedAdapter extends RecyclerView.Adapter<TestAdapter.MyViewHolder> {
private static final String TAG = "MainfeedAdapter";
private List<Photo> moviesList;
private DatabaseReference mReference;
private Context mContext;
private String currentUsername = "";
private int mLayoutResource;
private LayoutInflater mInflater;
private Photo photo;
private MyViewHolder mHolder;

public class MyViewHolder extends RecyclerView.ViewHolder implements ToroPlayer{
    static final int LAYOUT_RES = R.layout.main_list;
    private ExoPlayerViewHelper helper;
    private CircleImageView profile_image;
    //private String likeString;
    private TextView username,time,caption,likes,comment,stars;
    private SquareImageView image;
    private LikeButton mHeart,Star;
    private UserAccountSettings userAccountSettings = new UserAccountSettings();
    private User user = new User();
    private  StringBuilder users;
    private String mLIkeString;
    private String mStarString;
    private boolean likeByCurrentUSer;
    private boolean starbycurrentuser;
    private DatabaseReference mNotification;
    private ImageView commentBubble;
    private Uri mediaUri;
    @BindView(R.id.main_post_image2)
    PlayerView playerView;
    private CardView video;

    public MyViewHolder(View view) {
        super(view);
        profile_image = (CircleImageView)view.findViewById(R.id.post_profile_photo);
        username = (TextView) view.findViewById(R.id.main_username);
        time = (TextView) view.findViewById(R.id.main_image_time_posted);
        caption = (TextView) view.findViewById(R.id.main_image_caption);
        likes = (TextView) view.findViewById(R.id.main_likes);
        comment = (TextView) view.findViewById(R.id.main_showcomments);
        image = (SquareImageView) view.findViewById(R.id.main_post_image);
        mHeart = (LikeButton) view.findViewById(R.id.main_heart);
        Star = (LikeButton) view.findViewById(R.id.main_star);
        stars= (TextView)view.findViewById(R.id.stars);
        commentBubble = (ImageView)view.findViewById(R.id.main_comments) ;
        mNotification = FirebaseDatabase.getInstance().getReference().child("notification");
        mReference = FirebaseDatabase.getInstance().getReference();
        ButterKnife.bind(this, itemView);
        video = (CardView)view.findViewById(R.id.video_posts);

    }

    @NonNull
    @Override
    public View getPlayerView() {
        return playerView;
    }
    @NonNull
    @Override
    public PlaybackInfo getCurrentPlaybackInfo() {
        return helper != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo();
    }
    @Override
    public void initialize(@NonNull Container container, @Nullable PlaybackInfo playbackInfo) {
        if (helper == null) {
            helper = new SimpleExoPlayerViewHelper(container, this, mediaUri);
        }
        helper.initialize(playbackInfo);
    }
    // called from Adapter to setup the media
    void bind( Uri item, List<Photo> payloads) {
        if (item != null) {
            mediaUri = item;
        }else {
            video.setVisibility(View.GONE);
        }
    }
    @Override
    public void play() {
        if (helper != null) helper.play();
    }
    @Override
    public void pause() {
        if (helper != null) helper.pause();
    }
    @Override
    public boolean isPlaying() {
        return  helper != null && helper.isPlaying();
    }
    @Override
    public void release() {
        if (helper != null) {
            helper.release();
            helper = null;
        }
    }
    @Override
    public boolean wantsToPlay() {
        return ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= 0.85;
    }
    @Override
    public int getPlayerOrder() {
        return getAdapterPosition();
    }

    @Override
    public void onSettled(Container container) {

    }
}

public TestAdapter(List<Photo> moviesList) {
    this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.main_list, parent, false);
    mContext = parent.getContext();
    return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    photo = moviesList.get(position);
    mHolder = holder;
    holder.users = new StringBuilder();
    getCurrentUsername();
    getLikesString(mHolder);
    getStarString(mHolder);
    holder.bind(Uri.parse(photo.getVideo_path()),moviesList);



    //get the profile image and username
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference
            .child(mContext.getString(R.string.dbname_user_account_settings))
            .orderByChild(mContext.getString(R.string.field_user_id))
            .equalTo(getItem(position).getUser_id());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                // currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
                Log.d(TAG, "onDataChange: found user: "
                        + singleSnapshot.getValue(UserAccountSettings.class).getUsername());

                holder.username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
                holder.username.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "onClick: navigating to profile of: " +
                                holder.user.getUsername());
                        Intent intent = new Intent(mContext, Profile_Activity.class);
                        intent.putExtra(mContext.getString(R.string.calling_activity),
                                mContext.getString(R.string.home_activity));
                        intent.putExtra(mContext.getString(R.string.intent_user), holder.user);
                        mContext.startActivity(intent);
                    }
                });
                imageLoader.displayImage(singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
                        holder.profile_image);
                holder.profile_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "onClick: navigating to profile of: " +
                                holder.user.getUsername());
                        Intent intent = new Intent(mContext, Profile_Activity.class);
                        intent.putExtra(mContext.getString(R.string.calling_activity),
                                mContext.getString(R.string.home_activity));
                        intent.putExtra(mContext.getString(R.string.intent_user), holder.user);
                        mContext.startActivity(intent);
                    }
                });
                holder.userAccountSettings = singleSnapshot.getValue(UserAccountSettings.class);
                holder.comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((HomeActivity)mContext).onCommentThreadSelected(getItem(position),mContext.getString(R.string.home_activity));

                        //another thing?
                        ((HomeActivity)mContext).hideLayout();
                    }
                });
                holder.commentBubble.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((HomeActivity)mContext).onCommentThreadSelected(getItem(position),mContext.getString(R.string.home_activity));

                        //another thing?
                        ((HomeActivity)mContext).hideLayout();
                    }
                });
            }

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });
    Query userQuery = mReference
            .child(mContext.getString(R.string.dbname_users))
            .orderByChild(mContext.getString(R.string.field_user_id))
            .equalTo(getItem(position).getUser_id());
    userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                Log.d(TAG, "onDataChange: found user: " +
                        singleSnapshot.getValue(User.class).getUsername());

                holder.user = singleSnapshot.getValue(User.class);
            }

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private boolean rechedendoflist(int position){
    return position == getItemCount() -1;\
}

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

public Photo getItem(int position) {
    return moviesList.get(position);
}

private void getCurrentUsername(){
    Log.d(TAG, "getCurrentUser: ");
    Log.d(TAG, "getCurrentUsername: retrieving user account settings");
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Query query = reference
            .child(mContext.getString(R.string.dbname_users))
            .orderByChild(mContext.getString(R.string.field_user_id))
            .equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

请帮帮我们!在此先感谢!。

1 个答案:

答案 0 :(得分:1)

RecyclerView能够以多种类型显示项目。

在您的情况下,您可以定义两个ViewType。一个用于图像,另一个用于视频。

使用多种视图类型搜索RecyclerView

查看herethis

同样here是一个问题和答案。