android.support.v7.widget.GridLayoutManager类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(216)

本文整理了Java中android.support.v7.widget.GridLayoutManager类的一些代码示例,展示了GridLayoutManager类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GridLayoutManager类的具体详情如下:
包路径:android.support.v7.widget.GridLayoutManager
类名称:GridLayoutManager

GridLayoutManager介绍

[英]A RecyclerView.LayoutManager implementations that lays out items in a grid.

By default, each item occupies 1 span. You can change it by providing a custom SpanSizeLookup instance via #setSpanSizeLookup(SpanSizeLookup).
[中]回收站。在网格中布局项目的LayoutManager实现。
默认情况下,每个项目占用1个跨度。您可以通过#setPansizeLookup(SpanSizeLookup)提供自定义的SpanSizeLookup实例来更改它。

代码示例

代码示例来源:origin: Karumi/Rosie

private void initializeComicsView() {
 GridLayoutManager layoutManager = new GridLayoutManager(this, NUMBER_OF_COLUMNS);
 layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
  @Override public int getSpanSize(int position) {
   return position == 0 ? NUMBER_OF_COLUMNS : 1;
  }
 });
 comicsView.setHasFixedSize(true);
 comicsView.setLayoutManager(layoutManager);
 initializeAdapter();
 comicsView.setAdapter(comicDetailsAdapter);
}

代码示例来源:origin: HotBitmapGG/bilibili-android-client

@Override
 public int getSpanSize(int position) {
  return (0 == position) ? mGridLayoutManager.getSpanCount() : 1;
 }
});

代码示例来源:origin: facebook/litho

@Override
public int findLastVisibleItemPosition() {
 return mGridLayoutManager.findLastVisibleItemPosition();
}

代码示例来源:origin: bumptech/glide

@Override
public View onCreateView(
  @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View result = inflater.inflate(R.layout.recycler_view, container, false);
 recyclerView = (RecyclerView) result.findViewById(R.id.recycler_view);
 GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
 layoutManager.setOrientation(RecyclerView.HORIZONTAL);
 recyclerView.setLayoutManager(layoutManager);
 recyclerView.setHasFixedSize(true);
 return result;
}

代码示例来源:origin: facebook/litho

@Override
public int approximateRangeSize(
  int firstMeasuredItemWidth,
  int firstMeasuredItemHeight,
  int recyclerMeasuredWidth,
  int recyclerMeasuredHeight) {
 final int spanCount = mGridLayoutManager.getSpanCount();
 switch (mGridLayoutManager.getOrientation()) {
  case GridLayoutManager.HORIZONTAL:
   final int colCount = (int) Math.ceil(
     (double) recyclerMeasuredWidth / (double) firstMeasuredItemWidth);
   return colCount * spanCount;
  default:
   final int rowCount = (int) Math.ceil(
     (double) recyclerMeasuredHeight / (double) firstMeasuredItemHeight);
   return rowCount * spanCount;
 }
}

代码示例来源:origin: HotBitmapGG/bilibili-android-client

@Override
public void initRecyclerView() {
  mLiveAppIndexAdapter = new LiveAppIndexAdapter(LiveAppIndexActivity.this);
  mRecyclerView.setAdapter(mLiveAppIndexAdapter);
  GridLayoutManager layout = new GridLayoutManager(LiveAppIndexActivity.this, 12);
  layout.setOrientation(LinearLayoutManager.VERTICAL);
  layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
      return mLiveAppIndexAdapter.getSpanSize(position);
    }
  });
  mRecyclerView.setLayoutManager(layout);
}

代码示例来源:origin: naman14/Timber

private void setLayoutManager() {
  if (isGrid) {
    layoutManager = new GridLayoutManager(getActivity(), 2);
  } else {
    layoutManager = new GridLayoutManager(getActivity(), 1);
  }
  recyclerView.setLayoutManager(layoutManager);
}

代码示例来源:origin: jdsjlzx/LRecyclerView

GridLayoutManager manager = new GridLayoutManager(this, 2);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.addItemDecoration(SpacesItemDecoration.newInstance(spacing, spacing, manager.getSpanCount(), Color.GRAY));

代码示例来源:origin: dongjunkun/GanK

public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) {
  innerAdapter.onAttachedToRecyclerView(recyclerView);
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  if (layoutManager instanceof GridLayoutManager) {
    final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
    final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
      @Override
      public int getSpanSize(int position) {
        return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
      }
    });
    gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
  }
}

代码示例来源:origin: iMeiji/Toutiao

int spanCount = ((GridLayoutManager) manager).getSpanCount();
int spanCount = gridLayoutManager.getSpanCount();
  if (gridLayoutManager.findLastVisibleItemPosition() == getItemCount() - 1) {
      int firstVisiblePostion = gridLayoutManager.findFirstVisibleItemPosition();
      if (firstVisiblePostion == 0) {
        if (gridLayoutManager.findFirstCompletelyVisibleItemPosition() != 0) {
          int offset = (-recyclerView.getChildAt(0).getTop()) - recyclerView.getPaddingTop();
          targetY += offset;
if (currentPosition == gridLayoutManager.findLastVisibleItemPosition()
    && (currentPosition - mMyChannelItems.size() - COUNT_PRE_OTHER_HEADER) % spanCount != 0
    && (targetPosition - COUNT_PRE_MY_HEADER) % spanCount != 0) {

代码示例来源:origin: janishar/PlaceHolderView

public <T extends GridLayoutManager>PlaceHolderViewBuilder setLayoutManager(T layoutManager, final int spanSizeLookup){
  mLayoutManager = layoutManager;
  ((GridLayoutManager)mLayoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
      return spanSizeLookup;
    }
  });
  mRecyclerView.setLayoutManager(mLayoutManager);
  return this;
}

代码示例来源:origin: yqritc/RecyclerView-FlexibleDivider

/**
 * Determines whether divider was already drawn for the row the item is in,
 * effectively only makes sense for a grid
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return true if the divider can be skipped as it is in the same row as the previous one.
 */
private boolean wasDividerAlreadyDrawn(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.getSpanIndex(position, spanCount) > 0;
  }
  return false;
}

代码示例来源:origin: huxq17/XRefreshView

private BaseRecyclerAdapter getRecyclerApdater(RecyclerView recyclerView) {
  BaseRecyclerAdapter adapter = null;
  if (recyclerView.getAdapter() instanceof BaseRecyclerAdapter) {
    adapter = (BaseRecyclerAdapter) recyclerView.getAdapter();
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager != null && layoutManager instanceof GridLayoutManager) {
      GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
      gridLayoutManager.setSpanSizeLookup(new XSpanSizeLookup(adapter, gridLayoutManager.getSpanCount()));
    }
    adapter.insideEnableFooter(mParent.getPullLoadEnable());
    initFooterCallBack(adapter, mParent);
  }
  return adapter;
}

代码示例来源:origin: zhihu/Matisse

public void refreshSelection() {
  GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
  int first = layoutManager.findFirstVisibleItemPosition();
  int last = layoutManager.findLastVisibleItemPosition();
  if (first == -1 || last == -1) {
    return;
  }
  Cursor cursor = getCursor();
  for (int i = first; i <= last; i++) {
    RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(first);
    if (holder instanceof MediaViewHolder) {
      if (cursor.moveToPosition(i)) {
        setCheckStatus(Item.valueOf(cursor), ((MediaViewHolder) holder).mMediaGrid);
      }
    }
  }
}

代码示例来源:origin: zaaach/CityPicker

HotViewHolder(View itemView) {
    super(itemView);
    mRecyclerView = itemView.findViewById(R.id.cp_hot_list);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new GridLayoutManager(itemView.getContext(),
        GridListAdapter.SPAN_COUNT, LinearLayoutManager.VERTICAL, false));
    int space = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.cp_grid_item_space);
    mRecyclerView.addItemDecoration(new GridItemDecoration(GridListAdapter.SPAN_COUNT,
        space));
  }
}

代码示例来源:origin: HotBitmapGG/bilibili-android-client

@Override
protected void initRecyclerView() {
  mLiveAppIndexAdapter = new LiveAppIndexAdapter(getActivity());
  mRecyclerView.setAdapter(mLiveAppIndexAdapter);
  GridLayoutManager layout = new GridLayoutManager(getActivity(), 12);
  layout.setOrientation(LinearLayoutManager.VERTICAL);
  layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
      return mLiveAppIndexAdapter.getSpanSize(position);
    }
  });
  mRecyclerView.setLayoutManager(layout);
}

代码示例来源:origin: yqritc/RecyclerView-FlexibleDivider

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_sample);
  SimpleAdapter adapter = new SimpleAdapter(this);
  GridLayoutManager manager = new GridLayoutManager(this, 3);
  manager.setOrientation(OrientationHelper.VERTICAL);
  RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recyclerview);
  recyclerView.setLayoutManager(manager);
  recyclerView.setAdapter(adapter);
  recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
}

代码示例来源:origin: facebook/litho

@Override
public int computeWrappedHeight(int maxHeight, List<ComponentTreeHolder> componentTreeHolders) {
 final int itemCount = componentTreeHolders.size();
 final int spanCount = mGridLayoutManager.getSpanCount();
 int measuredHeight = 0;
 switch (mGridLayoutManager.getOrientation()) {
  case GridLayoutManager.VERTICAL:
   for (int i = 0; i < itemCount; i += spanCount) {
    final ComponentTreeHolder holder = componentTreeHolders.get(i);
    int firstRowItemHeight = holder.getMeasuredHeight();
    measuredHeight += firstRowItemHeight;
    measuredHeight += LayoutInfoUtils.getTopDecorationHeight(mGridLayoutManager, i);
    measuredHeight += LayoutInfoUtils.getBottomDecorationHeight(mGridLayoutManager, i);
    if (measuredHeight > maxHeight) {
     measuredHeight = maxHeight;
     break;
    }
   }
   return measuredHeight;
  case GridLayoutManager.HORIZONTAL:
  default:
   throw new IllegalStateException(
     "This method should only be called when orientation is vertical");
 }
}

代码示例来源:origin: Aspsine/SwipeToLoadLayout

@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  if (layoutManager instanceof GridLayoutManager) {
    final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
      @Override
      public int getSpanSize(int position) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (isFullSpanType(adapter.getItemViewType(position))) {
          return gridLayoutManager.getSpanCount();
        }
        return 1;
      }
    });
  }
}

代码示例来源:origin: yqritc/RecyclerView-FlexibleDivider

/**
 * Returns a group index for GridLayoutManager.
 * for LinearLayoutManager, always returns position.
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return group index of items
 */
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;
}

相关文章