我希望能够通过滚动instagram等回收站视图来播放和暂停视频。 我已经在stackoverflow上看到了很多与此主题有关的问题,但是我不知道如何在我的代码上实现解决方案。 因此,我有一个Recycler Adapter,可以从firebase获取所有用户信息并将其显示在我的应用程序中。
public class FeedRecyclerAdapter extends RecyclerView.Adapter<FeedRecyclerAdapter.ViewHolder> {
private List<Feed>feedList;
private FirebaseFirestore firebaseFirestore;
public Context context;
private FirebaseAuth firebaseAuth;
public FeedRecyclerAdapter(List<Feed> feedList){
this.feedList = feedList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_row, parent, false);
context = parent.getContext();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.setIsRecyclable(false);
final String feedPostId = feedList.get(position).FeedPostId;
final String currentUserId = firebaseAuth.getCurrentUser().getUid();
String desc_data = feedList.get(position).getDesc();
holder.setDesc(desc_data);
String video_url = feedList.get(position).getVideo_url();
holder.setFeedVideo(video_url);
}
@Override
public int getItemCount() {
return feedList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView desc;
private View mView;
private VideoView videoView;
private TextView username;
public ViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setDesc(String descText){
desc = mView.findViewById(R.id.post_desc);
desc.setText(descText);
}
public void setFeedVideo(final String downloadUri){
videoView = mView.findViewById(R.id.postVideo);
videoView.setVideoPath(downloadUri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setLooping(true);
}
});
videoView.start();
}
}
答案 0 :(得分:0)
首先使用如下所示的 ShapeHelper 设置您的 RecyclerView
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
然后在 LayoutManager 的帮助下,获得 RecyclerView 可见的位置,如下所示
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
在这里使用GridLayoutManager与您的lyaout管理器更改LayoutManger。
有关更多参考,请参见链接