【问题标题】:how to make horizontal scrolling like google play in android?如何在android中像google play一样进行水平滚动?
【发布时间】:2016-07-17 12:43:27
【问题描述】:

我尝试让 水平滚动 像 google play 一样。像这样:

它滚动类似ViewPager。只关注左侧的一项。 但是当我实现RecyclerView 和Horizo​​ntal LineararLayout 管理器时,滚动流畅但不像谷歌播放。代码已在下面给出,任何人都可以帮助我使滚动完全像谷歌播放滚动?

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

适配器布局:

<?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>

适配器类:

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

    }
}

【问题讨论】:

标签: android android-viewpager android-recyclerview


【解决方案1】:

支持库现在包含两个用于此行为的不同类。

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

就是这样。

如果您想要更多自定义,您需要扩展 LinearSnapHelper 或 PagerSnapHelper 并覆盖 calculateDistanceToFinalSnap 方法。

【讨论】:

    【解决方案2】:

    RecyclerView 通常用于显示列表或任何集合。 Recycler View 有自己的垂直或水平滚动行为。为此,我们必须为布局行为定义 LayoutManager。这里我举个例子 RecyclerView 如何声明和添加布局管理器:

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

    然后适配器将添加以显示带有项目视图的整个列表。现在我们可以让它像这样水平滚动:

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

    现在 RecyclerView 将以水平方式滚动。但主要问题是视图会一次滚动多个项目。使用 SnapHelper,我们可以像这样一次滚动单个项目:

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

    这两行会变魔术。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2015-09-20
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多