在RecycleView onScrolling图像闪烁

时间:2016-05-26 13:18:01

标签: android

这是我正在使用的适配器的代码。在这里我使用Volley进行图像加载。我的实际问题是每当我试图滚动所有图像时都会闪烁。 所以请建议我修复图像闪烁问题的解决方案。

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
  private Activity activity;

  private String search;
  public List<CategoryItem> catalogsList;
  private Context context;
  private RequestQueue mRequestQueue;
  private SimpleImageLoader newImageLoader;
  private int screenWidth, screenHeight;
  public ImageContainerResize imageContainerResize;
  private Handler mHandler;
  private String parentId;
  private String title;
  private int numFound;
  private OnClick onClick;
  private String categoriesName;
  private boolean isListViewMode;

  public ArrayList<String> getCategorySuggestionList() {
    return categorySuggestionList;
  }

  public void setListViewMode(boolean listViewMode) {
    isListViewMode = listViewMode;
  }

  public void setCategorySuggestionList(ArrayList<String> categorySuggestionList) {
    this.categorySuggestionList = categorySuggestionList;
  }

  private ArrayList<String> categorySuggestionList;

  private boolean isFashionPart;

  public RecycleViewAdapter(Context context, List<CategoryItem> data, String parentID,
      String title) {
    this.context = context;
    this.setSearch("");
    mHandler = new Handler();
    this.catalogsList = data;
    isFashionPart = false;
    newImageLoader = Utils.imageLoader;
    this.parentId = parentID;
    this.title = title;
    imageContainerResize = new ImageContainerResize(context);
    this.categorySuggestionList = new ArrayList<>();
    onClick = (OnClick) context;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View itemView;
    if (1 == viewType) {
      if (isListViewMode) {
        itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.component_list,
            viewGroup, false);
      } else {
        itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_category_c2,
            viewGroup, false);
      }
    } else {
      itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_suggestions,
          viewGroup, false);
    }

    return new ViewHolder(itemView, viewType, isListViewMode);
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
    if (getItemViewType(position) == 1) {
      holder.linearLayout.setVisibility(View.VISIBLE);
      CategoryItem categoryItem = this.catalogsList.get(holder.getAdapterPosition());
      if (!categoryItem.getCatalogs().isStock()) {
        holder.stokHabisTextView.setVisibility(View.VISIBLE);
      }
      if (categoryItem.getCatalogs().isZeroPercentInstallment()) {
        holder.cicilanTextView.setVisibility(View.VISIBLE);
        if (!categoryItem.getCatalogs().isStock()) {
          holder.dividerGridTextView.setVisibility(View.VISIBLE);
        }
      }

      String product = categoryItem.getCatalogs().getName(), productName, newProductName;
      if (product.length() > 35) {
        productName = product.substring(0, 20);
        newProductName = product.substring(product.length() - 15, product.length());
        product = productName + "..." + newProductName;
      }

      holder.newPriceTextView.setText(categoryItem.getCatalogs().getPriceDisplay());
      holder.oldPriceTextView
          .setPaintFlags(holder.oldPriceTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
      Utils.getDisplaySize(activity);
      int widthport = Utils.getWidthDisplayDevice() / 3;
      int widthlands = Utils.getHeightDisplayDevice(activity) / 3;

      String imageUrl = categoryItem.getCatalogs().getImageUrl();
      Log.wtf("imageURL", imageUrl);

      if (context.getResources()
          .getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        imageUrl = imageUrl + Utils.imageCompressionFunction(context, widthport, widthport);
        imageContainerResize.setFadeInImageLoader(newImageLoader, imageUrl, holder.imageView,
            widthport, widthport);
      } else {
        imageUrl = imageUrl + Utils.imageCompressionFunction(context, widthlands, widthlands);
        imageContainerResize.setFadeInImageLoader(newImageLoader, imageUrl, holder.imageView,
            widthlands, widthlands);
      }

      if (categoryItem.getCatalogs().getStrikeThrughtPriceDisplay() != null) {
        holder.oldPriceTextView.setText(categoryItem.getCatalogs().getStrikeThrughtPriceDisplay());
        if (categoryItem.getCatalogs().getDiscount() != 0) {
          holder.newPriceTextView
              .setText(String.format("%s (-%d%%)", categoryItem.getCatalogs().getPriceDisplay(),
                  categoryItem.getCatalogs().getDiscount()));
        } else {
          holder.newPriceTextView.setText(categoryItem.getCatalogs().getPriceDisplay());
        }
        if ("null".equals(categoryItem.getCatalogs().getStrikeThrughtPriceDisplay())) {
          holder.oldPriceTextView.setVisibility(View.GONE);
        }
      }
      // sets the title and the subtitle for the current component
      final SpannableStringBuilder spannableStringBuilder =
          Utils.makeSectionOfTextBold(product, getSearch());
      holder.descriptionTextView.setText(spannableStringBuilder);
      if (categoryItem.getCatalogs().getPriceDisplay().contains("tidak")) {
        holder.newPriceTextView.setText("Not available");
      }
      if (CategoryC1Activity.FASHION_ENABLE) {
        if (isFashionPart) {
          setGravityCenter(holder.descriptionTextView, holder.oldPriceTextView,
              holder.newPriceTextView);
          if (!isListViewMode) {
            holder.relative.setGravity(Gravity.CENTER_HORIZONTAL);
            holder.linearData.setGravity(Gravity.CENTER_HORIZONTAL);
            holder.linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
            holder.stokHabisTextView
                .setTextColor(ContextCompat.getColor(context, R.color.strike_through_price));
            holder.cicilanTextView
                .setTextColor(ContextCompat.getColor(context, R.color.strike_through_price));
          }
        }
      }

      if (categoryItem.getCatalogs().getProductRating() == 0) {
        Utils.hideProductRating(holder.imageViewOne, holder.imageViewTwo, holder.imageViewThree,
            holder.imageViewFour, holder.imageViewFive, holder.textViewReviewCount);
      } else {
        Utils.setRating(categoryItem.getCatalogs(), holder.imageViewOne, holder.imageViewTwo,
            holder.imageViewThree, holder.imageViewFour, holder.imageViewFive);
      }
      holder.textViewReviewCount
          .setTextColor(ContextCompat.getColor(context, R.color.textcolor_reviewcount));
      holder.textViewReviewCount.setText(String.format(" (%s)",
          String.valueOf(categoryItem.getCatalogs().getProductReviewCount())));
      holder.parentLinearLayout.setTag(R.id.url_image_cache, imageUrl);
      holder.parentLinearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          String id, productName;
          id = catalogsList.get(holder.getAdapterPosition()).getCatalogs().getId();
          productName = catalogsList.get(holder.getAdapterPosition()).getCatalogs().getName();

          // Call Analytics
          Utils.blibliProductClicktracking(context, parentId, title, String.valueOf(numFound), id);

          BundleProduct bundleProduct = new BundleProduct();
          bundleProduct.setId(id);
          bundleProduct.setFashion(catalogsList.get(holder.getAdapterPosition()).getCatalogs().isFashion());
          bundleProduct.setName(catalogsList.get(holder.getAdapterPosition()).getCatalogs().getName());
          bundleProduct
              .setPrice(catalogsList.get(holder.getAdapterPosition()).getCatalogs().getStrikeThrughtPriceDisplay());
          bundleProduct.setPriceAfterDisc(String.format("%s (-%d%%)",
              catalogsList.get(holder.getAdapterPosition()).getCatalogs().getPriceDisplay(),
              catalogsList.get(holder.getAdapterPosition()).getCatalogs().getDiscount()));
          bundleProduct
              .setImageUrl(String.valueOf(holder.parentLinearLayout.getTag(R.id.url_image_cache)));
          Utils.intentToProductDetail(activity, bundleProduct);
          Utils.measureActions(id, productName, Constants.ACTION_PRODUCT_C2,
              Constants.SCREEN_NAME_C2);
        }
      });
    } else if (0 == getItemViewType(position)) {
      if (holder.categoriesSuggestionLayout.getChildCount() > 0) {
        holder.categoriesSuggestionLayout.removeAllViews();
      }
      if (0 == position) {
        holder.categoriesSuggestionTitle.setVisibility(View.GONE);
        holder.closeCategoriesSuggestion.setVisibility(View.GONE);
        for (int i = 0; i < categorySuggestionList.size(); i++) {
          holder.categoriesSuggestionLayout
              .addView(onClick.generateView(categorySuggestionList.get(i)));
        }
      } else {
        if (categorySuggestionList.isEmpty()) {
          holder.suggestionRelativeLayout.getLayoutParams().height = 0;
          holder.suggestionRelativeLayout.setVisibility(View.GONE);
        } else {
          holder.suggestionRelativeLayout.setVisibility(View.VISIBLE);
          holder.suggestionRelativeLayout.getLayoutParams().height =
              RecyclerView.LayoutParams.MATCH_PARENT;
          holder.categoriesSuggestionTitle.setVisibility(View.VISIBLE);
          holder.closeCategoriesSuggestion.setVisibility(View.VISIBLE);
          holder.categoriesSuggestionTitle
              .setText(activity.getResources().getString(R.string.suggestion_category_2));
          for (String categoriesNameOne : categorySuggestionList) {
            final String categoriesName = categoriesNameOne;
            TextView categoriesTextView = new TextView(activity);
            categoriesTextView.setPadding(20, 20, 20, 20);
            categoriesTextView.setTypeface(null, Typeface.BOLD);
            categoriesTextView.setTextColor(Color.parseColor("#000000"));
            if (categoriesName.length() > 15) {
              categoriesTextView.setText(String.format("%s...", categoriesName.substring(0, 15)));
            } else {
              categoriesTextView.setText(categoriesName);
            }
            holder.categoriesSuggestionLayout.addView(categoriesTextView);
            categoriesTextView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                onClick.clickListener(categoriesName);
              }
            });
          }

          holder.closeCategoriesSuggestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              holder.suggestionRelativeLayout.getLayoutParams().height = 0;
              holder.suggestionRelativeLayout.setVisibility(View.GONE);
            }
          });
        }
      }
    }
  }

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

  @Override
  public int getItemViewType(int position) {
    if (catalogsList.get(position).isSection()) {
      return 0;
    } else {
      return 1;
    }
  }

  public static class ViewHolder extends RecyclerView.ViewHolder {
    TextView descriptionTextView;
    TextView oldPriceTextView;
    TextView newPriceTextView;
    TextView stokHabisTextView;
    TextView cicilanTextView;
    TextView dividerGridTextView;
    ImageView imageView;
    LinearLayout linearLayout;
    ImageView imageViewOne;
    ImageView imageViewTwo;
    ImageView imageViewThree;
    ImageView imageViewFour;
    ImageView imageViewFive;
    TextView textViewReviewCount;
    LinearLayout relative;
    LinearLayout linearData;
    LinearLayout parentLinearLayout;
    LinearLayout categoriesSuggestionLayout;
    TextView categoriesSuggestionTitle;
    ImageView closeCategoriesSuggestion;
    RelativeLayout suggestionRelativeLayout;

    public ViewHolder(View convertView, int viewType, boolean isListViewMode) {
      super(convertView);
      if (1 == viewType) {
        if (isListViewMode) {
          descriptionTextView = (TextView) convertView.findViewById(R.id.title);
          oldPriceTextView = (TextView) convertView.findViewById(R.id.old_price);
          newPriceTextView = (TextView) convertView.findViewById(R.id.price);
          stokHabisTextView = (TextView) convertView.findViewById(R.id.tv_out_of_stock);
          cicilanTextView = (TextView) convertView.findViewById(R.id.tv_installment);
          dividerGridTextView = (TextView) convertView.findViewById(R.id.divider_grid);

          imageView = (ImageView) convertView.findViewById(R.id.image);
          linearLayout = (LinearLayout) convertView.findViewById(R.id.product_rating);
          imageViewOne = (ImageView) convertView.findViewById(R.id.ratingone);
          imageViewTwo = (ImageView) convertView.findViewById(R.id.ratingtwo);
          imageViewThree = (ImageView) convertView.findViewById(R.id.ratingthree);
          imageViewFour = (ImageView) convertView.findViewById(R.id.ratingfour);
          imageViewFive = (ImageView) convertView.findViewById(R.id.ratingfive);
          textViewReviewCount = (TextView) convertView.findViewById(R.id.reviewcount);
        } else {
          descriptionTextView = (TextView) convertView.findViewById(R.id.tv_desc);
          oldPriceTextView = (TextView) convertView.findViewById(R.id.tv_old_price);
          newPriceTextView = (TextView) convertView.findViewById(R.id.tv_new_price);
          stokHabisTextView = (TextView) convertView.findViewById(R.id.tv_out_of_stock);
          cicilanTextView = (TextView) convertView.findViewById(R.id.tv_installment);
          dividerGridTextView = (TextView) convertView.findViewById(R.id.divider_grid);
          imageView = (ImageView) convertView.findViewById(R.id.image);
          linearLayout = (LinearLayout) convertView.findViewById(R.id.product_rating);
          imageViewOne = (ImageView) convertView.findViewById(R.id.ratingone);
          imageViewTwo = (ImageView) convertView.findViewById(R.id.ratingtwo);
          imageViewThree = (ImageView) convertView.findViewById(R.id.ratingthree);
          imageViewFour = (ImageView) convertView.findViewById(R.id.ratingfour);
          imageViewFive = (ImageView) convertView.findViewById(R.id.ratingfive);
          textViewReviewCount = (TextView) convertView.findViewById(R.id.reviewcount);
          relative = (LinearLayout) convertView.findViewById(R.id.relative);
          linearData = (LinearLayout) convertView.findViewById(R.id.linear_data);

        }
        parentLinearLayout = (LinearLayout) convertView.findViewById(R.id.parentlayoutid);

      } else {
        categoriesSuggestionLayout = (LinearLayout) convertView.findViewById(R.id.ll_suggest_c2);
        categoriesSuggestionTitle = (TextView) convertView.findViewById(R.id.title1);
        closeCategoriesSuggestion = (ImageView) convertView.findViewById(R.id.imageView3);
        suggestionRelativeLayout =
            (RelativeLayout) convertView.findViewById(R.id.suggestion_layout);
      }
    }
  }

  public boolean isFashionPart() {
    return isFashionPart;
  }

  public void setFashionPart(boolean isFashionPart) {
    this.isFashionPart = isFashionPart;
  }

  public String getSearch() {
    return search;
  }

  public void setSearch(String search) {
    this.search = search;
  }

  public Activity getActivity() {
    return activity;
  }

  public void setActivity(Activity activity) {
    this.activity = activity;
  }

  public void setScreenResolution(int width, int height) {
    screenWidth = width;
    screenHeight = height;
  }

  public void setGravityCenter(TextView... views) {
    for (TextView view : views)
      view.setGravity(Gravity.CENTER_HORIZONTAL);
  }

  public int getNumFound() {
    return numFound;
  }

  public void setNumFound(int numFound) {
    this.numFound = numFound;
  }

  public interface OnClick {
    void clickListener(String categoryName);

    View generateView(String categoryName);
  }
}

//这是我的recycleview xml代码

<android.support.v7.widget.RecyclerView
            android:visibility="gone"
            android:layout_marginTop="50dp"
            android:id="@+id/gridview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f4f4f4"/> 

0 个答案:

没有答案