我试图像google play一样进行水平滚动。就像这样:
滚动类似ViewPager
的内容。只关注左侧的一个项目。
但是当我实现RecyclerView
和Horizontal LineararLayout管理器时,但滚动顺畅但不像google play。代码已经过了,任何人都可以帮我制作滚动游戏,就像谷歌播放滚动一样吗?
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>
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());
}
});
}
}
}
答案 0 :(得分:3)
支持库现在包含两种不同的类用于此行为。
x
就是这样。
如果您想要进行更多自定义,则需要扩展LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView);
或LinearSnapHelper
并覆盖PagerSnapHelper
方法。
答案 1 :(得分:1)
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);
这两行将起到神奇作用。