getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { //access view holder val viewHolder = parent.getChildViewHolder(view) val params = view.layoutParams as GridLayoutManager.LayoutParams val layoutManager = parent.layoutManager as GridLayoutManager //how much space view is occupied val spanSize = params.spanSize //number of spans in the grid val totalSpanSize = layoutManager.spanCount //position depends on the RTL val position = params.spanIndex ... }
getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) //access view holder val viewHolder = parent.getChildViewHolder(view) val params = view.layoutParams as GridLayoutManager.LayoutParams val layoutManager = parent.layoutManager as GridLayoutManager //how much space view is occupied val spanSize = params.spanSize //number of spans in the grid val totalSpanSize = layoutManager.spanCount //position depends on the RTL val position = params.spanIndex … }
getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) //access view holder val viewHolder = parent.getChildViewHolder(view) val params = view.layoutParams as GridLayoutManager.LayoutParams val layoutManager = parent.layoutManager as GridLayoutManager //how much space view is occupied val spanSize = params.spanSize //number of spans in the grid val totalSpanSize = layoutManager.spanCount //position depends on the RTL val position = params.spanIndex ... }
span size of each item? layouManager.spanSizeLookup = GridLayoutManager.SpanSizeLookup() { override fun getSpanSize(position: Int): Int { return (...) } }
childCount = parent.childCount val manager = parent.layoutManager for (i in 0..childCount - 1) { val child = parent.get(i) val right = manager.getDecoratedRight(child) val left = manager.getDecoratedLeft(child) + paddingLeft val height = divider.intrinsicHeight ?: 0 val top = manager.getDecoratedBottom(child) val bottom = top + height divider.setBounds(left, top, right, bottom) divider.draw(c) } }
childCount = parent.childCount val manager = parent.layoutManager for (i in 0..childCount - 1) { val child = parent.get(i) val right = manager.getDecoratedRight(child) val left = manager.getDecoratedLeft(child) + paddingLeft val height = divider.intrinsicHeight val top = manager.getDecoratedBottom(child) val bottom = top + height divider.setBounds(left, top, right, bottom) divider.draw(c) } }
VIEW_TYPE_NORMAL = 0; private static final int VIEW_TYPE_SUBHEADER = 1; private static final int VIEW_TYPE_SEPARATOR = 2; private static final int VIEW_TYPE_HEADER = 3; }
int viewType) { switch (viewType) { case VIEW_TYPE_NORMAL: return new NormalViewHolder(mLayoutInflater, parent, mOnClickListener); case VIEW_TYPE_SUBHEADER: return new SubheaderViewHolder(mLayoutInflater, parent); case VIEW_TYPE_SEPARATOR: return new SeparatorViewHolder(mLayoutInflater, parent); case VIEW_TYPE_HEADER: return new HeaderViewHolder(mHeaderLayout); } return null; }