Zah*_*lam 9 android android-viewpager android-recyclerview
我试图像谷歌一样进行水平滚动.就像这样:
它滚动的东西就像ViewPager.只关注左侧的一个项目.但是当我实现RecyclerView和Horizontal 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);
Run Code Online (Sandbox Code Playgroud)
适配器布局:
<?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>
Run Code Online (Sandbox Code Playgroud)
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());
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
支持库现在包含用于此行为的两个不同的类。
LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(recyclerView);
Run Code Online (Sandbox Code Playgroud)
就是这样。
如果您想要更多自定义,您需要扩展LinearSnapHelperorPagerSnapHelper和重写calculateDistanceToFinalSnap方法。
RecyclerView通常用于显示列表或任何集合。Recycler View 有自己的垂直或水平滚动行为。为此,我们必须为布局行为定义 LayoutManager。这里我举一个例子,RecyclerView是如何声明和添加布局管理器的:
RecyclerView rvBotCollection;
rvBotCollection =(RecyclerView)itemView.findViewById(R.id.rvCollectionList);
Run Code Online (Sandbox Code Playgroud)
然后将添加一个适配器以通过项目视图显示整个列表。现在我们可以让它水平滚动,如下所示:
rvBotCollection.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
Run Code Online (Sandbox Code Playgroud)
现在RecyclerView将以水平方式滚动。但主要问题是视图一次会滚动多个项目。使用 SnapHelper 我们可以像这样一次滚动单个项目:
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rvBotCollection);
Run Code Online (Sandbox Code Playgroud)
这两行就会发挥魔力。
| 归档时间: |
|
| 查看次数: |
2063 次 |
| 最近记录: |