@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (getActionBar() != null) { getActionBar().setDisplayHomeAsUpEnabled(true); } String[] projection = new String[] {MediaStore.MediaColumns.DATA}; mGalleryCursor = getContentResolver() .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); GridLayoutManager glm = new GridLayoutManager(this, COUNT_ITEMS_IN_LINE); mRecyclerView.setLayoutManager(glm); mRecyclerView.setAdapter(new MyRecyclerAdapter(this, mGalleryCursor, glm.getSpanCount())); }
public void setLayoutManager(LayoutManager layoutManager) { super.setLayoutManager(layoutManager); if (mWrapperAdapter != null) { if (layoutManager instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; final int spanCount = gridLayoutManager.getSpanCount(); gridLayoutManager.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (mWrapperAdapter.isHeader(position) || mWrapperAdapter.isFooter(position) || mWrapperAdapter.isLoadMoreView(position)) || mWrapperAdapter.isPullRefreshView(position) ? spanCount : 1; } }); } } }