Search code examples
javaandroidviewandroid-galleryandroid-5.0-lollipop

Android recycle view like a gallery view?


I am trying to create a recycleview that works exactly like a Gallery view where when you are scrolling horizontally it will lock the current viewed item centraly like in a gallery view?

Is this possible?

So far I have created my recycleView and used a LinearLayoutManager set as Horizontal in order for it to scroll horizonally(works fine) but how do I lock the current item like in a gallery view?

   recyclerView = (RecyclerView) view.findViewById(R.id.recycleView);

        MyAdapter adapter = new MyMapAdapter();

        recyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(mLayoutManager);

Solution

  • RecyclerView provides these built-in layout managers:

    • LinearLayoutManager shows items in a vertical or horizontal scrolling list.
    • GridLayoutManager shows items in a grid.
    • StaggeredGridLayoutManager shows items in a staggered grid.

    Source: https://developer.android.com/training/material/lists-cards.html#RecyclerView

    GridLayoutManager:

        GridLayoutManager mLayoutManager = new GridLayoutManager(this, 4); // (Context context, int spanCount)
        mRecyclerView.setLayoutManager(mLayoutManager);
    

    StaggeredGridLayoutManager:

        StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL); // (int spanCount, int orientation)
        mRecyclerView.setLayoutManager(mLayoutManager);