Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Recyclerview中显示固定数量的项目?_Android_Android Recyclerview - Fatal编程技术网

Android 如何在Recyclerview中显示固定数量的项目?

Android 如何在Recyclerview中显示固定数量的项目?,android,android-recyclerview,Android,Android Recyclerview,我的任务是在屏幕上显示固定数量的项目。 这并不意味着我有固定大小的列表,这意味着只有5项应该是可见的滚动时 如何做到这一点? 我没有找到任何关于它的有用信息。如果我没有弄错您的问题,那么每当用户停止滚动时,您都会试图在屏幕上显示固定数量的列表项 这可以通过计算屏幕高度/宽度,然后相应地设置列表项布局尺寸高度/宽度来实现 view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY; 现在,根据您想要的是水平列表

我的任务是在屏幕上显示固定数量的项目。 这并不意味着我有固定大小的列表,这意味着只有5项应该是可见的滚动时

如何做到这一点?
我没有找到任何关于它的有用信息。

如果我没有弄错您的问题,那么每当用户停止滚动时,您都会试图在屏幕上显示固定数量的列表项

这可以通过计算屏幕高度/宽度,然后相应地设置列表项布局尺寸高度/宽度来实现

view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;
现在,根据您想要的是水平列表还是垂直列表,更改列表项布局的宽度或高度值

检查这些链接


最简单的解决方案是让onBindViewHolder动态设置其视图的高度/宽度。对于垂直列表:

float containerHeight = mRecyclerView.getHeight();
holder.itemView.setMinimumHeight(Math.round(containerHeight/5));

我也遇到了类似的问题。我几乎完美地解决了这个问题。我选择扩展LinearLayoutManager

公共类MaxCountLayoutManager扩展了LinearLayoutManager{ 私有整数最大计数=-1; 公共MaxCountLayoutManagerContext上下文{ 超级上下文; } 公共MaxCountLayoutManagerContext上下文、int方向、布尔反转{ 超级上下文,方向,反转; } 公共MaxCountLayoutManagerContext上下文、属性集属性、int-defStyleAttr、int-defStyleRes{ 超级上下文,attrs,defStyleAttr,defStyleRes; } 公共无效setMaxCountint maxCount{ this.maxCount=maxCount; } @凌驾 公共空间集MeasuredDimensionInt widthSize,int heightSize{ int maxHeight=getMaxHeight; 如果maxHeight>0&&maxHeight如果getChildCount==0 | | maxCount as for me child.layoutParams.height返回-2 WRAP_CONTENT,但child.height在pixels@YTerle是的!你说得对。我写的代码只是一个参考想法。你可以用自己的方式实现它