有没有办法让我知道在RecyclerView中我的布局的百分比在屏幕上是可见的。
在RecyclerView中膨胀的布局具有
的参数 android:layout_width="match_parent"
android:layout_height="match_parent"
修改添加的布局文件
布局文件 - 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>
活动布局文件。
<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>
我正在使用Vertical LinearLayoutManager
答案 0 :(得分:1)
经过多次尝试我弄清楚了。如果有帮助的话。
由于我的布局高度为match_parent
,因此对我来说更容易。但是这种方法对每个人都有一些计算效果。
int firstItemPos=mLayoutManager.findFirstVisibleItemPosition();
View firstItemView=mLayoutManager.findViewByPosition(firstItemPos);
int lastItemPos=mLayoutManager.findLastVisibleItemPosition();
View lastItemView=mLayoutManager.findViewByPosition(lastItemPos);
现在firstItemPos
和lastItemPos
之间的所有项目都可见100%。
对于firstItemView-
Math.abs(firstItemView.getY()) / firstItemView.getHeight()
与lastItemView-
类似Math.abs(lastItemView.getY()) / lastItemView.getHeight()
答案 1 :(得分:-1)
用这个; 在适配器中;
private final Rect mCurrentViewRect = new Rect();
... your inits...
你的onBindViewHolder中的;
View firstItemView = linearLayoutManager.findViewByPosition(position);
int pos= getVisibilityPercents(position);
使用这些方法;
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;
}
不过,不记得在构造函数中将recyclerView和linearLayoutManager放到适配器上。