Reputation: 633
Is there a way from which I can know what percentage of my layout inside a recyclerView is visible on the Screen.
The layout inflated inside RecyclerView has params of
android:layout_width="match_parent"
android:layout_height="match_parent"
Edit added layout file
Layout file - custom_card.xml
<?xml version="1.0" encoding="utf-8"?
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/imageView"
android:layout_alignTop="@id/imageView"
android:scaleType="fitCenter"
android:src="@drawable/image_view_screen" />
</RelativeLayout>
Activity Layout File.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:visibility="gone"
android:layout_width="56dp"
android:layout_height="56dp"
android:id="@+id/progress_bar"
android:layout_centerInParent="true"/>
</RelativeLayout>
I am using a Vertical LinearLayoutManager
Upvotes: 4
Views: 1945
Reputation: 425
use this; in your adapter;
private final Rect mCurrentViewRect = new Rect();
... your inits...
in your onBindViewHolder;
View firstItemView = linearLayoutManager.findViewByPosition(position);
int pos= getVisibilityPercents(position);
use these metods;
public int getVisibilityPercents(View currentView) {
int percents = 100;
currentView.getLocalVisibleRect(mCurrentViewRect);
int height = currentView.getHeight();
if (viewIsPartiallyHiddenTop()) {
percents = (height - mCurrentViewRect.top) * 100 / height;
} else if (viewIsPartiallyHiddenBottom(height)) {
percents = mCurrentViewRect.bottom * 100 / height;
}
return percents;
}
private boolean viewIsPartiallyHiddenBottom(int height) {
return mCurrentViewRect.bottom > 0 && mCurrentViewRect.bottom < height;
}
private boolean viewIsPartiallyHiddenTop() {
return mCurrentViewRect.top > 0;
}
btw, dont remember get your recyclerView and linearLayoutManager to your adapter in constructor.
For more => https://github.com/danylovolokh/VideoPlayerManager
Upvotes: -1
Reputation: 633
After much Try I figured it out. If it helps anyone.
Since my layout height was match_parent
it was easier for me. But this method will work for everyone with some calculations.
int firstItemPos=mLayoutManager.findFirstVisibleItemPosition();
View firstItemView=mLayoutManager.findViewByPosition(firstItemPos);
int lastItemPos=mLayoutManager.findLastVisibleItemPosition();
View lastItemView=mLayoutManager.findViewByPosition(lastItemPos);
Now all items between firstItemPos
and lastItemPos
is visible 100%.
For firstItemView-
Math.abs(firstItemView.getY()) / firstItemView.getHeight()
Similarly for lastItemView-
Math.abs(lastItemView.getY()) / lastItemView.getHeight()
Upvotes: 2