前言:
使用setSpanSizeLookup后的效果图如下
对应的Activity代码如下,setSpanSizeLookup的具体解释已经在代码中给出:
public class GrideActivity extends AppCompatActivity implements GrideAdapter.OnItemClickListener { private ActivityGrideBinding binding; private GrideAdapter grideAdapter; private List<GrideModel> grideModels = new ArrayList<>(); private static final String TAG = "GrideActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_gride); GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3, RecyclerView.VERTICAL, false); binding.rvList.setLayoutManager(gridLayoutManager); for (int i = 0; i < 20; i++) { grideModels.add(new GrideModel("https://tse3-mm.cn.bing.net/th/id/OIP-C.ezHUw2d5cvio6idA9t5zCwHaEK?w=302&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7", "Android开发")); } grideAdapter = new GrideAdapter(grideModels); grideAdapter.setOnItemClickListener(this); gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) {//列表项的占位规则 Log.i(TAG, "getSpanSize,position:" + position); //返回该item所占的份数 那么下标为4的item占三份,那么则占据整个屏幕的宽。 //注意一个item所占的份数,不能超过GridLayoutManager的spanCount return (1 + position) % 7 == 0 ? 3 : 1; } }); binding.rvList.setAdapter(grideAdapter); } @Override public void onItemClick(int position) { ToastUtils.showShort(position); } }