RecyclerView.Adapter中的多卡服务器调用

时间:2018-12-11 19:45:31

标签: android android-recyclerview retrofit

在这里我很痛苦,因此对于RecyclerView中创建的每个元素,我都会在内部创建一个Chip元素列表。您知道的,我做到了,但是现在每次RecyclerView滚动时,我都会折磨我的服务器。如何做到这一点,以免每次滚动时都拍摄服务器?在我的情况下,当我调用productRepository getToastFromProduct方法时发生服务器调用

package com.example.myapplication.Adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.myapplication.Application.CloudLiquid;
import com.example.myapplication.DataModel.ChipModel;
import com.example.myapplication.DataModel.ProductLayout.Information;
import com.example.myapplication.DataModel.ProductLayout.Product;
import com.example.myapplication.Managers.UuidManager;
import com.example.myapplication.R;
import com.example.myapplication.Retrofit.ProductRepository;
import com.example.myapplication.Retrofit.RatingRepository;
import com.example.myapplication.Retrofit.RetrofitBase;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import com.google.android.material.internal.FlowLayout;
import com.xiaofeng.flowlayoutmanager.FlowLayoutManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import co.lujun.androidtagview.TagContainerLayout;
import co.lujun.androidtagview.TagView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Holder> {

    private List<com.example.myapplication.DataModel.ProductLayout.Product> products;
    private List<com.example.myapplication.DataModel.ProductLayout.Product> filteredList;
    private Context context;
    private RatingRepository rating;
    private CloudLiquid application;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    private ProductRepository productRepository;

    public ProductAdapter(List<com.example.myapplication.DataModel.ProductLayout.Product> products, Context context) {
        this.products = products;
        this.context = context;
        this.filteredList = products;
        this.rating = RetrofitBase.getRetrofit().create(RatingRepository.class);
        this.application = (CloudLiquid) context;
        this.productRepository = RetrofitBase.getRetrofit().create(ProductRepository.class);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itempreview, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, final int position) {
        Glide.with(context).
                load(filteredList.get(position).getImage()).
                apply(new RequestOptions().override(512, 512)).
                into(holder.image);
        holder.product.setText(String.valueOf(filteredList.get(position).getName()));
        holder.company.setText(String.valueOf(filteredList.get(position).getManufacturer()));
        holder.comment.setText(String.valueOf(filteredList.get(position).getCommentCnt()));
        holder.like.setText(String.valueOf(filteredList.get(position).getLike()));
        holder.dislike.setText(String.valueOf(filteredList.get(position).getDislike()));
        compositeDisposable.add(productRepository.getToastFromProduct(String.valueOf(products.get(position).getId()))
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<List<ChipModel>>() {
            @Override
            public void accept(List<ChipModel> chipModels) throws Exception {
                holder.category.removeAllViews();
                for (int i = 0; i < chipModels.size(); i++){
                    final Chip chip = new Chip(context);
                    chip.setChipBackgroundColor(context.getResources().getColorStateList(R.color.ms_black));
                    chip.setText(chipModels.get(i).getChipText());
                    chip.setTextColor(context.getResources().getColor(R.color.ms_white));
                    Glide.with(context)
                            .load(chipModels.get(i).getChipImage())
                            .into(new SimpleTarget<Drawable>() {
                                @Override
                                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                  chip.setChipIcon(resource);
                                }
                            });
                    holder.category.addView(chip);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                throwable.printStackTrace();
            }
        }));
        holder.commentImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CloudLiquid application = (CloudLiquid) context;
                Bundle bundle = new Bundle();
                bundle.putString("product_id", String.valueOf(filteredList.get(position).getId()));
                application.showCommentFragmet(bundle);
            }
        });

        holder.card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CloudLiquid application = (CloudLiquid) context;
                Bundle bundle = new Bundle();
                bundle.putString("id", String.valueOf(filteredList.get(position).getId()));
                application.showItemProfile(bundle);
            }
        });
        holder.likeimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.compositeDisposable.add(rating.addRatingToProduct("Like", new UuidManager(context).getUuid(), String.valueOf(products.get(position).getId()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Product>() {
                            @Override
                            public void accept(Product product) throws Exception {
                                products.remove(position);
                                products.add(position, product);
                                notifyItemChanged(position);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                throwable.printStackTrace();
                            }
                        }));
            }
        });
        holder.dislikeimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.compositeDisposable.add(rating.addRatingToProduct("Dislike", new UuidManager(context).getUuid(), String.valueOf(products.get(position).getId()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Product>() {
                            @Override
                            public void accept(Product product) throws Exception {
                                products.remove(position);
                                products.add(position, product);
                                notifyItemChanged(position);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                throwable.printStackTrace();
                            }
                        }));
            }
        });
    }


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


    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
    }

    class Holder extends RecyclerView.ViewHolder {
        private AppCompatImageView image;
        private AppCompatTextView product, company, like, dislike, comment;
        private AppCompatImageView commentImage, dislikeimage, likeimage;
        private ChipGroup category;
        private MaterialCardView card;
        private CompositeDisposable compositeDisposable = new CompositeDisposable();

        public Holder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.appCompatImageView4);
            commentImage = itemView.findViewById(R.id.appCompatImageView5);
            product = itemView.findViewById(R.id.appCompatTextView6);
            company = itemView.findViewById(R.id.appCompatTextView4);
            like = itemView.findViewById(R.id.appCompatTextView5);
            dislike = itemView.findViewById(R.id.appCompatTextView3);
            comment = itemView.findViewById(R.id.appCompatTextView);
            category = itemView.findViewById(R.id.parentlinear);
            card = itemView.findViewById(R.id.itempreview);
            dislikeimage = itemView.findViewById(R.id.appCompatImageView6);
            likeimage = itemView.findViewById(R.id.appCompatImageView7);
        }

        @Override
        public String toString() {
            return super.toString();
        }
    }

}

0 个答案:

没有答案