10

i've tried to make Horizontal Scrolling like google play.Like this :

Google Play Scrolling

it scroll something like ViewPager. Only focus one item from the left. But when i implement the RecyclerView and Horizontal LineararLayout manager , but scroll smoothly but not like google play. Code has been given bellow and can anyone help me to make the scrolling exactly like google play scrolling ?

Recyclerview Declaration :

RecyclerView nowShowingMovies;

.......

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
nowShowingMovies.setLayoutManager(layoutManager);
nowShowingMovies.setHasFixedSize(true);

NowShowingAdapter adapter = new NowShowingAdapter(getActivity());
nowShowingMovies.setAdapter(adapter);

Adapter Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:clickable="true"
        android:paddingRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/movieImage"
            android:layout_width="144dp"
            android:layout_height="200dp"/>

    </LinearLayout>

</LinearLayout>

AdapterClass :

public class NowShowingAdapter extends RecyclerView.Adapter<NowShowingAdapter.NowShowingViewHolder> {

    MovieListClick movieListClick;

    ArrayList<MovieListModel> movieListModel;
    int movieImageId[] = new int[]{
        R.drawable.kubo_image,
        R.drawable.batman1,
        R.drawable.jugnle_1,
        R.drawable.kanfu_1,
        R.drawable.peanuts,
        R.drawable.sweetheart
    };
    Context context;

    public NowShowingAdapter(Context context){
        this.context = context;
    }

    @Override
    public NowShowingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.movie_for_list,null,false);
        return new NowShowingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NowShowingViewHolder holder, int position) {
        holder.movieImage.setImageResource(movieImageId[position]);
    }

    public void setOnMovieClickListener(MovieListClick movieListClick){
        this.movieListClick = movieListClick;
    }

    @Override
    public int getItemCount() {
        return movieImageId.length;
    }

    public class NowShowingViewHolder extends RecyclerView.ViewHolder {

        public ImageView movieImage;
        public NowShowingViewHolder(View itemView) {
            super(itemView);
            movieImage = (ImageView) itemView.findViewById(R.id.movieImage);
            movieImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    movieListClick.onMovieClick(getLayoutPosition());
                }
            });
        }

    }
}
Zahidul Islam
  • 3,006
  • 1
  • 22
  • 33
  • i have a problem like this before try this link [Use RecyclerView inside ScrollView with flexible Recycler item height](http://stackoverflow.com/questions/32420770/use-recyclerview-inside-scrollview-with-flexible-recycler-item-height) – Ashkan Jul 17 '16 at 12:58
  • what do you mean? snap left most item to the edge? – Hamed Ghadirian Jul 28 '16 at 07:17
  • Yes. I found something which need to check the gesture, but if there any buildin function it will be helpful. – Zahidul Islam Jul 28 '16 at 07:20

2 Answers2

4

The support library now includes two different classes for this behavior.

LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView);

That's it.

If you want more customization you need to extend the LinearSnapHelper or PagerSnapHelper and override calculateDistanceToFinalSnap method.

M. Reza Nasirloo
  • 15,302
  • 2
  • 23
  • 39
1

RecyclerView usually used to show the list or any collections. Recycler View has an own scroll behaviour vertically or horizontally. For this, we have to define LayoutManager for layout behaviour. Here I am giving an example how RecyclerView declares and add layout manager:

RecyclerView rvBotCollection;
rvBotCollection =(RecyclerView)itemView.findViewById(R.id.rvCollectionList);

Then an adapter will add to show the whole list with item view. Now we can make it horizontally scroll-able like this:

rvBotCollection.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

Now RecyclerView will scroll in a horizontal way. But the main problem is view will scroll at a time multiple items. Using SnapHelper we can make single item scroll at a time like this way:

SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rvBotCollection);

This two line will do the magic.

Ali Ahmed
  • 918
  • 1
  • 7
  • 17