在GridLayoutManager中,我可以设置跨度计数,这使得视图中的项可以调整大小,以便能够水平地适应跨度计数。
我有一个LinearLayoutManager,我想以同样的方式使用它,让固定数量的项目可见,并根据需要调整它们的大小。
我在同一视图上同时使用线性和网格,并根据屏幕大小显示项目。我似乎找不到一种方法来让两个布局显示相同数量的项目。
发布于 2016-05-27 16:52:08
已更新
我之前的回答是使用ItemsAdapter来设置每个项目的宽度,从代码设计的角度来看,这不是最好的解决方案。正确的方法是扩展LinearLayoutManager,因为布局项目视图是LayoutManager的职责。
要点:https://gist.github.com/modjke/b652021679a2ed1935645a18102ab799
代码:
//usage:
//int itemsPerPage = 7;
//layoutManager = new LinearLayoutPagerManager(context, LinearLayoutManager.HORIZONTAL, false, itemsPerPage);
//recyclerView.setLayoutManager(layoutManager);
public class LinearLayoutPagerManager extends LinearLayoutManager {
private int mItemsPerPage;
public int getItemsPerPage()
{
return mItemsPerPage;
}
public LinearLayoutPagerManager(Context context, int orientation, boolean reverseLayout, int itemsPerPage) {
super(context, orientation, reverseLayout);
mItemsPerPage = itemsPerPage;
}
@Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
return super.checkLayoutParams(lp) && lp.width == getItemSize();
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return setProperItemSize(super.generateDefaultLayoutParams());
}
@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return setProperItemSize(super.generateLayoutParams(lp));
}
private RecyclerView.LayoutParams setProperItemSize(RecyclerView.LayoutParams lp) {
int itemSize = getItemSize();
if (getOrientation() == HORIZONTAL) {
lp.width = itemSize;
} else {
lp.height = itemSize;
}
return lp;
}
private int getItemSize() {
int pageSize = getOrientation() == HORIZONTAL ? getWidth() : getHeight();
return Math.round((float) pageSize / mItemsPerPage);
}}
上一个答案
通过提供每个项目的宽度,您可以在适配器中设置准确的项目计数。仅当所有项的宽度相等时才有效,请不要忘记设置
recyclerView.setHasFixedSize(true);在RecyclerView上
final static int ITEMS_PER_PAGE = 7;
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int itemWidth = parent.getWidth() / ITEMS_PER_PAGE;
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_layout, parent, false);
ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
layoutParams.width = itemWidth;
itemView.setLayoutParams(layoutParams);
return new ItemViewHolder(itemView);
}发布于 2019-02-05 17:07:26
以下是Mihail Ignatiev答案的kotlin版本
class LinearLayoutPagerManager(context: Context,orientation: Int,reverseLayout: Boolean, private val itemsPerPage: Int) : LinearLayoutManager(context,orientation,reverseLayout) {
override fun checkLayoutParams(lp: RecyclerView.LayoutParams?): Boolean {
return super.checkLayoutParams(lp) && lp!!.width == getItemSize()
}
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return setProperItemSize(super.generateDefaultLayoutParams())
}
override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams {
return setProperItemSize(super.generateLayoutParams(lp))
}
private fun setProperItemSize(lp: RecyclerView.LayoutParams): RecyclerView.LayoutParams {
val itemSize = getItemSize()
if (orientation == LinearLayoutManager.HORIZONTAL) {
lp.width = itemSize
} else {
lp.height = itemSize
}
return lp
}
private fun getItemSize(): Int {
val pageSize = if (orientation == LinearLayoutManager.HORIZONTAL) width else height
return Math.round(pageSize.toFloat() / itemsPerPage)
}
}发布于 2018-10-18 07:39:23
我知道不久前有人问过这个问题,但我找到了一个简单得多的解决方案。
只需创建您自己的LayoutManager并覆盖getChildCount()方法
结果应该是这样的:
@Override
public int getChildCount() {
return super.getChildCount() > 3 ? 3 : super.getChildCount();
}这样,当您将此LayoutManager添加到回收视图中时,它将一次仅显示3个项目,而不管适配器有多少个项目。
而且,如果你想在滚动时获得类似寻呼机的体验,只需添加一个SnapHelper
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);https://stackoverflow.com/questions/36635442
复制相似问题