user9822291
user9822291

Reputation:

load image and video in recylerview

I have an adapter class where I am able to load all video from Firebase.
But the problem is I can't load both, image and video in one RecyclerView

See what I have done:

I am using toro library to auto play video if your wondering and ExoPlayer to play video

This is my adapter class

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) {
        }
    });
}

Please help me guys!. Thanks in Advance!.

Upvotes: 0

Views: 2298

Answers (1)

Seyyed
Seyyed

Reputation: 1534

TheRecyclerView has ability to show items in multiple types.

In your case you can define two ViewType. one for Images and another for Videos.

Search about RecyclerView with multiple view types.

Take a look at here and this.

Also here is a question and answers about it.

Upvotes: 5

Related Questions