Single Column Horizintal Scroll Grid with a RecyclerView Android

695 views Asked by At

I am working on a music player app where in the artist details activity, I have used a RecyclerView to display the artist bio, albums by the artist and the songs by the artist, present on the device. The Albums are in a Grid done using a GridLayoutManager.

The Problem is that I want the Album Grid to be a single column grid with horizontal scroll. And I can't seem to figure out a way to do it using RecyclerView viewholder

Here is the GridLayoutManager :

final GridLayoutManager layoutManager = new GridLayoutManager(this, numColumns);
    GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {

            if (adapter.getItemViewType(position) == Adapter.ALBUM_INSTANCE) return 1;
            else return numColumns;
        }
    };

    spanSizeLookup.setSpanIndexCacheEnabled(true); 


    layoutManager.setSpanSizeLookup(spanSizeLookup);
    recyclerView.setLayoutManager(layoutManager);}

And the onCreateViewHolder method

 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case BIO_VIEW:
                return new BioViewHolder(LayoutInflater.from(ArtistActivity.this).inflate(R.layout.instance_artist_bio, parent, false), this);
            case HEADER_VIEW:
                return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.subheader, parent, false));
            case ALBUM_INSTANCE:
                return new AlbumViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.instance_album, parent, false));
            case SONG_INSTANCE:
                SongViewHolder viewHolder = new SongViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.instance_song, parent, false));
                viewHolder.setSongList(songs);
                return viewHolder;
            default:
                return null;
        }
    }

The result is that after the artist Bio, the album section inflates as a GridView with multiple rows, depending on the no of albums. However I want them to be in a single row, and horizontally scrollable.

How do I do this?

0

There are 0 answers