如何使用自定义Recycler适配器创建封闭循环Recyclerview?

时间:2016-06-13 08:21:52

标签: java android android-recyclerview

这可能是重复的,但仍然没有正确的答案。  我提到了thisthis以及this

如链接中所述,我希望实施循环的回收视图,即

[查看1] - [查看2] ....- [查看N-1] - [查看N] - [查看1] .....等等

由于没有覆盖方法来获取View并在recyclerview中获取Item,我 我无法成功。

请帮忙。谢谢!

我的回收商适配器代码

public class HorizontalRecyclerAdapter extends RecyclerView.Adapter<HorizontalRecyclerAdapter.ProductViewHolder> {

    List<Product> products;
    private Context mContext;
    ImageLoader imageLoader;


    HorizontalRecyclerAdapter(List<Product> products, Context mContext) {
        this.products = products;
        this.mContext = mContext;

    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_layout, parent, false);
        return new ProductViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ProductViewHolder holder, int position) {

        imageLoader = SingletonRequestQueue.getInstance(mContext).getImageLoader();
        String URL = products.get(position).getProductImageUrl();
        holder.progressBar.setVisibility(View.VISIBLE);
        /* to hide the progress bar after image response */

        imageLoader.get(URL, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response != null) {
                    Bitmap bitmap = response.getBitmap();
                    if (bitmap != null) {
                        holder.progressBar.setVisibility(View.GONE);
                    }
                }
            }
            @Override
            public void onErrorResponse(VolleyError error) {}
        });
        holder.itemImage.setImageUrl(URL, imageLoader);
        holder.itemName.setText(products.get(position).getProductName());
        holder.itemPrice.setText("₹ "+products.get(position).getProductPrice());
        holder.sellerLogo.setImageResource(products.get(position).getProductSellerId());

    }

    @Override
    public int getItemCount() {
        return products.size();
    }

    public static class ProductViewHolder extends RecyclerView.ViewHolder {

        NetworkImageView itemImage;
        NetworkImageView sellerLogo;
        TextView itemName;
        TextView itemPrice;
        ProgressBar progressBar;


        public ProductViewHolder(View itemView) {
            super(itemView);
            itemImage = (NetworkImageView) itemView.findViewById(R.id.product_image);
            sellerLogo = (NetworkImageView) itemView.findViewById(R.id.product_seller);
            itemName = (TextView) itemView.findViewById(R.id.product_name);
            itemPrice = (TextView) itemView.findViewById(R.id.product_price);
            progressBar = (ProgressBar)itemView.findViewById(R.id.network_image_progressbar);


        }
    }
}

2 个答案:

答案 0 :(得分:5)

我深深地看了一眼,找到了一个有效的解决方案/ hack here。我已经测试了它并且效果很好。我正在关闭这个问题。谢谢!

答案 1 :(得分:0)

检查我的回答here。我尝试过,它适用于回收站视图,并从最终创建无限滚动效果。