What to use for drag and swipe items in horizontal scroll view – RecyclerView or ViewPager

问题内容:

I need to create a horizontal scrollable list that will contain videos as items. Only the current video needs to be played, and video items should be, dragged for changing the position of video clip, and swiped for deleting that video clip. I tried using RecyclerView with ItemTouchHelper and got that drag and swipe things working but playing the current video item is not happening. Not getting how to get that single video to play and pause. If I put the play method in ViewHolder then it plays all the videos simultaneously or it cannot play any video. So then I tried using view pager but then I cannot use ItemTouchHelper to perform drag – drop and swipe. Please guide me someone.

Below is a code portion for ease of understanding:

ViewHolder holding every single item:

public static class ItemViewHolder extends RecyclerView.ViewHolder {

    private ItemVideoClipBinding itemVideoClipBinding;

    public ItemViewHolder(ItemVideoClipBinding clipBinding) {
        super(clipBinding.getRoot());
        itemVideoClipBinding = clipBinding;
    }

    public void play(File file) {
        itemVideoClipBinding.videoGlSurfaceViewClips.setVideoUri(
                Uri.parse(file.getAbsolutePath()), MediaPlayer::start, new VideoPlayerGLSurfaceView.PlayCompletionCallback() {
            @Override
            public void playComplete(MediaPlayer player) {
                player.start();
            }

            @Override
            public boolean playFailed(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
    }
    }

Adapter class:

public class VideoClipsListAdapter extends RecyclerView.Adapter<VideoClipsListAdapter.ItemViewHolder> implements ItemTouchHelperAdapter {

    private ArrayList<File> videoClipsList = new ArrayList<>();
    private Activity activity;
    private LayoutInflater layoutInflater;

    public VideoClipsListAdapter(Activity activity, ArrayList<File> videoClipsList) {
        this.activity = activity;
        this.videoClipsList = videoClipsList;

    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (layoutInflater == null)
            layoutInflater = LayoutInflater.from(parent.getContext());

        ItemVideoClipBinding itemVideoClipBinding = ItemVideoClipBinding.inflate(layoutInflater, parent, false);
        return new ItemViewHolder(itemVideoClipBinding);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        holder.itemVideoClipBinding.videoGlSurfaceViewClips.getLayoutParams().width = CommonUtils.getDisplayWidth(activity);
        holder.itemVideoClipBinding.videoGlSurfaceViewClips.getLayoutParams().width = CommonUtils.getDisplayWidth(activity);
        holder.itemVideoClipBinding.videoGlSurfaceViewClips.requestLayout();
        holder.play(videoClipsList.get(position));
    }

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

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(videoClipsList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(videoClipsList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        boolean isDeleted = videoClipsList.get(position).delete();
        if (isDeleted) {
            videoClipsList.remove(position);
            notifyItemRemoved(position);
        }
    }
}

Scroll and video play code:

@Override

public void onScrollStart(@NonNull RecyclerView.ViewHolder currentItemHolder, int adapterPosition) {
    Log.d("TAG", "adapterPos" + adapterPosition);
    playerGLSurfaceView = (VideoPlayerGLSurfaceView) 
    currentItemHolder.itemView;
    playerGLSurfaceView.onPause();
}

@Override
public void onScrollEnd(@NonNull RecyclerView.ViewHolder currentItemHolder, int adapterPosition) {
    Log.d("TAG", "adapterPos" + adapterPosition);
    playerGLSurfaceView = (VideoPlayerGLSurfaceView) 
    currentItemHolder.itemView;
    playVideo(playerGLSurfaceView, adapterPosition);
}

private void playVideo(VideoPlayerGLSurfaceView currentVideoView, int adapterPosition) {
    currentVideoView.setVideoUri(Uri.parse(String.valueOf(videoClipsList.get(adapterPosition))),
            MediaPlayer::start, new VideoPlayerGLSurfaceView.PlayCompletionCallback() {
                @Override
                public void playComplete(MediaPlayer player) {
                    player.start();
                }

                @Override
                public boolean playFailed(MediaPlayer mp, int what, int extra) {
                    return false;
                }
            });
}

This is what I have tried in recyclerview but I want to try view pager as well if I can get that drag and drop of items.
Thanks

问题评论:

原文地址:

https://stackoverflow.com/questions/47756355/what-to-use-for-drag-and-swipe-items-in-horizontal-scroll-view-recyclerview-or

添加评论