RecyclerView LinearLayoutManager设置项目数 [英] RecyclerView LinearLayoutManager set item count

查看:539
本文介绍了RecyclerView LinearLayoutManager设置项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GridLayoutManager中,我可以设置跨度计数,这使得视图内的项目可以调整大小以使其能够水平适应该跨度计数.

In GridLayoutManager i am able to set span count and that makes the items inside the view to resize to be able to fit that span count horizontally.

我有一个LinearLayoutManager,我想以相同的方式使用它,可以看到固定数量的项目,并调整它们的大小以适合他们.

I have a LinearLayoutManager and i want to use it the same way, have a fixed number of items visible and resize them to fit.

我在同一视图上同时使用线性和网格,并根据屏幕尺寸显示项目.我似乎找不到一种方法来使两种布局都显示相同数量的项目.

I use both linear and grid on same view and shows items depending of the screen size. I can't seem to find a way to get both layouts showing the same amount of items.

推荐答案

更新

我以前的答案是使用ItemsAdapter设置每个项目的宽度,从代码设计的角度来看,这不是最佳解决方案. 正确的方法是扩展LinearLayoutManager,因为LayoutManager负责布局项目视图.

UPDATED

My previous answer was to use ItemsAdapter to set each items width, from the code design stand point this is not the best solution. Proper way to do that is to extend LinearLayoutManager, since it is LayoutManager responsibility to layout item views.

要点: 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);
}

}

您可以通过为每个项目提供宽度来在适配器中设置确切的项目数.仅当您所有物品的宽度相等时,此功能才起作用,请不要忘记设置

You can set exact item count in your adapter by providing a width for each item. This works only if all your items have equal width, do not forget to set

recyclerView.setHasFixedSize(true);

在您的RecyclerView上

on your 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);
}

这篇关于RecyclerView LinearLayoutManager设置项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆