RecyleView 一個數據佔據多個item
阿新 • • 發佈:2018-11-10
private RecyclerView mRecylerView; private GriViewAdapter mAdapter; private GridLayoutManager gridLayoutManager;
mRecylerView.setAdapter(mAdapter); mRecylerView.setNestedScrollingEnabled(false); 設定一行顯示的數目18 gridLayoutManager = new GridLayoutManager(getContext(), 18, GridLayoutManager.VERTICAL, false); mRecylerView.setLayoutManager(gridLayoutManager); 根據資料型別顯示佔據的item個數 gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { int type = mAdapter.getItemViewType(position); switch (type) { case GriViewAdapter.ITEM_VIEW_TYPE_0: return 1; case GriViewAdapter.ITEM_VIEW_TYPE_1: return 2; } return 1; } }); 設定間距 HashMap<String, Integer> stringIntegerHashMap = new HashMap<String, Integer>(); stringIntegerHashMap.put(SpaceItemDecoration.TOP, 0); stringIntegerHashMap.put(SpaceItemDecoration.BOTTOM, 4); stringIntegerHashMap.put(SpaceItemDecoration.LEFT, 1); stringIntegerHashMap.put(SpaceItemDecoration.RIGHT, 1); mRecylerView.addItemDecoration(new SpaceItemDecoration(stringIntegerHashMap));
adapter:
@Override public void onBindViewHolder(ViewHolder holder, final int position) { int type = getItemViewType(position); switch (type) { case ITEM_VIEW_TYPE_0: break; case ITEM_VIEW_TYPE_1: break; } } @Override public int getItemViewType(int position) { if (position < mKeyList.size() - 1) { return ITEM_VIEW_TYPE_0; } else { return ITEM_VIEW_TYPE_1; } }