获取确切数量的Recyclerview项目以适应可用的屏幕高度

时间:2018-03-26 09:54:20

标签: android android-layout android-recyclerview

我从网上获取结果以在RecyclerView中显示它,为了优化流程,我希望限制从网络上获取的最大结果。

为了限制结果的数量,我需要知道填充整个屏幕需要多少列表项。我有dp中每个列表项的高度,但我无法获得屏幕上可用空间的高度,以便我可以将屏幕高度除以每个项目的高度(dp)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/video_list_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.bgbtech.gujaratirasoi.VideoListScreen">

     <android.support.v7.widget.RecyclerView
        android:id="@+id/video_list_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:visibility="gone" />

 </RelativeLayout>

那么如何获得屏幕上的可用高度,不包括操作栏

1 个答案:

答案 0 :(得分:0)

您可以获得如下屏幕尺寸:

Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
int displayHeight = size.y;

你可以像这样获得actionBar高度

TypedValue typedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize,typedValue, true);
int actionBarHeight = getResources().getDimensionPixelSize(typedValue.resourceId);