RecyclerView具有不同的宽度

时间:2016-03-20 14:46:32

标签: android android-recyclerview

我想创建包含高度相同但宽度不同的项目的RecyclerView。使用Horizo​​ntal Recycler View非常容易,但我希望使用Vertical Recycler View。

结果应如下所示:

 ___________________________________________
|                                           | 
|[................]  [......] [........]    |
|[......................]  [..............] |
|[.....]  [.................]               |
|[......................]  [......]         |
|                                           |
 -------------------------------------------

我怎样才能实现这一目标?或者也许有比回收者观点更好的东西?

2 个答案:

答案 0 :(得分:2)

使用Doron Yakovlev-Golani建议的方法,我在这个课程中做到了:

public class LessonsLayoutManager extends StaggeredGridLayoutManager {

    private Point mMeasuredDimension = new Point();

    public LessonsLayoutManager() {
        super(2, HORIZONTAL);
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                          int widthSpec, int heightSpec) {

        final int widthSize = View.MeasureSpec.getSize(widthSpec) - (getPaddingRight() + getPaddingLeft());

        int width = 0;
        int height = 0;
        int row = 0;

        for (int i = 0; i < getItemCount(); i++) {

            if (!measureScrapChild(recycler, i,
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                mMeasuredDimension)) continue;

            if (width + mMeasuredDimension.x > widthSize || mMeasuredDimension.x > widthSize) {
                row++;
                width = mMeasuredDimension.x;
            } else {
                width += mMeasuredDimension.x;
            }

            height += mMeasuredDimension.y;
        }

        setSpanCount(row);
        setMeasuredDimension(View.MeasureSpec.getSize(widthSpec), height);
    }

    @Override
    public boolean canScrollHorizontally() {
        return false;
    }

    @Override
    public boolean canScrollVertically() {
        return false;
    }

    private boolean measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, Point measuredDimension) {

        View view = null;
        try {
            view = recycler.getViewForPosition(position);
        } catch (Exception ex) {
            // try - catch is needed since support library version 24
        }

        if (view != null) {

            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();

            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                getPaddingLeft() + getPaddingRight(), p.width);
            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                getPaddingTop() + getPaddingBottom(), p.height);

            view.measure(childWidthSpec, childHeightSpec);

            measuredDimension.set(
                view.getMeasuredWidth() + p.leftMargin + p.rightMargin,
                view.getMeasuredHeight() + p.bottomMargin + p.topMargin
            );

            recycler.recycleView(view);

            return true;
        }

        return false;
    }

}

结果如下:

nedb method update and delete creates a new entry instead updating existing one

答案 1 :(得分:1)

简短的回答是LayoutManager。它为您提供了许多选项,可以在RecyclerView中执行您想要的任何布局。您描述的布局似乎是GridLayoutManager提供的变体,因此可能从那里开始。您可以在此question中看到类似问题的解决方案。