Java Code Examples for androidx.recyclerview.widget.GridLayoutManager#getSpanCount()

The following examples show how to use androidx.recyclerview.widget.GridLayoutManager#getSpanCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * @param manager
 * @param position
 * @return
 */
protected int positionTotalSpanSize(GridLayoutManager manager, int position) {
    int totalSpanSize = 0;
    GridLayoutManager.SpanSizeLookup spanSizeLookup = manager.getSpanSizeLookup();
    int spanCount = manager.getSpanCount();
    int groupIndex = spanSizeLookup.getSpanGroupIndex(position, spanCount);
    for (int i = position; i >= 0; i--) {
        int thisGroupIndex = spanSizeLookup.getSpanGroupIndex(i, spanCount);
        if (thisGroupIndex == groupIndex) {
            totalSpanSize += spanSizeLookup.getSpanSize(i);
        } else {
            break;
        }
    }
    return totalSpanSize;
}
 
Example 2
Source File: FlexibleDividerDecoration.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * @param manager
 * @param position
 * @return
 */
protected int positionTotalSpanSize(GridLayoutManager manager, int position) {
    int totalSpanSize = 0;
    GridLayoutManager.SpanSizeLookup spanSizeLookup = manager.getSpanSizeLookup();
    int spanCount = manager.getSpanCount();
    int groupIndex = spanSizeLookup.getSpanGroupIndex(position, spanCount);
    for (int i = position; i >= 0; i--) {
        int thisGroupIndex = spanSizeLookup.getSpanGroupIndex(i, spanCount);
        if (thisGroupIndex == groupIndex) {
            totalSpanSize += spanSizeLookup.getSpanSize(i);
        } else {
            break;
        }
    }
    return totalSpanSize;
}
 
Example 3
Source File: SubsonicFragment.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            SectionAdapter adapter = getCurrentAdapter();
            if (adapter != null) {
                int viewType = adapter.getItemViewType(position);
                if (viewType == SectionAdapter.VIEW_TYPE_HEADER) {
                    return gridLayoutManager.getSpanCount();
                } else {
                    return 1;
                }
            } else {
                return 1;
            }
        }
    };
}
 
Example 4
Source File: RecyclerViewHelper.java    From AndroidFastScroll with Apache License 2.0 5 votes vote down vote up
private int getItemCount() {
    LinearLayoutManager linearLayoutManager = getVerticalLinearLayoutManager();
    if (linearLayoutManager == null) {
        return 0;
    }
    int itemCount = linearLayoutManager.getItemCount();
    if (itemCount == 0) {
        return 0;
    }
    if (linearLayoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) linearLayoutManager;
        itemCount = (itemCount - 1) / gridLayoutManager.getSpanCount() + 1;
    }
    return itemCount;
}
 
Example 5
Source File: SelectDirectoryFragment.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int viewType = entryGridAdapter.getItemViewType(position);
            if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == EntryInfiniteGridAdapter.VIEW_TYPE_LOADING) {
                return gridLayoutManager.getSpanCount();
            } else {
                return 1;
            }
        }
    };
}
 
Example 6
Source File: RecyclerPreloadView.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(int dx, int dy) {
    super.onScrolled(dx, dy);
    if (onRecyclerViewPreloadListener != null) {
        if (isEnabledLoadMore) {
            LayoutManager layoutManager = getLayoutManager();
            if (layoutManager == null) {
                throw new RuntimeException("LayoutManager is null,Please check it!");
            }
            Adapter adapter = getAdapter();
            if (adapter == null) {
                throw new RuntimeException("Adapter is null,Please check it!");
            }
            boolean isReachBottom = false;
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                int rowCount = adapter.getItemCount() / gridLayoutManager.getSpanCount();
                int lastVisibleRowPosition = gridLayoutManager.findLastVisibleItemPosition() / gridLayoutManager.getSpanCount();
                isReachBottom = (lastVisibleRowPosition >= rowCount - reachBottomRow);
            }

            if (!isReachBottom) {
                isInTheBottom = false;
            } else if (!isInTheBottom) {
                onRecyclerViewPreloadListener.onRecyclerViewPreloadMore();
                if (dy > 0) {
                    isInTheBottom = true;
                }
            } else {
                // 属于首次进入屏幕未滑动且内容未超过一屏,用于确保分页数设置过小导致内容不足二次上拉加载...
                if (dy == 0) {
                    isInTheBottom = false;
                }
            }
        }
    }
}
 
Example 7
Source File: SelectDeviceDialog.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    adapter.setThermometerMode();
    adapter.setBlueGeckoTabSelected(true);

    discovery.connect(activity);

    layout = new GridLayoutManager(activity, activity.getResources().getInteger(R.integer.device_selection_columns), LinearLayoutManager.VERTICAL, false);
    itemDecoration = new RecyclerView.ItemDecoration() {
        final int horizontalMargin = getResources().getDimensionPixelSize(R.dimen.item_margin);

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            final int columns = layout.getSpanCount();
            if (columns == 1) {
                outRect.set(0, 0, 0, 0);
            } else {
                int itemPos = parent.getChildAdapterPosition(view);
                if (itemPos % columns == columns - 1) {
                    outRect.set(0, 0, 0, 0);
                } else {
                    outRect.set(0, 0, horizontalMargin, 0);
                }
            }
        }
    };
}
 
Example 8
Source File: RecyclerViewTouchHelper.java    From YImagePicker with Apache License 2.0 5 votes vote down vote up
private int getSpanCount() {
    if (spanCount != 0) {
        return spanCount;
    }
    GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
    if (gridLayoutManager != null) {
        spanCount = gridLayoutManager.getSpanCount();
        return spanCount;
    }
    return 0;
}
 
Example 9
Source File: GridDividerDecoration.java    From pandora with Apache License 2.0 5 votes vote down vote up
private int getGroupIndex(int position, RecyclerView parent) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
        int spanCount = layoutManager.getSpanCount();
        return spanSizeLookup.getSpanGroupIndex(position, spanCount);
    }

    return position;
}
 
Example 10
Source File: LazyFetchingOnScrollListener.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
public void checkForNewElements(RecyclerView recyclerView) {
    int currentOffset = mLazyFetchingActivity.getCurrentOffset();
    int maxElementsToFetchTotal = mLazyFetchingActivity.getMaxElementsToFetch();

    // If the task has already been run, make a new task as a task can only be run once.
    if (getElementsTask.getStatus() == AsyncTask.Status.FINISHED) {
        getElementsTask = new GetVisualElementsTask<>(mLazyFetchingActivity);
    }

    // Only bother to check if we need to fetch more game objects if we are not already in the process of doing so.
    if (getElementsTask.getStatus() != AsyncTask.Status.RUNNING && currentOffset < maxElementsToFetchTotal) {
        GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
        RecyclerView.Adapter mAdapter = recyclerView.getAdapter();
        if (mAdapter == null) {
            return;
        }

        int lastViewPosition = lm.findLastVisibleItemPosition();
        int spanCount = lm.getSpanCount();
        int itemCount = mAdapter.getItemCount();
        final double FETCH_WHEN_BELOW_FIVE = 5;
        final double NUMBER_OF_ROWS = Math.ceil(itemCount / (spanCount * 1.0)); // Round UP to the nearest Integer
        final double LAST_ROW_VISIBLE = Math.ceil(lastViewPosition / (spanCount * 1.0)); // Round UP to the nearest Integer

        // If the Second to last or the last row is visible, then fetch more game objects.
        if (LAST_ROW_VISIBLE >= NUMBER_OF_ROWS - FETCH_WHEN_BELOW_FIVE) {
            getElementsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            mLazyFetchingActivity.startProgress();
        }
    }
}
 
Example 11
Source File: SearchFragment.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
    return new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int viewType = adapter.getItemViewType(position);
            if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == ArtistAdapter.VIEW_TYPE_ARTIST) {
                return gridLayoutManager.getSpanCount();
            } else {
                return 1;
            }
        }
    };
}
 
Example 12
Source File: RecyclerViewHelper.java    From AndroidFastScroll with Apache License 2.0 5 votes vote down vote up
private int getFirstItemPosition() {
    int position = getFirstItemAdapterPosition();
    LinearLayoutManager linearLayoutManager = getVerticalLinearLayoutManager();
    if (linearLayoutManager == null) {
        return RecyclerView.NO_POSITION;
    }
    if (linearLayoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) linearLayoutManager;
        position /= gridLayoutManager.getSpanCount();
    }
    return position;
}
 
Example 13
Source File: BaseDataAdapter.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
        if (mGridSpanSizeLookup == null) {
            mSpanCount = gridLayoutManager.getSpanCount();
            mGridSpanSizeLookup = getGridSpanSizeLookup();
        }
        gridLayoutManager.setSpanSizeLookup(mGridSpanSizeLookup);
    }
}
 
Example 14
Source File: EndlessRecyclerViewScrollListener.java    From android-app with GNU General Public License v3.0 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
 
Example 15
Source File: EndlessRecyclerViewScrollListener.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
 
Example 16
Source File: EndlessUpScrollListener.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
public EndlessUpScrollListener(GridLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
    visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
 
Example 17
Source File: GridSpacingItemDecoration.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/***/
private void drawGrideview1(Canvas canvas, RecyclerView parent) {
    GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();
    int childSize = parent.getChildCount();
    int top, bottom, left, right, spancount;
    spancount = linearLayoutManager.getSpanCount();
    for (int i = 0; i < childSize; i++) {
        final View child = parent.getChildAt(i);
        //画横线
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getBottom() + layoutParams.bottomMargin;
        bottom = top + space;
        left = layoutParams.leftMargin + child.getPaddingLeft() + space;
        right = child.getMeasuredWidth() * (i + 1) + left + space * i;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
        //画竖线
        top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);
        bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;
        left = child.getRight() + layoutParams.rightMargin;
        right = left + space;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }

        //画上缺失的线框
        if (i < spancount) {
            top = child.getTop() + layoutParams.topMargin;
            bottom = top + space;
            left = (layoutParams.leftMargin + space) * (i + 1);
            right = child.getMeasuredWidth() * (i + 1) + left + space * i;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
        if (i % spancount == 0) {
            top = (layoutParams.topMargin + space) * (i / linearLayoutManager.getSpanCount() + 1);
            bottom = (child.getMeasuredHeight() + space) * (i / linearLayoutManager.getSpanCount() + 1) + space;
            left = child.getLeft() + layoutParams.leftMargin;
            right = left + space;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }
}
 
Example 18
Source File: MaterialCalendar.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@NonNull
private ItemDecoration createItemDecoration() {
  return new ItemDecoration() {

    private final Calendar startItem = UtcDates.getUtcCalendar();
    private final Calendar endItem = UtcDates.getUtcCalendar();

    @Override
    public void onDraw(
        @NonNull Canvas canvas, @NonNull RecyclerView recyclerView, @NonNull State state) {
      if (!(recyclerView.getAdapter() instanceof YearGridAdapter)
          || !(recyclerView.getLayoutManager() instanceof GridLayoutManager)) {
        return;
      }
      YearGridAdapter adapter = (YearGridAdapter) recyclerView.getAdapter();
      GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();

      for (Pair<Long, Long> range : dateSelector.getSelectedRanges()) {
        if (range.first == null || range.second == null) {
          continue;
        }
        startItem.setTimeInMillis(range.first);
        endItem.setTimeInMillis(range.second);

        int firstHighlightPosition = adapter.getPositionForYear(startItem.get(Calendar.YEAR));
        int lastHighlightPosition = adapter.getPositionForYear(endItem.get(Calendar.YEAR));
        View firstView = layoutManager.findViewByPosition(firstHighlightPosition);
        View lastView = layoutManager.findViewByPosition(lastHighlightPosition);

        int firstRow = firstHighlightPosition / layoutManager.getSpanCount();
        int lastRow = lastHighlightPosition / layoutManager.getSpanCount();

        for (int row = firstRow; row <= lastRow; row++) {
          int firstPositionInRow = row * layoutManager.getSpanCount();
          View viewInRow = layoutManager.findViewByPosition(firstPositionInRow);
          if (viewInRow == null) {
            continue;
          }
          int top = viewInRow.getTop() + calendarStyle.year.getTopInset();
          int bottom = viewInRow.getBottom() - calendarStyle.year.getBottomInset();
          int left = row == firstRow ? firstView.getLeft() + firstView.getWidth() / 2 : 0;
          int right =
              row == lastRow
                  ? lastView.getLeft() + lastView.getWidth() / 2
                  : recyclerView.getWidth();
          canvas.drawRect(left, top, right, bottom, calendarStyle.rangeFill);
        }
      }
    }
  };
}
 
Example 19
Source File: SelectDeviceDialog.java    From EFRConnect-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onAttach(Context context) {
    super.onAttach(context);

    adapter = new ScannedDevicesAdapter(new DeviceInfoViewHolder.Generator(R.layout.device_item) {
        @Override
        public DeviceInfoViewHolder generate(View itemView) {
            final ViewHolder holder = new ViewHolder(itemView);
            holder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPos = holder.getAdapterPosition();
                    if (adapterPos != RecyclerView.NO_POSITION) {
                        BluetoothDeviceInfo devInfo = (BluetoothDeviceInfo) adapter.getDevicesInfo().get(adapterPos);
                        connect(devInfo);
                    }
                }
            });
            return holder;
        }

    }, context);

    discovery = new Discovery(adapter, this);

    adapter.setThermometerMode();

    discovery.connect(context);

    layout = new GridLayoutManager(context, context.getResources().getInteger(R.integer.device_selection_columns), LinearLayoutManager.VERTICAL, false);
    itemDecoration = new RecyclerView.ItemDecoration() {
        final int horizontalMargin = getResources().getDimensionPixelSize(R.dimen.item_margin);

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            final int columns = layout.getSpanCount();
            if (columns == 1) {
                outRect.set(0, 0, 0, 0);
            } else {
                int itemPos = parent.getChildAdapterPosition(view);
                if (itemPos % columns == columns - 1) {
                    outRect.set(0, 0, 0, 0);
                } else {
                    outRect.set(0, 0, horizontalMargin, 0);
                }
            }
        }
    };
}
 
Example 20
Source File: SpacerDecoration.java    From RecyclerExt with Apache License 2.0 4 votes vote down vote up
public int getSpanCount() {
    GridLayoutManager layoutManager = gridLayoutManager.get();
    return layoutManager == null ? 1 : layoutManager.getSpanCount();
}