我尝试将图像滑块横幅放在我的android应用顶部。及其工作正常。但是现在我想这样。我在RecyclerView
中放入了5张图片或项目。当用户到达第五项并再次向左滑动时,第一项再次出现。意味着我想要无尽的RecyclerView。或者可以说我想要360度RecyclerView。
您可以看到Flipkart android应用示例。
这是我的代码:
recyclerViewHeaderSlider = view.findViewById(R.id.bannerSlider);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerViewHeaderSlider);
recyclerViewHeaderSlider.setHasFixedSize(true);
recyclerViewHeaderSlider.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
recyclerViewHeaderSlider.setAdapter(headerSliderAdapter);
适配器类:
public class HeaderSliderAdapter extends RecyclerView.Adapter<HeaderSliderAdapter.ViewHolder> {
public List<Banner> banners;
public Context context;
public HeaderSliderAdapter(Context context, List<Banner> banners) {
this.banners = banners;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_header, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (TextUtils.isEmpty(banners.get(position).getmSliderImage())) {
holder.mSliderImageView.setImageResource(R.drawable.app_logo);
} else {
Picasso.with(context)
.load(banners.get(position).getmSliderImage())
.fit()
.into(holder.mSliderImageView, new Callback() {
@Override
public void onSuccess() {
holder.mBackgroundImage.setVisibility(View.GONE);
}
@Override
public void onError() {
holder.mBackgroundImage.setVisibility(View.VISIBLE);
}
});
}
}
@Override
public int getItemCount() {
return banners.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView mSliderImageView,mBackgroundImage;
View mView;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mSliderImageView = mView.findViewById(R.id.mSliderImage);
mBackgroundImage = mView.findViewById(R.id.backgroundImage);
}
}
}