Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make horizontal scrolling like google play in android?

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());
                }
            });
        }

    }
}
like image 639
Zahidul Islam Avatar asked Jul 17 '16 12:07

Zahidul Islam


People also ask

Can scroll horizontally Android?

The android. widget. HorizontalScrollView class provides the functionality of horizontal scroll view. HorizontalScrollView is used to scroll the child elements or views in a horizontal direction.

How do I change horizontal scrolling?

Mouse scrolls horizontally instead of verticallyMove your mouse pointer away from the scroll-bar and then move the mouse wheel. Or else see if you can disable the horizontal scroll-bar. Press the middle mouse button once and then move your pointer to scroll the page vertically.

What is scroll layout Android?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

Is horizontal scrolling good?

Horizontal scrolling saves a lot of vertical screen space. Rather than displaying all the content at once on a very long page, horizontal layouts introduce users to smaller chunks of information. The layout is much more flexible. One can add content in both directions — vertical and horizontal.


2 Answers

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.

like image 82
M. Reza Nasirloo Avatar answered Sep 22 '22 15:09

M. Reza Nasirloo


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.

like image 29
Ali Ahmed Avatar answered Sep 23 '22 15:09

Ali Ahmed