[英]Get Exact Number of Recyclerview Items to fit the available screen height
I am fetching results from the web to display it in the RecyclerView, in order to optimize the process, I wish to limit maximum results fetched from the web. 我正在从Web上获取结果以在RecyclerView中显示,为了优化流程,我希望限制从Web上获取的最大结果。
In order to limit the number of results, I need to know how many list items will be required to fill the entire screen. 为了限制结果的数量,我需要知道填充整个屏幕需要多少个列表项。 I have the height of each list item in dp, but I am not able to get the height of available space on screen so that I can divide screen height by height of each item (dp).
我具有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>
So how to get the available height on the screen, excluding the actionbar 那么如何获得屏幕上的可用高度(不包括操作栏)
You can get screen size like this: 您可以这样获得屏幕尺寸:
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int displayHeight = size.y;
You can get actionBar height like this 你可以像这样获得actionBar的高度
TypedValue typedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize,typedValue, true);
int actionBarHeight = getResources().getDimensionPixelSize(typedValue.resourceId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.