In my app there is a recycler-view having video-views, I want to auto-play the specific video view which is completely in focus when scrolled.
Asked
Active
Viewed 8,389 times
2 Answers
10
RecyclerView rv_featureList;
private View currentFocusedLayout, oldFocusedLayout;
@Override
public void onCreate(Bundle instanceState) {
rv_featuredList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView,
int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//get the recyclerview position which is completely visible and first
int positionView = ((LinearLayoutManager) rv_featuredList.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
Log.i("VISISBLE", positionView + "");
if (positionView >= 0) {
if (oldFocusedLayout != null) {
//Stop the previous video playback after new scroll
VideoView vv_dashboard = (VideoView) oldFocusedLayout.findViewById(R.id.vv_dashboard);
vv_dashboard.stopPlayback();
}
currentFocusedLayout = ((LinearLayoutManager) rv_featuredList.getLayoutManager()).findViewByPosition(positionView);
VideoView vv_dashboard = (VideoView) currentFocusedLayout.findViewById(R.id.vv_dashboard);
//to play video of selected recylerview, videosData is an array-list which is send to recyclerview adapter to fill the view. Here we getting that specific video which is displayed through recyclerview.
playVideo(videosData.get(positionView));
oldFocusedLayout = currentFocusedLayout;
}
}
}
});
}

TGIO
- 523
- 8
- 19

Amna Mirza
- 705
- 1
- 5
- 21
-
2What is this? If you want to update your source code, edit your own question first. – Nguyễn Hoài Nam Nov 16 '15 at 06:03
-
@NguyễnHoàiNam I edited the answer to as much generalized possible, the application i am working has rich contents which are unable to post here as question. – Amna Mirza Nov 16 '15 at 06:20
-
2I mean you should edit your original QUESTION instead. But well anyway, what is the problem now, with your current implementation? – Nguyễn Hoài Nam Nov 16 '15 at 06:27
-
I solved it now after working on it about 2 to 3 days and i provide the way out here – Amna Mirza Nov 16 '15 at 06:28
-
2Glad to hear that. You should check this as accepted answer (even if it was yours), so others (like me) would not misunderstand. And it would help others though. – Nguyễn Hoài Nam Nov 16 '15 at 06:46
-
1Why will this work? Code-only answers are generally unhelpful. – Sufian Nov 19 '15 at 09:18
-
can i get complete code becuase i don't understand how playVideo() function play video.. – Nitin Nov 12 '16 at 04:58
-
What is playVideo(videosData.get(positionView)); – Mehul Tank Jul 31 '17 at 06:10
-
Please Provide Full Source Code – Ravish Sharma Feb 06 '18 at 06:08
0
Answer inspired from https://stackoverflow.com/a/45281307/9752209
Callback to get center item position when scrolling stopped
import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class RecyclerCenterItemFinder(
private val context: Context,
private val layoutManager: LinearLayoutManager,
private val callback: (Int) -> Unit,
private val controlState: Int = RecyclerView.SCROLL_STATE_IDLE
) :
RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (controlState == ALL_STATES || newState == controlState) {
val firstVisible = layoutManager.findFirstVisibleItemPosition()
val lastVisible = layoutManager.findLastVisibleItemPosition()
val itemsCount = lastVisible - firstVisible + 1
val screenCenter: Int = context.resources.displayMetrics.widthPixels / 2
var minCenterOffset = Int.MAX_VALUE
var middleItemIndex = 0
for (index in 0 until itemsCount) {
val listItem = layoutManager.getChildAt(index) ?: return
val topOffset = listItem.top
val bottomOffset = listItem.bottom
val centerOffset =
Math.abs(topOffset - screenCenter) + Math.abs(bottomOffset - screenCenter)
if (minCenterOffset > centerOffset) {
minCenterOffset = centerOffset
middleItemIndex = index + firstVisible
}
}
callback(middleItemIndex)
}
}
companion object {
const val ALL_STATES = 10
}
}
recycler.addOnScrollListener(RecyclerCenterItemFinder(requireContext(),
recycler.layoutManager,
{ centerItemPosition ->
playVideoAtPosition(centerItemPosition)
}
))

Omkar T
- 755
- 8
- 19