I want to play a video when its view is 75% on the screen inside of the recyclerView adapter. So findFirstVisibleItem and all those would not work here. (Or if they do, I have no idea how they would work).
Currently I am waiting for views to be recycled before I am able to pause or play a video, but this begins the moment a new view is generated. Can anyone please help I have been researching this for very long and have gotten no where.
4 Answers
Answers 1
All you have to do is use an **Model Class**
.!
Class Video{ String videoUrl; //Http:// int videoDrawable; //R.id.video String videoName=; boolean isplaying=flase; boolean ispuase=false; int lastDuration=0; //Constructors gettter and setter here.! } /////////////////// Class AdapterClasss{ int lastpostion=-1; VideoView videoView ; public MyViewHolder(View view) { super(view); videoView =(VideoView)view.findViewById(R.id.videoView1); playBtn=(Button)view.findViewById(R.id.playBtn); pauseBtn=(Button)view.findViewById(R.id.pauseBtn); }
now in Your Adapter Class
do few changes in onBindViewHolder
@Override public void onBindViewHolder(final MyViewHolder holder, final int position) { final Video videoObj= videoList.get(position); /Creating MediaController holder.playBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MediaController mediaController= new MediaController(this); mediaController.setAnchorView(holder.videoView); //specify the location of media file Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/media/"+videoObj.getvideoName()+".mp4"); //Setting MediaController and URI, then starting the videoView videoView.setMediaController(mediaController); videoView.setVideoURI(uri); videoView.requestFocus(); lastpostion=position; if(videoObj.getisPuase()) { //now here you have to set your duration fro where you want to start your video if(videoView.ispause()) { videoView.start(); videoObj.setisPlaying(true); notifyItemChanged(position); } }else if(videoObj.getisPlaying()!=true) { videoView.start(); videoObj.setisPlaying(true); notifyItemChanged(position); } } holder.pauseBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int duration = videoView.getDuration(); videoView.pause(); lastDuration.setLastDuration(duration); videoObj.setisPlaying(false); videoObj.setisPlaying(true); } }
Answers 2
Use a RecyclerView.ItemDecoration.
ItemDecoration is called multiple time while rendering the recycler's content so it is not supposed to do heavy stuff here, but you can use it to notify when some percentage of the view is visible.
ItemDecoration have several methods, but the one you need to implement is:
public void onDrawOver(Canvas c, RecyclerView parent, State state);
or maybe
public void onDraw(Canvas c, RecyclerView parent, State state);
For the case you are asking for you will notice no difference, but to complement the explanation the first one is called when your row is rendered on the screen so you could paint "over" it and the second one is called before the row is rendered so you can paint something "below".
For this case you don't need to paint anything, just to calc the size of your view. For that mission you need to get the rendering views, and detect which one is the one with the video.
@Override public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount - 1; i++) { View child = parent.getChildAt(i); int adapterPos = parent.getChildAdapterPosition(child); int viewType = parent.getAdapter().getItemViewType(adapterPos); ... Your code here ... // if(viewType == THE_EXPECTED_TYPE) { // if(child.top() == parent.top()) { // notifyViewIsVisibleAt100(); //100%; make your calcs to ensure your 75% // } // } } }
You need to register the ItemDecoration to the recyclerView at first.
recyclerView.addItemDecoration(theItemDecoration);
Answers 3
Add this code in your recycle view scroll listener on ScrollStatechanged:
if (newState == RecyclerView.SCROLL_STATE_IDLE) { LinearLayoutManager layoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager()); int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition(); int findFirstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition(); Video video; if (urls != null && urls.size() > 0) { if (findFirstCompletelyVisibleItemPosition >= 0) { //handle here to play video in adapter } else { //code here for another position } }}
0 comments:
Post a Comment