简体   繁体   English

RecyclerView LinearLayoutManager设置项目数

[英]RecyclerView LinearLayoutManager set item count

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. 在GridLayoutManager中,我可以设置跨度计数,这可以使视图内的项目调整大小以使其能够水平适应该跨度计数。

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. 我有一个LinearLayoutManager,我想以相同的方式使用它,有固定数量的可见项目,并调整它们的大小以适合。

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. 我似乎找不到一种使两种布局显示相同数量的项目的方法。

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. 我以前的答案是使用ItemsAdapter设置每个项目的宽度,从代码设计的角度来看,这不是最佳解决方案。 Proper way to do that is to extend LinearLayoutManager, since it is LayoutManager responsibility to layout item views. 正确的方法是扩展LinearLayoutManager,因为LayoutManager负责布局项目视图。

Gist: https://gist.github.com/modjke/b652021679a2ed1935645a18102ab799 要点: https : //gist.github.com/modjke/b652021679a2ed1935645a18102ab799

Code: 码:

//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);
}

} }

PREVIOUS ANSWER 上一个答案

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);

on your RecyclerView 在您的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);
}

I know this was asked a while ago, but I've found a much simpler solution. 我知道这是前一段时间提出的,但是我找到了一个更简单的解决方案。

Just create your own LayoutManager and overwrite the method getChildCount() 只需创建自己的LayoutManager并覆盖方法getChildCount()

The result should be something like this: 结果应该是这样的:

@Override
public int getChildCount() {
   return super.getChildCount() > 3 ? 3 : super.getChildCount();
}

That way, when you add this LayoutManager to the Recyclerview, it will just display 3 items at a time, no matter how many items the adapter has. 这样,将这个LayoutManager添加到Recyclerview时,无论适配器有多少个,它一次只会显示3个项目。

And, if you want a Pager like experience when scrolling just add a SnapHelper 而且,如果您想在滚动时获得类似Pager的体验,只需添加SnapHelper

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);

Here is the kotlin version of Mihail Ignatiev's Answer 这是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)
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM