简体   繁体   English

如何在Android中像谷歌一样进行水平滚动?

[英]how to make horizontal scrolling like google play in android?

i've tried to make Horizontal Scrolling like google play.Like this : 我试图像谷歌一样进行水平滚动。就像这样:

Google Play滚动

it scroll something like ViewPager . 它会像ViewPager一样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. 但是,当我实现RecyclerView和Horizo​​ntal LineararLayout管理器,但滚动顺利但不喜欢谷歌播放。 Code has been given bellow and can anyone help me to make the scrolling exactly like google play scrolling ? 代码已经给出了,任何人都可以帮助我使滚动完全像谷歌播放滚动?

Recyclerview Declaration : Recyclerview宣言:

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

    }
}

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. 如果需要更多自定义,则需要扩展LinearSnapHelperPagerSnapHelper并覆盖calculateDistanceToFinalSnap方法。

RecyclerView usually used to show the list or any collections. RecyclerView通常用于显示列表或任何集合。 Recycler View has an own scroll behaviour vertically or horizontally. Recycler View具有垂直或水平的滚动行为。 For this, we have to define LayoutManager for layout behaviour. 为此,我们必须为布局行为定义LayoutManager。 Here I am giving an example how RecyclerView declares and add layout manager: 在这里,我将举例说明RecyclerView如何声明并添加布局管理器:

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. 现在RecyclerView将以水平方式滚动。 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 snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rvBotCollection);

This two line will do the magic. 这两行将起到魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM