I want to get the visibility of the Recyclerview
item in the percentage i.e. view is that much percent visible on screen while scrolling. I want to implement that if view is 50% visible i want to hide some other view else visible.

- 697
- 3
- 12
- 28
-
Possible duplicate of [Hiding views in RecyclerView](http://stackoverflow.com/questions/27574805/hiding-views-in-recyclerview) – SaravInfern Jul 23 '16 at 09:21
-
What you call `View` here? Row in RecyclerView or RecyclerView itself? – Divers Jul 23 '16 at 09:22
-
@SaravInfern I want to get visibility in percentage..i.e.integer value – Shikha Ratra Jul 23 '16 at 09:23
-
@SaravInfern I think it's different, this one is asking "how much of a row in the recycler is visible at this time" and only after that he wants to hide or show certain views – LuigiPower Jul 23 '16 at 09:23
-
You could keep track of all the underlying inflated views or viewholders in a weakhashmap, and then query their coordinates, compare that to the screen size to derive a percentage. It's probably a lot more complicated than you want, but I'm not aware of anything simple. – NameSpace Jul 23 '16 at 09:28
-
@SaravInfern means the row that contains `Linear Layout`. its visibility – Shikha Ratra Jul 23 '16 at 09:29
-
Try this solution it will give you percent value of each row https://stackoverflow.com/a/49168492/5498233 – Vishal Sanghani Mar 08 '18 at 08:25
-
try this solution that will give you percent visible of each row. https://stackoverflow.com/a/49168492/5498233 – Vishal Sanghani Mar 08 '18 at 08:26
3 Answers
Just kick off example:
private int getPercantageOfVisibleRow(RecyclerView recyclerView) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int visibleRows = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition();
return (int) (((float) (visibleRows / layoutManager.getItemCount())) * 100);
}

- 9,531
- 7
- 45
- 88
You are probably looking for this library:
https://github.com/danylovolokh/VideoPlayerManager/tree/master/list-visibility-utils
this library gives you callback which is called when MOST(by percentage) visible item gets focus. You can easily modify example to get item in focus visibility percentage.
But just to warn you this lib is not stable and mature enoguh, am using it in production app but is very buggy and am trying to replace it with something more stable.

- 399
- 3
- 10
you can use GravitySnapHelper: If you need snapping support to start, top, end or bottom, use GravitySnapHelper.
Add this to your build.gradle:
implementation 'com.github.rubensousa:gravitysnaphelper:1.5'
Otherwise, center snapping is done with LinearSnapHelper (part of the recyclerview-v7 package).
Snapping center:
SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
Snapping start with GravitySnapHelper:
startRecyclerView.setLayoutManager(new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false));
SnapHelper snapHelperStart = new GravitySnapHelper(Gravity.START);
snapHelperStart.attachToRecyclerView(startRecyclerView);
Snapping top with GravitySnapHelper:
topRecyclerView.setLayoutManager(new LinearLayoutManager(this));
SnapHelper snapHelperTop = new GravitySnapHelper(Gravity.TOP);
snapHelperTop.attachToRecyclerView(topRecyclerView);

- 29
- 7