Android --- GridLayoutManager 设置了 item 均匀分布,但是无效
生活随笔
收集整理的這篇文章主要介紹了
Android --- GridLayoutManager 设置了 item 均匀分布,但是无效
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
檢查你的 item 布局的最外層是否是
android:layout_width="match_parent"附加均勻分布的方法
// 創建布局管理
GridLayoutManager manager = new GridLayoutManager(getContext(),2,GridLayoutManager.VERTICAL,false);rv_organ.addItemDecoration(new SpaceItemDecoration(0,2));rv_organ.setLayoutManager(manager);public class SpaceItemDecoration extends RecyclerView.ItemDecoration {private int leftRight;private int topBottom;// leftRight 為橫向間的距離 topBottom為縱向間距離public SpaceItemDecoration(int leftRight, int topBottom) {this.leftRight = leftRight;this.topBottom = topBottom;}@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDraw(c, parent, state);}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();// 豎直方向的if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {// 最后一項需要 bottomif (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {outRect.bottom = topBottom;}outRect.top = topBottom;outRect.left = leftRight;outRect.right = leftRight;} else {// 最后一項需要 rightif (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {outRect.right = leftRight;}outRect.top = topBottom;outRect.left = leftRight;outRect.bottom = topBottom;}}}總結
以上是生活随笔為你收集整理的Android --- GridLayoutManager 设置了 item 均匀分布,但是无效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android --- 怎么设置 Edi
- 下一篇: Android——Spinner 怎么禁