android


In RecycleView onScrolling images are flickering


This is the code for adapter which I am using. In this I am using Volley for the image loading. My actual problem is that whenever I am trying to scroll all the images the flickering.
So please suggest me the solution to fix the image flickering problem.
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);
}
}
// This is my recycleview xml code
<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"/>

Related Links

How can i write this layout code in my Android Activity?
How to build android standalone toolchain in windows 7
How can I download files from my Android app?
ExpandableListView Child Events
Button android while pressed
Why was this change made to SocketInputBuffer for Ice Cream Sandwich?
Comprehensive list of Splash Screen and Icons for Phonegap
Image classification with opencv
How to get libgdx view from andengine project
Horizontal scrolling between activities in Android
Read QR Code without using other app
Gps,GeoCoding causes app hang in android
Android App Closes on creation?
GridLayout spitting out “inconsistent constraint” debug-level logs
Android DB: Merging Two Databases Using SQL Dumps
Questio about AdMob ad url to application

Categories

HOME
embedded
porting
gridview
cheerio
graphics
opendj
google-cloud-functions
mapbox
boto3
jquery-ui-sortable
appcelerator-studio
cmdb
selenium-ide
cumulocity
heat
glyphicons
easyphp
sencha-touch
r-mice
xcode-ui-testing
pylons
catch-unit-test
carriage-return
salesforce-communities
google-earth-engine
node-mssql
filesystemwatcher
p4merge
tasm
movilizer
knime
importerror
optimizely
magento-2.0.7
azure-cli
compiler-design
edit
sundials
shinobi
juice-ui
visual-studio-2008-sp1
grails-plugin
microstation
rspec2
android-dateutils
gogs
android-wake-lock
riot
tracker-enabled-dbcontext
cycle2
user-management
maybe
tfs-workitem
rate-limiting
django-urls
phonertc
eoferror
apache-commons-daemon
shopizer
at-job
angular-gettext
rmysql
build-script
windows-embedded-standard
spring-3
gd-graph
scrypt
c++builder-xe5
isqlquery
scringo
workitem
scmmanager
joomla-template
pclzip
sqlclr
web-notifications
correlated-subquery
lambdaj
jquery-address
relative
tournament
mbeans
dataform
http-daemon
maven-1
pos-for-.net
request-headers
qtabbar
scrubyt
fault-tolerance
sqlobject
commercial-application
great-circle

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App