IndexOutOfBoundsException Inconsistency detected by RecyclerView.onLayout() - android

This kind of crash happens too much!!!
Found this problem today.
I am getting the above mention crash in a few devices lately. I think this has something to do with RecyclerView inside of a ScrollView??
But I am not sure, as it was working fine for months but suddenly started to see it. Recycler View Inconsistency Detected error, coming while scrolling fast or scrolling while loading more items.
I get it in Fabric Crashlytics.
Fatal Exception: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2e20039 position=4 id=-1, oldPos=-1, pLpos:-1 no parent} android.support.v7.widget.RecyclerView{9d0ee32 VFED..... .F...... 0,0-1440,191 #7f0a037d app:id/recylerCategoryList}, adapter:ahs#8a75b3d, layout:android.support.v7.widget.LinearLayoutManager#b3c9f32, context:com.ui.activity.NEWBusinessCardMainActivity#fe48a6e
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition + 5715(RecyclerView.java:5715)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline + 5898(RecyclerView.java:5898)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline + 286(GapWorker.java:286)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline + 343(GapWorker.java:343)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline + 359(GapWorker.java:359)
at android.support.v7.widget.GapWorker.prefetch + 366(GapWorker.java:366)
at android.support.v7.widget.GapWorker.run + 397(GapWorker.java:397)
at android.os.Handler.handleCallback + 751(Handler.java:751)
at android.os.Handler.dispatchMessage + 95(Handler.java:95)
at android.os.Looper.loop + 154(Looper.java:154)
at android.app.ActivityThread.main + 6682(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 1520(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main + 1410(ZygoteInit.java:1410)
Adapter:
public class CategoryHomeAdapter extends RecyclerView.Adapter<CategoryHomeAdapter.ItemViewHolder> {
public static final String TAG = "CategoryAdapter";
private Context activity;
private ArrayList<NEWCategoryList> categoryList = new ArrayList<>();
private ArrayList<NEWCategoryList> originalList = new ArrayList<>();
private RecyclerViewItemInterface viewItemInterface;
int i = 0;
int defaultSelectedValue;
private int selectedPosition = 0;
public CategoryHomeAdapter(Context activity, ArrayList<NEWCategoryList> categoryList) {
this.categoryList = categoryList;
this.activity = activity;
this.defaultSelectedValue = defaultSelectedValue;
Log.i(TAG, "categoryList Size :" + categoryList.size());
}
public void setViewItemInterface(RecyclerViewItemInterface viewItemInterface) {
Log.i(TAG, "setViewItemInterface: ");
this.viewItemInterface = viewItemInterface;
}
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_home_category_list, parent, false);
return new ItemViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull final ItemViewHolder holder, final int position) {
try {
final NEWCategoryList category = categoryList.get(position);
holder.categoryName.setText(category.getTagName());
holder.itemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "[onClick] getAdapterPosition:" + holder.getAdapterPosition());
try {
if (holder.getAdapterPosition() != -1 && category.getSubCategoryTagId() != null) {
Log.i(TAG, "onClick: Category ID selected :" + category.getSubCategoryTagId());
if (categoryList != null && categoryList.size() > 0) {
for (NEWCategoryList newList : categoryList) {
if (!newList.getSubCategoryTagId().equals(category.getSubCategoryTagId())) {
Log.i(TAG, "onClick: FALSE");
holder.CategoryImage.setVisibility(View.GONE);
newList.setSelected(false);
} else {
holder.CategoryImage.setVisibility(View.VISIBLE);
Log.i(TAG, "onClick: TRUE");
newList.setSelected(true);
}
}
}
Log.i(TAG, "Category Name : " + category.getTagName() + "Category Id : " + category.getSubCategoryTagId());
if (viewItemInterface != null) {
viewItemInterface.onItemClick(holder.getAdapterPosition(), categoryList.get(holder.getAdapterPosition()));
}
} else {
Log.i(TAG, "onClick: category Id is Wrong....");
}
} catch (Throwable e) {
// e.printStackTrace();
AppUtils.throwFatalException(e);
}
}
});
} catch (Exception e) {
// e.printStackTrace();
AppUtils.throwFatalException(e);
}
}
public void changeImage(int index) {
categoryList.get(index).setSelected(true);
notifyItemChanged(index);
}
#Override
public int getItemCount() {
return this.categoryList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView categoryName;
private ImageView CategoryImage;
public ItemViewHolder(View view) {
super(view);
this.categoryName = view.findViewById(R.id.textCaregoryname);
this.CategoryImage = view.findViewById(R.id.CategoryImage);
}
}
public void makeSearchList() {
this.originalList.clear();
this.originalList.addAll(this.categoryList);
}
}
Fragment:
public class Fragment extends DefaultFragment implements OnLoadMoreListener, RecyclerViewItemInterface {
#Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e(TAG, "****** onAttach *******");
this.activity = baseActivity;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sample_img_list_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
populateList();
populatedCategoryList();
loadCategory();
}
private void populateList() {
if (AppUtils.isValidContext(activity) && isAdded()) {
try {
sampleJsonList.clear();
LinearLayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
listBgImg.setLayoutManager(mLayoutManager);
bgImageAdapterNEW = new CategoryImageAdapterNew(activity, listBgImg, new GlideImageLoader(activity.getApplicationContext()), sampleJsonList, freeSampleList);
listBgImg.setAdapter(bgImageAdapterNEW);
bgImageAdapterNEW.setViewItemInterface(new RecyclerViewItemInterface() {
bgImageAdapterNEW.setPageAppendListener(new PageAppendListener() {
#Override
public void onPageAppendClick(final int page) {
Log.i(TAG, "onPageAppendClick : " + page);
listBgImg.post(new Runnable() {
public void run() {
// Remove Append Button
try {
Log.i(TAG, "List Size : " + sampleJsonList.size());
sampleJsonList.remove(sampleJsonList.size() - 1);
bgImageAdapterNEW.notifyItemRemoved(sampleJsonList.size());
onLoadMore(page, true);
} catch (Throwable e) {
e.printStackTrace();
}
}
});
}
#Override
public void onPageAppendClick() {
Log.i(TAG, "btnLoadMore button Click ");
}
#Override
public void showBottomToTop(boolean isShow) {
}
});
bgImageAdapterNEW.setOnLoadMoreListener(this);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
}
private void populatedCategoryList() {
if (AppUtils.isValidContext(activity) && isAdded()) {
try {
Log.i(TAG, "populatedCategoryList: ");
categoryList.clear();
LinearLayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
CategoryRecylerView.setLayoutManager(mLayoutManager);
categoryHomeAdapter = new CategoryHomeAdapter(activity, categoryList);
categoryHomeAdapter.setViewItemInterface(this);
CategoryRecylerView.setAdapter(categoryHomeAdapter);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
private void loadCategory() {
try {
categoryList.clear();
ArrayList<NEWCategoryList> localList = new ArrayList<>();
String s = SessionManager.getInstance().getKeyCatelogTagtemplate();
Log.i(TAG, "loadCategory: " + s);
if (!s.isEmpty()) {
CategorySampleData data = new Gson().fromJson(s, CategorySampleData.class);
if (data != null && data.getCategorylist() != null && data.getCategorylist().size() > 0) {
localList = data.getCategorylist();
}
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
AppUtils.throwFatalException(e);
}
}
/*Recyler View */
#Override
public void onItemClick(int position, Object obj) {
try {
Log.i(TAG, "onItemClick: Object");
if (categoryHomeAdapter != null && obj instanceof NEWCategoryList) {
if (((NEWCategoryList) obj).getSubCategoryTagId() != null) {
categoryHomeAdapter.notifyDataSetChanged();
NEWCategoryList CategoryList = (NEWCategoryList) obj;
if (subCategoryId != CategoryList.getSubCategoryTagId()) {
subCategoryId = CategoryList.getSubCategoryTagId();
Log.i(TAG, "onItemClick: subCategoryId" + subCategoryId);
if (!sampleJsonList.isEmpty()) {
sampleJsonList.clear();
if (bgImageAdapterNEW != null) {
bgImageAdapterNEW.notifyDataSetChanged();
}
}
tagName = CategoryList.getTagName();
Log.i(TAG, "onItemClick: Tag Name is : - " + tagName);
cancelOldRequest();
getAllSample(1, true, subCategoryId, tagName, false);
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
How can I fix this?

Related

Increment Page Index for Paginated Recyclerview

I have implemented pagination in my app but I am having trouble with incrementing the page index. I am getting data from unsplash api and have formed the request url like below:
private String url = Constants.SEARCH_URL + "top30&page="+ PAGE_INDEX;
and this is the url used below:
https://api.unsplash.com/search/photos?client_id=api_key&order_by=latest&query=top30&page=1
In my fragment, I have a constant int like private static int PAGE_INDEX = 1; which tracks the current page and used for getting the next pages till page limit.
The first request runs fine and when scrolling towards the last item, more items are fetched. However, these items are same as from the first page index and after logging the url, I get the same as in first request like below:
https://api.unsplash.com/search/photos?client_id=api_key&order_by=latest&query=top30&page=1
I have attached my scroll listener in recyclerview like this and incremented the PAGE_INDEX but not getting the next page in url but in log cat it shows the page index as 2. Here's the listener attached:
top30RV.addOnScrollListener(new PaginationScrollListener(llm) {
#Override
protected void loadMoreItems() {
isLoading = true;
PAGE_INDEX = PAGE_INDEX + 1;
Log.d(TAG, "PIn:\t" + PAGE_INDEX);
loadNextPage();
}
#Override
protected int getTotalPageCount() {
return PAGE_COUNT;
}
#Override
public boolean isLoading() {
return isLoading;
}
#Override
public boolean isLastPage() {
return isLastPage;
}
});
and my PaginationScrollListener class code:
public abstract class PaginationScrollListener extends RecyclerView.OnScrollListener {
LinearLayoutManager llm;
public PaginationScrollListener(LinearLayoutManager llm) {
this.llm = llm;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visisbleItemCount = recyclerView.getChildCount();
int totalItemCount = llm.getItemCount();
int firstVisibleItemPosn = llm.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()){
if ((visisbleItemCount + firstVisibleItemPosn) >= totalItemCount && firstVisibleItemPosn > 0){
loadMoreItems();
}
}
}
protected abstract void loadMoreItems();
protected abstract int getTotalPageCount();
public abstract boolean isLoading();
public abstract boolean isLastPage();
}
in my adapter I have called the method to addLoadingFooter and it works but also the progressbar is not dismissed and removeLoadingFooter is called but doesn't work because of the page index is less than the page limit.
public class Top30Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = Top30Adapter.class.getSimpleName();
private final Context context;
private List<UnsplashImages> itemsList;
private UnsplashImages unsplashImages;
private static final int ITEMS = 0;
private static final int PROGRESS = 1;
private boolean isLoadingAdded = false;
private boolean retryPageLoad = false;
private String errorMsg;
private PaginationAdapterCallback mCallback;
public Top30Adapter(Context context, List<UnsplashImages> itemsList) {
this.context = context;
this.itemsList = itemsList;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case ITEMS:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.wallpaper_items_layout, parent, false);
return new WallpaperItemViewHolder(view);
case PROGRESS:
View pv = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_items_layout, parent, false);
return new LoadingViewHolder(pv);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewholder, final int position) {
switch (getItemViewType(position)){
case ITEMS:
WallpaperItemViewHolder wallpaperItemViewHolder = (WallpaperItemViewHolder) viewholder;
unsplashImages = (UnsplashImages) itemsList.get(position);
Picasso.with(context)
.load(unsplashImages.getRegularImg())
.placeholder(R.drawable.drawer_header_trimmed)
.into(wallpaperItemViewHolder.wallpaperItemImg);
wallpaperItemViewHolder.favoriteWP_IV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Favorites favorites = new Favorites();
favorites.setFavoritesId(UUID.randomUUID().toString());
favorites.setLargeImgURL(unsplashImages.getRegularImg());
favorites.setPreviewImgURL(unsplashImages.getRegularImg());
favorites.save();
Toast.makeText(context, "Added to Favorites", Toast.LENGTH_SHORT).show();
}
});
wallpaperItemViewHolder.setWallPaper_TV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final WallpaperManager wpm = WallpaperManager.getInstance(context);
Picasso.with(context)
.load(((UnsplashImages) itemsList.get(position)).getRegularImg())
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
wpm.setBitmap(bitmap);
Toast.makeText(context, "Your New Wallpaper Has Been Set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d(TAG, "Bitmap Load Failed");
Toast.makeText(context, "Could Not Set Wallpaper...Choose Another", Toast.LENGTH_SHORT).show();
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d(TAG, "Prep to Load Bitmap");
}
});
}
});
break;
case PROGRESS:
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) viewholder;
if (retryPageLoad){
loadingViewHolder.mErrorLayout.setVisibility(View.VISIBLE);
loadingViewHolder.mProgressBar.setVisibility(View.GONE);
loadingViewHolder.mErrorTxt.setText(errorMsg != null
? errorMsg
: context.getString(R.string.error_msg_unknown));
} else {
loadingViewHolder.mErrorLayout.setVisibility(View.GONE);
loadingViewHolder.mProgressBar.setVisibility(View.VISIBLE);
}
break;
}
}
#Override
public int getItemCount() {
if (itemsList == null) {
return 0;
}
return itemsList.size();
}
#Override
public int getItemViewType(int position) {
return (position == itemsList.size() - 1 && isLoadingAdded) ? PROGRESS : ITEMS;
}
public void addImage(UnsplashImages images){
itemsList.add(images);
notifyItemInserted(itemsList.size() - 1);
}
public void addAllImages(List<UnsplashImages> list){
for (UnsplashImages images : list){
addImage(images);
}
}
public void addLoadingFooter() {
isLoadingAdded = true;
addImage(new UnsplashImages());
}
public void removeLoadingFooter(){
isLoadingAdded = false;
int position = itemsList.size() - 1;
UnsplashImages unsplashImages = getItem(position);
if (unsplashImages != null){
itemsList.remove(unsplashImages);
notifyItemRemoved(position);
}
}
private UnsplashImages getItem(int position) {
return itemsList.get(position);
}
public void showRetry(boolean show, #Nullable String errorMsg){
retryPageLoad = true;
notifyItemChanged(itemsList.size() - 1);
if (errorMsg != null){this.errorMsg = errorMsg; } else { errorMsg = "Can't Fetch Wallpapers Now..."; }
}
}
Full fragment code:
public class Top30Fragment extends Fragment {
private static final String TAG = Top30Fragment.class.getSimpleName();
private RecyclerView top30RV;
private List<UnsplashImages> itemsList = new ArrayList<>();
private Top30Adapter adapter;
private int PAGE_COUNT;
private static int PAGE_INDEX = 1;
private static int PER_PAGE = 20;
private static int CURRENT_PAGE = PAGE_INDEX;
private boolean isLoading = false;
private boolean isLastPage = false;
private String url = Constants.SEARCH_URL + "top30&page="+ PAGE_INDEX;
public Top30Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vtop = inflater.inflate(R.layout.fragment_top30, container, false);
top30RV = vtop.findViewById(R.id.top30RV);
top30RV.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
top30RV.setLayoutManager(llm);
if (Utils.isNetwork(getActivity())){
fetchWallpapers();
} else {
Snackbar.make(getActivity().findViewById(android.R.id.content), "Check Connection", Snackbar.LENGTH_LONG).show();
}
adapter = new Top30Adapter(getActivity(), itemsList);
top30RV.setAdapter(adapter);
top30RV.addOnScrollListener(new PaginationScrollListener(llm) {
#Override
protected void loadMoreItems() {
isLoading = true;
PAGE_INDEX = PAGE_INDEX + 1;
Log.d(TAG, "PIn:\t" + PAGE_INDEX);
loadNextPage();
}
#Override
protected int getTotalPageCount() {
return PAGE_COUNT;
}
#Override
public boolean isLoading() {
return isLoading;
}
#Override
public boolean isLastPage() {
return isLastPage;
}
});
adapter.notifyDataSetChanged();
return vtop;
}
private void loadNextPage() {
int newIndex = PAGE_INDEX +=1;
Log.d(TAG, "Next Page:\t" + newIndex);
Log.d(TAG, "Next Page Index:\t" + newIndex);
Log.d(TAG, "Next Page URL:\t" + url);
AndroidNetworking.get(url)
.setTag("Load Next Top30")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Next Page Response:\t" + response.toString());
if (response != null){
isLoading = false;
adapter.removeLoadingFooter();
try {
JSONObject jsonObject = new JSONObject(response.toString());
JSONArray results = jsonObject.getJSONArray("results");
for (int p = 0; p < results.length(); p++){
JSONObject items = results.getJSONObject(p);
UnsplashImages images = new UnsplashImages();
images.setImageId(items.getString("id"));
JSONObject urls = items.getJSONObject("urls");
images.setRawImg(urls.getString("raw"));
images.setFullImg(urls.getString("full"));
images.setRegularImg(urls.getString("regular"));
images.setSmallImg(urls.getString("small"));
images.setThumbImg(urls.getString("thumb"));
itemsList.add(images);
adapter.notifyDataSetChanged();
Log.d(TAG, "New List Size:\t" + itemsList.size());
if (CURRENT_PAGE != PAGE_COUNT){
adapter.addLoadingFooter();
} else {
isLastPage = true;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
#Override
public void onError(ANError anError) {
}
});
}
private void fetchWallpapers() {
// String url = Constants.SEARCH_URL + "top30&page="+ PAGE_INDEX + "per_page=" + PER_PAGE;
Log.d(TAG, "Top30 Unsplash URL:\t" + url);
AndroidNetworking.get(url)
.setPriority(Priority.HIGH)
.setTag("Get Seasons Wallpapers")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, "Un-Response:\t" + response.toString());
if (response != null){
try {
JSONObject jsonObject = new JSONObject(response.toString());
PAGE_COUNT = jsonObject.getInt("total_pages");
Log.d(TAG, "Page Count:\t" + PAGE_COUNT);
JSONArray results = jsonObject.getJSONArray("results");
for (int p = 0; p < results.length(); p++){
JSONObject items = results.getJSONObject(p);
UnsplashImages images = new UnsplashImages();
images.setImageId(items.getString("id"));
JSONObject urls = items.getJSONObject("urls");
images.setRawImg(urls.getString("raw"));
images.setFullImg(urls.getString("full"));
images.setRegularImg(urls.getString("regular"));
images.setSmallImg(urls.getString("small"));
images.setThumbImg(urls.getString("thumb"));
itemsList.add(images);
adapter.notifyDataSetChanged();
Log.d(TAG, "List Size:\t" + itemsList.size());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
#Override
public void onError(ANError anError) {
}
});
}
Is there another way to construct the request url to increment the page index? Thanks.

API successfull called, but not displayed

i am trying to call local API server using retrofit, in my Logcat shows that the API success called, but in my app there is nothing displayed. how i can fix this issue? and here is my code :
interface.java :
public interface Service {
#GET("vocabulary/id/*")
Call<APIResponse<List<VocabMaster>>> getVocabMaster();
}
Vocab Master.java to collect data from API :
public class VocabMaster implements Serializable {
private BigDecimal vocab_id;
private String hanzi_t;
private String hanzi_s;
private String pinyin;
private BigDecimal level_id;
private List<VocabMeaning> meaning;
public BigDecimal getVocab_id() {
return vocab_id;
}
public void setVocab_id(BigDecimal vocab_id) {
this.vocab_id = vocab_id;
}
public String getHanzi_t() {
return hanzi_t;
}
public void setHanzi_t(String hanzi_t) {
this.hanzi_t = hanzi_t;
}
public String getHanzi_s() {
return hanzi_s;
}
public void setHanzi_s(String hanzi_s) {
this.hanzi_s = hanzi_s;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
public List<VocabMeaning> getMeaning() {
return meaning;
}
public void setMeaning(List<VocabMeaning> meaning) {
this.meaning = meaning;
}
public BigDecimal getLevel_id() {
return level_id;
}
public void setLevel_id(BigDecimal level_id) {
this.level_id = level_id;
}
#Override
public String toString() {
return "VocabMaster{" +
"vocab_id=" + vocab_id +
", hanzi_t='" + hanzi_t + '\'' +
", hanzi_s='" + hanzi_s + '\'' +
", pinyin='" + pinyin + '\'' +
", level_id=" + level_id +
", meaning=" + meaning +
'}';
}
}
And this is MainActivity.java :
public class MainActivity extends AppCompatActivity {
RecyclerView rvReligiVideo;
List<VocabMaster> vocabMasters = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvReligiVideo = findViewById(R.id.rvReligi);
initReligiVideo();
loadReligiVideo();
}
private void initReligiVideo() {
LinearLayoutManager layout = new LinearLayoutManager(this);
layout.setOrientation(LinearLayoutManager.HORIZONTAL);
rvReligiVideo.setLayoutManager(layout);
VocabAdapter ar = new VocabAdapter(vocabMasters);
rvReligiVideo.setAdapter(ar);
}
//=========== Request to API ==========
private void loadReligiVideo() {
Call<APIResponse<List<VocabMaster>>> call = ServicesFactory.getService().getVocabMaster();
call.enqueue(new Callback<APIResponse<List<VocabMaster>>>() {
#Override
public void onResponse(Call<APIResponse<List<VocabMaster>>> call, Response<APIResponse<List<VocabMaster>>> response) {
if (response.isSuccessful() && response.body().isSuccessful()) {
List<VocabMaster> data = response.body().data;
if (data != null) {
vocabMasters.clear();
vocabMasters.addAll(data);
rvReligiVideo.getAdapter().notifyDataSetChanged();
}
} else {
Toast.makeText(MainActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<APIResponse<List<VocabMaster>>> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
logcat on android studio :
JSON Data Structure :
Here is my Adapter :
public class VocabAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<VocabMaster> data;
public VocabAdapter(List<VocabMaster> data) {
this.data = data;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.vocabulary_model, null);
return new VH(v);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
VocabMaster vm = data.get(position);
VH vh = (VH) holder;
vh.hanziS.setText(vm.getHanzi_s());
vh.hanziT.setText(vm.getHanzi_t());
vh.pinyin.setText(vm.getPinyin());
vh.sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Sound", Toast.LENGTH_SHORT).show();
}
});
vh.favorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Favorite", Toast.LENGTH_SHORT).show();
}
});
vh.share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Share", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return (data == null) ? 0 : data.size();
}
public class VH extends RecyclerView.ViewHolder {
TextView hanziS;
TextView hanziT;
TextView pinyin;
TextView desc;
ImageView sound, share, favorite;
Context context;
public VH(View itemView) {
super(itemView);
hanziS = itemView.findViewById(R.id.hanziS);
hanziT = itemView.findViewById(R.id.hanziT);
pinyin = itemView.findViewById(R.id.pinyin);
desc = itemView.findViewById(R.id.txtDesc);
sound = itemView.findViewById(R.id.imgSpeaker);
share = itemView.findViewById(R.id.imgShare);
favorite = itemView.findViewById(R.id.imgFavotite);
}
}
}
i did some changes in your code... use this
private VocabAdapter ar;
private void initReligiVideo() {
LinearLayoutManager layout = new LinearLayoutManager(this);
layout.setOrientation(LinearLayoutManager.HORIZONTAL);
rvReligiVideo.setLayoutManager(layout);
ar = new VocabAdapter(vocabMasters);
rvReligiVideo.setAdapter(ar);
}
private void loadReligiVideo() {
Call<APIResponse<List<VocabMaster>>> call = ServicesFactory.getService().getVocabMaster();
call.enqueue(new Callback<APIResponse<List<VocabMaster>>>() {
#Override
public void onResponse(Call<APIResponse<List<VocabMaster>>> call, Response<APIResponse<List<VocabMaster>>> response) {
if (response.isSuccessful() && response.body().isSuccessful()) {
List<VocabMaster> data = response.body().data;
if (data != null) {
ar.setVocoList(data);
}
} else {
Toast.makeText(MainActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<APIResponse<List<VocabMaster>>> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
Add this method in your adapter Class
public void setVocoList(List<VocabMaster> list){
if(list!=null)
data=list;
notifyDataSetChanged();
}
Do not change adapter's list when you have new list.
//incorrect except that the adapter is first time set to recyclerview and setVocoList is called before recyclerview.setAdater
public void setVocoList(List<VocabMaster> list){
if(list!=null)
data=list;
notifyDataSetChanged();
}
//correct
private List<VocabMaster> datas = new ArrayList()
public void setVocoList(List<VocabMaster> list){
if(list==null) return;
datas.clear();
datas.addAll(list);
notifyDataSetChanged();
}

Glide and RecyclerView - Images messed up

I'm trying to create a RecyclerView with ImageViews. Here is what I get:
As you can see all the artist covers are messed up and in the wrong order. If I scroll or reload, the respective images might change.
Here is my adapter onBindViewHolder:
public class ArtistsAdapter extends RecyclerView.Adapter<ArtistViewHolder> implements ListPreloader.PreloadModelProvider {
private static final String TAG = ArtistsAdapter.class.getSimpleName();
private final LayoutInflater mInflater;
private final GlideRequest<Bitmap> fullRequest;
ArtistsAdapter(Context context) {
this.mContext = context;
this.artists = new ArrayList<>();
this.mInflater = LayoutInflater.from(context);
this.fullRequest = GlideApp.with(context).asBitmap();
}
private final Context mContext;
public List<CompactdArtist> getArtists() {
return artists;
}
void swapArtists (List<CompactdArtist> artists) {
this.artists.clear();
this.artists.addAll(artists);
notifyDataSetChanged();
}
private final List<CompactdArtist> artists;
#Override
public ArtistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_artist_item, parent, false);
return new ArtistViewHolder(view);
}
#Override
public void onViewRecycled(ArtistViewHolder holder) {
super.onViewRecycled(holder);
Log.d(TAG, "onViewRecycled: ");
GlideApp.with(mContext).clear(holder.artistImageView);
holder.artistImageView.setImageDrawable(null);
}
#Override
public void onBindViewHolder(final ArtistViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: " + position);
final CompactdArtist current = artists.get(position);
// holder.setIsRecyclable(false);
holder.artistImageView.setImageDrawable(null);
try {
fullRequest.load(new MediaCover(current))
.fallback(ImageUtils.getFallback(mContext))
.into(new ImageViewTarget<Bitmap>(holder.artistImageView) {
#Override
protected void setResource(#Nullable Bitmap resource) {
if (resource == null) {
// holder.setIsRecyclable(true);
return;
}
holder.artistImageView.setImageBitmap(resource);
int color = Palette.from(resource).generate().getMutedColor(0xFFFFFF);
try {
current.fetch();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
holder.artistNameText.setText(R.string.unknown_artist_name);
}
holder.artistBackground.setBackgroundColor(color);
holder.bindArtist(current);
holder.artistSub.setText(current.getAlbumCount() + " albums");
holder.artistNameText.setText(current.getName());
// holder.setIsRecyclable(true);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
super.onLoadCleared(placeholder);
// holder.setIsRecyclable(true);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public long getItemId(int position) {
return artists.get(position).getId().hashCode();
}
#Override
public int getItemCount() {
return this.artists.size();
}
#NonNull
#Override
public List getPreloadItems(int position) {
return this.artists.subList(position, position + 1);
}
#Nullable
#Override
public RequestBuilder<?> getPreloadRequestBuilder(Object item) {
MediaCover cover = (MediaCover) item;
return fullRequest.clone().thumbnail(0.2f).load(cover);
}
}
Here is code in my fragment container:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_artists, container, false);
mArtistRecyclerView = rootView.findViewById(R.id.artists_recyclerview);
LinearLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
mArtistRecyclerView.setLayoutManager(layoutManager);
mArtistsAdapter = new ArtistsAdapter(getActivity());
mArtistRecyclerView.setAdapter(mArtistsAdapter);
mArtistRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
#Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
ArtistViewHolder artistViewHolder = (ArtistViewHolder) holder;
GlideApp.with(holder.itemView).clear(artistViewHolder.getArtistImage());
}
});
FixedPreloadSizeProvider<MediaCover> coverSizeProvider = new FixedPreloadSizeProvider<>(64, 64);
RecyclerViewPreloader<MediaCover> preloader = new RecyclerViewPreloader<MediaCover>(GlideApp.with(this), mArtistsAdapter, coverSizeProvider, 2);
return rootView;
}
As a bonus, I add the fetcher for my MediaCover:
/**
* Created by vinz243 on 13/12/2017.
*/
public class MediaCoverFetcher implements DataFetcher<InputStream> {
private final MediaCover mediaCover;
private boolean cancelled = false;
private InputStream stream;
public MediaCoverFetcher(MediaCover mediaCover) {
this.mediaCover = mediaCover;
}
#Override
public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
CompactdArtwork artwork = mediaCover.getArtwork();
try {
stream = artwork.getImage(ArtworkSize.LARGE);
if (cancelled) {
callback.onLoadFailed(new RuntimeException("Cancelled"));
} else {
callback.onDataReady(stream);
}
} catch (NullPointerException e) {
callback.onLoadFailed(e);
}
}
#Override
public void cleanup() {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void cancel() {
cancelled = true;
}
#NonNull
#Override
public Class<InputStream> getDataClass() {
return InputStream.class;
}
#NonNull
#Override
public DataSource getDataSource() {
return DataSource.LOCAL;
}
}
I guess it comes from the recycling part, but I don't know how to fix the behavior. As you may see in the code, I tried setIsRecyclable but I would get errors saying unmatched calls and it wouldn't fix it
You should try to change a couple of things :
move your setters on your ArtistViewHolder object in the setResource function
define your CompactdArtist variable as a final
#Override
public void onBindViewHolder(final ArtistViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder: " + position);
final CompactdArtist current = artists.get(position);
// holder.setIsRecyclable(false);
holder.bindArtist(artists.get(position));
holder.artistImageView.setImageDrawable(null);
try {
current.fetch();
fullRequest.load(new MediaCover(current))
.fallback(ImageUtils.getFallback(mContext))
.into(new ImageViewTarget<Bitmap>(holder.artistImageView) {
#Override
protected void setResource(#Nullable Bitmap resource) {
if (resource == null) {
// holder.setIsRecyclable(true);
return;
}
holder.artistImageView.setImageBitmap(resource);
int color = Palette.from(resource).generate().getMutedColor(0xFFFFFF);
holder.artistBackground.setBackgroundColor(color);
// holder.setIsRecyclable(true);
holder.artistNameText.setText(current.getName());
holder.artistSub.setText(current.getAlbumCount() + " albums");
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
super.onLoadCleared(placeholder);
// holder.setIsRecyclable(true);
}
});
} catch (CouchbaseLiteException e) {
e.printStackTrace();
holder.artistNameText.setText(R.string.unknown_artist_name);
} catch (IOException e) {
e.printStackTrace();
}
}
I assume that the way you load your images is asynchronous, so in this case your problems resides in the fact that you set your variables as if they were in a synchronous process, that's why you get your information displayed on the wrong blocks.

callback in recyclerview affect all cells

I have a button in every cell of a RecyclerView that launches a download network call. The cell displays differently according to whether it's downloading, downloaded or finished.
my simplified code :
#Override public void onBindViewHolder(final CatalogViewHolder holder, int position) {
final DownloadStatusCallback statusCallback = new DownloadStatusCallback() {
#Override public void started() {
mainThreadHandler.post(new Runnable() {
#Override public void run() {
holder.itemView.setBackground(//color1
}
});
}
#Override public void finished() {
mainThreadHandler.post(new Runnable() {
#Override public void run() {
holder.itemView.setBackground(//color 2
}
});
}
#Override public void error(Exception e) {
mainThreadHandler.post(new Runnable() {
#Override public void run() {
holder.itemView.setBackground(//color 3
}
});
}
};
holder.button1.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
assyncCall(statusCallback);
}
});
}
The first time i clic on a cell, everything works fine. If I clic on the download button of another cell, both of them will update.
I understand that's due to recyclerview recycling cells, but I can't figure out how to do better.
Thanks !
my full adapter :
public class CatalogRecyclerAdapter extends RecyclerView.Adapter<CatalogViewHolder> {
public static final String TAG = "CatalogRecyclerAdapter";
private final LayoutInflater inflater;
private final DownloadCenter downloadCenter;
private final ListInterface.FlowController flowController;
private final ResourcesStringRepository resourcesStringRepository;
private final ImageManagerFactory imageManagerFactory;
private final Handler mainThreadHandler;
public CatalogRecyclerAdapter(LayoutInflater inflater, ListInterface.FlowController flowController,
DownloadCenter downloadCenter, ResourcesStringRepository resourcesStringRepository,
ImageManagerFactory imageManagerFactory, Handler mainThreadHandler) {
this.inflater = inflater;
this.flowController = flowController;
this.downloadCenter = downloadCenter;
this.resourcesStringRepository = resourcesStringRepository;
this.imageManagerFactory = imageManagerFactory;
this.mainThreadHandler = mainThreadHandler;
}
private static final int TITLE = 0;
private static final int USER = 2;
private static final int PROGRAM = 3;
private static final int COURSE = 4;
private static final int GROUP = 5;
private static final int MEDIA = 6;
private static final int ERROR = 7;
private static final int DEMO = 8;
//The list of all elements
private List<FilterableUser> users = new ArrayList<>();
private List<CatalogProgram> programs = new ArrayList<>();
private List<CatalogProgram> demos = new ArrayList<>();
private List<CatalogCourse> courses = new ArrayList<>();
private List<FilterableGroup> groups = new ArrayList<>();
private List<CatalogMedia> medias = new ArrayList<>();
//The list that will be displayed after filtering and research.
List<Object> displayedList = new ArrayList<>();
static final String TITLES[] = new String[10];
static {
Context ctx = M360Application.getContext();
TITLES[USER] = ctx.getString(R.string.users);
TITLES[PROGRAM] = ctx.getString(R.string.programs);
TITLES[COURSE] = ctx.getString(R.string.courses);
TITLES[GROUP] = ctx.getString(R.string.groups);
TITLES[MEDIA] = ctx.getString(R.string.documents);
TITLES[DEMO] = ctx.getString(R.string.programs_demo);
}
private String searchString;
#Override public int getItemViewType(int position) {
if (displayedList.get(position) instanceof String) {
return TITLE;
} else if (displayedList.get(position) instanceof FilterableUser) {
return USER;
} else if (displayedList.get(position) instanceof CatalogProgramDemo) {
return DEMO;
} else if (displayedList.get(position) instanceof CatalogProgram) {
return PROGRAM;
} else if (displayedList.get(position) instanceof CatalogCourse) {
return COURSE;
} else if (displayedList.get(position) instanceof FilterableGroup) {
return GROUP;
} else if (displayedList.get(position) instanceof CatalogMedia) {
return MEDIA;
} else if (displayedList.get(position) instanceof CatalogError) {
return ERROR;
} else {
throw new ClassCastException(
"this adapter's displayedList is corrupted" + displayedList.get(position).toString());
}
}
public void setData(List<Filterable> data, String searchedString) {
searchString = searchedString;
setData(data);
}
private void setData(List<Filterable> data) {
LogDev.i(TAG, "setting data size: " + data.size());
groups.clear();
users.clear();
programs.clear();
demos.clear();
courses.clear();
medias.clear();
for (Filterable element : data) {
if (element instanceof CatalogCourse) {
courses.add((CatalogCourse) element);
} else if (element instanceof FilterableUser) {
users.add((FilterableUser) element);
} else if (element instanceof CatalogProgramDemo) {
demos.add((CatalogProgramDemo) element);
} else if (element instanceof CatalogProgram) {
programs.add((CatalogProgram) element);
} else if (element instanceof FilterableGroup) {
groups.add((FilterableGroup) element);
} else if (element instanceof CatalogMedia) {
medias.add((CatalogMedia) element);
}
}
constructDataSet();
}
private void constructDataSet() {
displayedList.clear();
if (!demos.isEmpty()) {
displayedList.add(TITLES[DEMO]);
displayedList.addAll(demos);
}
if (!programs.isEmpty()) {
displayedList.add(TITLES[PROGRAM]);
displayedList.addAll(programs);
}
if (!courses.isEmpty()) {
displayedList.add(TITLES[COURSE]);
displayedList.addAll(courses);
}
if (!users.isEmpty()) {
displayedList.add(TITLES[USER]);
displayedList.addAll(users);
}
if (!groups.isEmpty()) {
displayedList.add(TITLES[GROUP]);
displayedList.addAll(groups);
}
if (!medias.isEmpty()) {
displayedList.add(TITLES[MEDIA]);
displayedList.addAll(medias);
}
if (displayedList.isEmpty()) {
displayedList.add(new CatalogError());
}
LogDev.w(TAG, "displayedList.size() : " + displayedList.size());
notifyDataSetChanged();
}
#Override public CatalogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TITLE:
return new TitleViewHolder(inflater.inflate(R.layout.item_list_title_catalog, parent, false));
case USER:
return new UserViewHolder(inflater.inflate(R.layout.widget_user_small, parent, false));
case PROGRAM:
case DEMO:
return new ProgramViewHolder(inflater.inflate(R.layout.widget_program_small, parent, false));
case COURSE:
return new CourseViewHolder(inflater.inflate(R.layout.widget_course_small, parent, false));
case GROUP:
return new GroupViewHolder(inflater.inflate(R.layout.widget_group_small, parent, false));
case MEDIA:
return new MediaViewHolder(inflater.inflate(R.layout.widget_media_small, parent, false));
case ERROR:
return new CatalogErrorViewHolder(inflater.inflate(R.layout.widget_noresult_small, parent, false));
default:
LogDev.e(TAG, "view type not supported");
return null;
}
}
#Override public void onBindViewHolder(CatalogViewHolder holder, int position) {
Object displayedObject = displayedList.get(position);
//holder.bind(displayedObject, errorDisplayInterface);
if (holder instanceof TitleViewHolder && displayedObject instanceof String) {
((TitleViewHolder) holder).tv.setText((String) displayedObject);
} else if (holder instanceof ProgramViewHolder && displayedObject instanceof CatalogProgram) {
bindProgramViewHolder((ProgramViewHolder) holder, (CatalogProgram) displayedObject);
} else if (holder instanceof CourseViewHolder && displayedObject instanceof CatalogCourse) {
bindCourseViewHolder((CourseViewHolder) holder, (CatalogCourse) displayedObject);
} else if (holder instanceof GroupViewHolder && displayedObject instanceof FilterableGroup) {
bindGroupViewHolder((GroupViewHolder) holder, (FilterableGroup) displayedObject);
} else if (holder instanceof UserViewHolder && displayedObject instanceof FilterableUser) {
bindUserViewHolder((UserViewHolder) holder, (FilterableUser) displayedObject);
} else if (holder instanceof MediaViewHolder && displayedObject instanceof CatalogMedia) {
bindMediaViewHolder((MediaViewHolder) holder, (CatalogMedia) displayedObject);
} else if (holder instanceof CatalogErrorViewHolder) {
//No binding with any data
} else {
throw new ClassCastException(displayedObject.toString());
}
//Highlight
if (searchString != null && !searchString.isEmpty())
{
TextViewHighlighter.highlight(holder, searchString);
}
}
private void bindCourseViewHolder(final CourseViewHolder courseViewHolder, final CatalogCourse course) {
courseViewHolder.name_textView.setText(course.name);
courseViewHolder.viewNb_textView.setText(course.views != null ? course.views.toString() : "0");
if (course.elementCount == null) {
courseViewHolder.counterLinear.setVisibility(View.GONE);
} else {
courseViewHolder.counterLinear.setVisibility(View.VISIBLE);
courseViewHolder.questionNb_textView.setText(
course.elementCount.questions != null ? course.elementCount.questions.toString() : "0");
courseViewHolder.mediaNb_textView.setText(
course.elementCount.medias != null ? course.elementCount.medias.toString() : "0");
courseViewHolder.sheetNb_textView.setText(
course.elementCount.sheets != null ? course.elementCount.sheets.toString() : "0");
}
imageManagerFactory.course(course.id).thumbnail(courseViewHolder.pic_imageView);
//new CourseImageManager(course.id).load(courseViewHolder.pic_imageView);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override public void onClick(View view) {
flowController.routeToCourse(course.id);
}
};
courseViewHolder.container.setOnClickListener(clickListener);
if (course.canBeOffline) {
courseViewHolder.downloadBlock.setVisibility(View.VISIBLE);
DownloadState state = downloadCenter.getCourseStatus(course.id);
LogDev.i(TAG, "can be offline " + state.name());
if (state == DownloadState.DOWNLOADING) {
updateDownloadBlock(courseViewHolder, DownloadableStatus.DOWNLOADING);
}
if (state == DownloadState.TO_DOWNLOAD) {
updateDownloadBlock(courseViewHolder, DownloadableStatus.DOWNLOADABLE);
}
if (state == DownloadState.DOWNLOADED || state == DownloadState.DOWNLOADED_WITH_SHARED_MODE) {
updateDownloadBlock(courseViewHolder, DownloadableStatus.DOWNLOADED);
} else {
DownloadStatusCallback statusCallback = new DownloadStatusCallback() {
#Override public void started() {
LogDev.i(TAG, "started");
mainThreadHandler.post(new Runnable() {
#Override public void run() {
updateDownloadBlock(courseViewHolder, DownloadableStatus.DOWNLOADING);
}
});
}
#Override public void finished() {
mainThreadHandler.post(new Runnable() {
#Override public void run() {
updateDownloadBlock(courseViewHolder, DownloadableStatus.DOWNLOADED);
}
});
}
#Override public void error(Exception e) {
mainThreadHandler.post(new Runnable() {
#Override public void run() {
updateDownloadBlock(courseViewHolder, DownloadableStatus.ERROR);
}
});
}
};
downloadCenter.subscribe(course.id, statusCallback);
courseViewHolder.downloadBlock.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v) {
new Thread() {
#Override public void run() {
super.run();
try {
downloadCenter.downloadCourse(course.id, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
});
}
} else {
LogDev.i(TAG, "can't be offline");
courseViewHolder.downloadBlock.setVisibility(View.INVISIBLE);
}
}
private void updateDownloadBlock(CourseViewHolder courseViewHolder, DownloadableStatus status) {
if (status == null) return;
courseViewHolder.downloadBlock.setVisibility(
status.equals(DownloadableStatus.NOT_DOWNLOADABLE) ? View.GONE : View.VISIBLE);
courseViewHolder.downloadImage.setVisibility(
status.equals(DownloadableStatus.DOWNLOADABLE) ? View.VISIBLE : View.GONE);
courseViewHolder.downloadProgress.setVisibility(
status.equals(DownloadableStatus.DOWNLOADING) ? View.VISIBLE : View.GONE);
courseViewHolder.downloadedImage.setVisibility(
status.equals(DownloadableStatus.DOWNLOADED) ? View.VISIBLE : View.GONE);
courseViewHolder.downloadErrImage.setVisibility(
status.equals(DownloadableStatus.ERROR) ? View.VISIBLE : View.GONE);
}
private enum DownloadableStatus {
NOT_DOWNLOADABLE, DOWNLOADABLE, DOWNLOADING, DOWNLOADED, ERROR
}
private void bindProgramViewHolder(ProgramViewHolder programViewHolder, final CatalogProgram program) {
imageManagerFactory.program(program.id).thumbnail(programViewHolder.pic_imageView);
//new ProgramImageManager(program.id).load(programViewHolder.pic_imageView);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override public void onClick(View view) {
flowController.routeToProgram(program.id);
}
};
programViewHolder.container.setOnClickListener(onClickListener);
programViewHolder.pic_imageView.setOnClickListener(onClickListener);
programViewHolder.title_textView.setText(program.name);
programViewHolder.viewCount_textView.setText(program.views != null ? program.views.toString() : "0");
}
private void bindUserViewHolder(UserViewHolder userViewHolder, final FilterableUser user) {
userViewHolder.name_textView.setText(user.name);
userViewHolder.job_textView.setText(user.description);
imageManagerFactory.user(user.id).thumbnail(userViewHolder.pic_imageView);
//new UserImageManager(user.id).loadProfilePic(userViewHolder.pic_imageView, NetworkUtils.isNetworkAvailable(),
// true);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override public void onClick(View view) {
flowController.routeToUser(user.id);
}
};
userViewHolder.pic_imageView.setOnClickListener(onClickListener);
userViewHolder.container.setOnClickListener(onClickListener);
}
private void bindMediaViewHolder(MediaViewHolder mediaViewHolder, final CatalogMedia media) {
imageManagerFactory.media(media.id, media.type, media.extention).symbolOnThumbnail(mediaViewHolder.complex);
//new MediaImageManager(media).load(mediaViewHolder.pic_imageView, NetworkUtils.isNetworkAvailable(), false);
mediaViewHolder.title_textView.setText(media.title);
mediaViewHolder.authorName_textView.setText(media.authorName);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override public void onClick(final View view) {
flowController.routeToDocument(media.id);
}
};
mediaViewHolder.complex.setOnClickListener(onClickListener);
mediaViewHolder.container.setOnClickListener(onClickListener);
}
private void bindGroupViewHolder(GroupViewHolder groupViewHolder, final FilterableGroup group) {
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override public void onClick(View view) {
flowController.routeToGrouop(group.id);
}
};
groupViewHolder.pic_imageView.setOnClickListener(onClickListener);
groupViewHolder.container.setOnClickListener(onClickListener);
groupViewHolder.name_textView.setText(group.name);
String str = resourcesStringRepository.getQuantityString(R.plurals.catalog_group_stat_program,
group.nbProgramsRunning, group.nbProgramsRunning);
str += " - " + resourcesStringRepository.getQuantityString(R.plurals.catalog_group_stat_user, group.nbUser,
group.nbUser);
groupViewHolder.stats_textView.setText(str);
imageManagerFactory.group(group.id).thumbnail(groupViewHolder.pic_imageView);
//new GroupImageManager(group.id).load(groupViewHolder.pic_imageView, NetworkUtils.isNetworkAvailable(), true);
}
#Override public int getItemCount() {
return displayedList.size();
}
}
It is recycling the views.So while clicking the button you have to store its position and change views accordingly.
Maintain a position storing variable globally like this
private int itemClicked=-1;
While clicking the view store the position into itemclicked
holder.button1.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
itemclicked=position;
assyncCall(statusCallback);
}
});
Then while updating views check if the position is same like this
if(position==itemclicked){
//show download for clicked view
}else{
//show download stopped for other views
}
Solution
As Surender and Trickcy Solution suggested, I updated the presented data and then tell the adapter to update the cell accordingly :
DownloadStatusCallback statusCallback = new DownloadStatusCallback() {
#Override public void started() {
LogDev.i(TAG, "started");
course.downloadState = DownloadState.DOWNLOADING;
final int position = courseViewHolder.getAdapterPosition();
mainThreadHandler.post(new Runnable() {
#Override public void run() {
notifyItemChanged(position);
}
});
}
#Override public void finished() {
course.downloadState = DownloadState.DOWNLOADED;
final int position = courseViewHolder.getAdapterPosition();
mainThreadHandler.post(new Runnable() {
#Override public void run() {
notifyItemChanged(position);
}
});
}
#Override public void error(Exception e) {
course.downloadState = DownloadState.ERROR_WHILE_DOWNLOADING;
final int position = courseViewHolder.getAdapterPosition();
mainThreadHandler.post(new Runnable() {
#Override public void run() {
notifyItemChanged(position);
}
});
}
};

Recycler View showing wrong data when scrolled fast.Have Added Images For The Same

I have a RecycleView where i have three different views for cells. Data is shown perfectly when RView is loaded for the first time. As I start to scroll, it changes the view color. Tried different ways but not able to resolve it.
original view.
wrong view
public class TimelineRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int DATE_LAYOUT = 1;
private static final int EVEN_LAYOUT = 2;
private static final int ODD_LAYOUT = 3;
private List<TimeLineResponseDO.content> contents = new ArrayList<>();
Context context;
LayoutInflater layoutInflater;
private boolean isVisible = false;
private ConnectionDetector connectionDetector;
private ProgressDialog progressDialog;
private Fragment fragment1;
DateFormat inputFormat;
DateFormat outputFormat;
int counterOnCreate = 0;
int counterOnBind = 0;
public interface OpenSubmissionDetails{
void openSubmissionDetails(String submissionId);
}
public interface IGetTimeLineData {
void getTimeLineData(String date);
void loadSnackBar(String msg);
}
public ProgressDialog getProgressDialog(Context context){
if (progressDialog == null) {
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setMessage(context.getString(R.string.loading_message));
}
return progressDialog;
}
IGetTimeLineData getTimeLineData;
public IGetTimeLineData getGetTimeLineData() {
return getTimeLineData;
}
public void setGetTimeLineData(IGetTimeLineData getTimeLineData) {
this.getTimeLineData = getTimeLineData;
}
OpenSubmissionDetails openSubmissionDetails;
public OpenSubmissionDetails getOpenSubmissionDetails() {
return openSubmissionDetails;
}
public void setOpenSubmissionDetails(OpenSubmissionDetails openSubmissionDetails) {
this.openSubmissionDetails = openSubmissionDetails;
}
public TimelineRecyclerAdapter(List<TimeLineResponseDO.content> contents, Context context, Fragment fragment, IGetTimeLineData iGetTimeLineData) {
this.contents = contents;
this.context = context;
this.fragment1 = fragment;
this.getTimeLineData = iGetTimeLineData;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
connectionDetector = new ConnectionDetector(context);
}
public class TimelineDateHolder extends RecyclerView.ViewHolder{
Button date;
public TimelineDateHolder(View convertView) {
super(convertView);
date = (Button) convertView.findViewById(R.id.date);
}
public void bindViews(TimeLineResponseDO.content response){
Date date1 = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("DD-MM-yyyy");
long dateLong = System.currentTimeMillis();
String da = simpleDateFormat.format(dateLong);
Log.e(" Date is :- ", da);
if (response.getDate().equals(da)){
date.setText("Today");
}
else {
if (!TextUtils.isEmpty(response.getDate()))
date.setText(response.getDate());
}
}catch (Exception e){
e.printStackTrace();
if (!TextUtils.isEmpty(response.getDate()))
date.setText(response.getDate());
}
}
}
public class TimelineRecyclerEvenHolder extends RecyclerView.ViewHolder{
RelativeLayout evenContainer;
TextView subjectTypeEven, timingEven, subjectEven, locationEven, teacherEven, cancelTextEven;
Button submissionEven, notificationEven ,submissionEvenDetails ;
ImageView pointerEven, imgCancelBtnEven, imgDelayBtnEven, imgNotificationBtnEven;
View verticalLine;
public TimelineRecyclerEvenHolder(View convertView) {
super(convertView);
evenContainer = (RelativeLayout) convertView.findViewById(R.id.evenContainer);
subjectTypeEven = (TextView) convertView.findViewById(R.id.subjectTypeEven);
timingEven = (TextView) convertView.findViewById(R.id.timingEven);
subjectEven = (TextView) convertView.findViewById(R.id.subjectEven);
locationEven = (TextView) convertView.findViewById(R.id.locationEven);
teacherEven = (TextView) convertView.findViewById(R.id.teacherEven);
pointerEven = (ImageView) convertView.findViewById(R.id.circleEven);
submissionEven = (Button) convertView.findViewById(R.id.submissionEven);
submissionEvenDetails = (Button) convertView.findViewById(R.id.submissionEvenDetails);
imgCancelBtnEven = (ImageView) convertView.findViewById(R.id.cancelBtnEven);//#sp 29-09-2016 added
imgDelayBtnEven = (ImageView) convertView.findViewById(R.id.delayBtnEven);//#sp 30-09-2016 added
imgNotificationBtnEven = (ImageView) convertView.findViewById(R.id.notificationBtnEven);//#sp 30-09-2016 added
notificationEven = (Button) convertView.findViewById(R.id.notificationEven);//#sp 04-10-2016 added
cancelTextEven = (TextView) convertView.findViewById(R.id.cancelTextEven);
subjectTypeEven.setTypeface(NotifyApplication.getExoSemiBold(context));
timingEven.setTypeface(NotifyApplication.getExoSemiBold(context));
subjectEven.setTypeface(NotifyApplication.getExoSemiBold(context));
locationEven.setTypeface(NotifyApplication.getExoSemiBold(context));
teacherEven.setTypeface(NotifyApplication.getExoSemiBold(context));
cancelTextEven.setTypeface(NotifyApplication.getExoSemiBold(context));
}
public void bindViews(TimeLineResponseDO.content response, TimelineRecyclerEvenHolder holder){
holder.evenContainer.setVisibility(View.VISIBLE);
if (!TextUtils.isEmpty(response.getSubjectType())) {
holder.subjectTypeEven.setText(response.getSubjectType());
Log.d("Ifeven", "" + response.getSubjectType());
}
if (response.getLectureSubmission().equals(Enum.LectureSubmission.Submission.toString())) {
holder.subjectTypeEven.setText(Enum.LectureSubmission.Submission.toString());
Log.d("ElseIfeven", "" + response.getSubjectType());
}
Log.d("LectureSubmEven", "" + response.getLectureSubmission());
if (!TextUtils.isEmpty(response.getRoomNo()))
holder.locationEven.setText(response.getRoomNo());
if (!TextUtils.isEmpty(response.getSubject()))
holder.subjectEven.setText(response.getSubject());
if (Boolean.valueOf(StorageService.getInstance().getString("IS_STUDENT")))
{
if (!TextUtils.isEmpty(response.getTeacherAbbreviation()))
holder.teacherEven.setText(response.getTeacherAbbreviation());
}
else if (Boolean.valueOf(StorageService.getInstance().getString("IS_TEACHER")))
{
if (!TextUtils.isEmpty(response.getCourse()))
holder.teacherEven.setText(response.getCourse());
}
if (!TextUtils.isEmpty(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime()))
holder.timingEven.setText(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime());
holder.pointerEven.setBackgroundResource(R.drawable.timeline_pointer_left);
holder.timingEven.setCompoundDrawablesWithIntrinsicBounds(R.drawable.timeline_timing, 0, 0, 0);
Log.d("resp isSubmission", "" + response.isSubmission());
if (response.isSubmission()) {
holder.submissionEven.setVisibility(View.VISIBLE);
holder.submissionEven.setText(response.getSubmissionCount() + " Submission");
} else {
holder.submissionEven.setVisibility(View.GONE);
}
Log.d("resp Noti", "" + response.isNotiification());
if (response.isNotiification()) {
holder.notificationEven.setVisibility(View.VISIBLE);
} else {
holder.notificationEven.setVisibility(View.GONE);
}
if (response.getLectureSubmission().equals("Submission")) {
holder.submissionEvenDetails.setVisibility(View.VISIBLE);
} else {
holder.submissionEvenDetails.setVisibility(View.GONE);
}
if (response.isCancelled()) {
ChangeFontColorEven(holder);
}
if (response.getSubmissionStatus() != null) {
if (response.getSubmissionStatus().equals("No")) {
ChangeFontColorEven(holder);
}
}
if (response.isEvenLayout()) {
if (response.getLectureSubmission().equals("Submission")) {
holder.imgDelayBtnEven.setImageResource(R.drawable.pencil);
} else {
holder.imgDelayBtnEven.setImageResource(R.drawable.delay);
}
}
if (!TextUtils.isEmpty(response.getTrnLectureId())) {
submissionLoadId(response);
}
holder.notificationEven.setText(response.getNotificationCount() + " Notifications");
if (isVisible) {
if (!response.isDateContainer()) {
if (response.getLectureSubmission().equals("Submission") && !TextUtils.isEmpty(response.getLectureSubmission())) {
if (response.getSubmissionStatus().equals("No")) {
ShowHideEditLectureIconsEven(holder, View.GONE);
} else {
ShowHideEditLectureIconsEven(holder, View.VISIBLE);
}
} else {
if (response.isCancelled()) {
ShowHideEditLectureIconsEven(holder, View.GONE);
} else {
ShowHideEditLectureIconsEven(holder, View.VISIBLE);
}
}
}
}else {
ShowHideEditLectureIconsEven(holder, View.GONE);
}
}
}
public class TimelineRecyclerHolder extends RecyclerView.ViewHolder{
RelativeLayout oddContainer;
TextView subjectTypeOdd, timingOdd, subjectOdd, locationOdd, teacherOdd, tvCancelTextOdd;
Button submissionOdd, notificationOdd , submissionOddDetails;
ImageView pointerOdd, imgCancelBtnOdd, imgDelayBtnOdd, imgNotificationBtnOdd;
View verticalLine;
public TimelineRecyclerHolder(View convertView) {
super(convertView);
oddContainer = (RelativeLayout) convertView.findViewById(R.id.oddContainer);
subjectTypeOdd = (TextView) convertView.findViewById(R.id.subjectTypeOdd);
timingOdd = (TextView) convertView.findViewById(R.id.timingOdd);
subjectOdd = (TextView) convertView.findViewById(R.id.subjectOdd);
locationOdd = (TextView) convertView.findViewById(R.id.locationOdd);
teacherOdd = (TextView) convertView.findViewById(R.id.teacherOdd);
pointerOdd = (ImageView) convertView.findViewById(R.id.circleOdd);
submissionOdd = (Button) convertView.findViewById(R.id.submissionOdd);
submissionOddDetails = (Button) convertView.findViewById(R.id.submissionOddDetails);
imgCancelBtnOdd = (ImageView) convertView.findViewById(R.id.cancelBtnOdd);
imgDelayBtnOdd = (ImageView) convertView.findViewById(R.id.delayBtnOdd);
imgNotificationBtnOdd = (ImageView) convertView.findViewById(R.id.notificationBtnOdd);
notificationOdd = (Button) convertView.findViewById(R.id.notificationOdd);
tvCancelTextOdd = (TextView) convertView.findViewById(R.id.cancelTextOdd);
subjectTypeOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
timingOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
subjectOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
locationOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
teacherOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
tvCancelTextOdd.setTypeface(NotifyApplication.getExoSemiBold(context));
}
public void bindViews(TimeLineResponseDO.content response, TimelineRecyclerHolder holder){
//holder.verticalLine.setVisibility(View.VISIBLE);
holder.oddContainer.setVisibility(View.VISIBLE);
if (!TextUtils.isEmpty(response.getSubjectType())) {
holder.subjectTypeOdd.setText(response.getSubjectType());
}
if (response.getLectureSubmission().equals(Enum.LectureSubmission.Submission.toString())) {
holder.subjectTypeOdd.setText(Enum.LectureSubmission.Submission.toString());
}
if (!TextUtils.isEmpty(response.getRoomNo()))
holder.locationOdd.setText(response.getRoomNo());
if (!TextUtils.isEmpty(response.getSubject()))
holder.subjectOdd.setText(response.getSubject());
if (Boolean.valueOf(StorageService.getInstance().getString("IS_STUDENT")))
{
if (!TextUtils.isEmpty(response.getTeacherAbbreviation()))
holder.teacherOdd.setText(response.getTeacherAbbreviation());
}
else if (Boolean.valueOf(StorageService.getInstance().getString("IS_TEACHER")))
{
if (!TextUtils.isEmpty(response.getCourse()))
holder.teacherOdd.setText(response.getCourse());
}
if (!TextUtils.isEmpty(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime()))
holder.timingOdd.setText(response.getStartTime() + context.getString(R.string.timelineHypen) + response.getEndTime());
holder.pointerOdd.setBackgroundResource(R.drawable.timeline_pointer_right);
holder.timingOdd.setCompoundDrawablesWithIntrinsicBounds(R.drawable.timeline_timing, 0, 0, 0);
if (response.isSubmission()) {
holder.submissionOdd.setVisibility(View.VISIBLE);
holder.submissionOdd.setText(response.getSubmissionCount() + " Submission");
} else {
holder.submissionOdd.setVisibility(View.GONE);
}
if (response.isNotiification()) {
holder.notificationOdd.setVisibility(View.VISIBLE);
} else {
holder.notificationOdd.setVisibility(View.GONE);
}
if (response.getLectureSubmission().equals("Submission")) {
holder.submissionOddDetails.setVisibility(View.VISIBLE);
} else {
holder.submissionOddDetails.setVisibility(View.GONE);
}
if (response.isCancelled()) {
ChangeFontColor(holder, Enum.LayoutType.Odd);
}
if (response.getSubmissionStatus() != null) {
if (response.getSubmissionStatus().equals("No")) {
ChangeFontColor(holder, Enum.LayoutType.Odd);
}
}
if (response.getLectureSubmission().equals("Submission")) {
holder.imgDelayBtnOdd.setImageResource(R.drawable.pencil);
} else {
holder.imgDelayBtnOdd.setImageResource(R.drawable.delay);
}
if (!TextUtils.isEmpty(response.getTrnLectureId())) {
submissionLoadId(response);
}
holder.notificationOdd.setText(response.getNotificationCount() + " Notifications");
if (isVisible) {
if (!response.isDateContainer()) {
if (response.getLectureSubmission().equals("Submission") && !TextUtils.isEmpty(response.getLectureSubmission())) {
if (response.getSubmissionStatus().equals("No")) {
ShowHideEditLectureIcons(holder, View.GONE);
} else {
ShowHideEditLectureIcons(holder, View.VISIBLE);
}
} else {
if (response.isCancelled()) {
ShowHideEditLectureIcons(holder, View.GONE);
} else {
ShowHideEditLectureIcons(holder, View.VISIBLE);
}
}
}
}else {
ShowHideEditLectureIcons(holder, View.GONE);
}
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case DATE_LAYOUT:
View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.timeline_date_header, parent, false);
return new TimelineDateHolder(convertView);
case EVEN_LAYOUT:
View convertView1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.timeline_even_layout_child, parent, false);
return new TimelineRecyclerEvenHolder(convertView1);
default:
View convertView2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.timeline_odd_layout_child, parent, false);
return new TimelineRecyclerHolder(convertView2);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int itemType = getItemViewType(position);
TimeLineResponseDO.content content = contents.get(position);
switch (itemType){
case DATE_LAYOUT:
((TimelineDateHolder) holder).bindViews(content);
break;
case EVEN_LAYOUT:
((TimelineRecyclerEvenHolder) holder).bindViews(content, (TimelineRecyclerEvenHolder) holder);
break;
default:
((TimelineRecyclerHolder) holder).bindViews(content, (TimelineRecyclerHolder) holder);
break;
}
if (position == (contents.size() - 1)) {
if (connectionDetector.isConnectingToInternet()) {
getTimeLineData.getTimeLineData(content.getContentDate());
}
else {
getTimeLineData.loadSnackBar(context.getString(R.string.no_internet_connection_schedule));
}
}
}
#Override
public int getItemCount() {
return contents.size();
}
#Override
public int getItemViewType(int position) {
if (contents.get(position).isDateContainer()){
return DATE_LAYOUT;
}
else if (contents.get(position).isEvenLayout()){
return EVEN_LAYOUT;
}
else {
return ODD_LAYOUT;
}
}
private void ChangeFontColor(TimelineRecyclerHolder holder, Enum.LayoutType layoutType) {
switch (layoutType){
case Even:
break;
case Odd:
holder.subjectTypeOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.timingOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.pointerOdd.setBackgroundResource(R.drawable.timeline_pointer_right_red);
holder.subjectOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.locationOdd.setVisibility(View.GONE);
holder.teacherOdd.setVisibility(View.GONE);
holder.tvCancelTextOdd.setVisibility(View.VISIBLE);
holder.tvCancelTextOdd.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.submissionOdd.setVisibility(View.GONE);
holder.submissionOddDetails.setVisibility(View.GONE);
break;
}
}
private void ChangeFontColorEven(TimelineRecyclerEvenHolder holder) {
holder.subjectTypeEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.timingEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.pointerEven.setBackgroundResource(R.drawable.timeline_pointer_left_red);
holder.subjectEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.locationEven.setVisibility(View.GONE);
holder.teacherEven.setVisibility(View.GONE);
holder.cancelTextEven.setVisibility(View.VISIBLE);
holder.cancelTextEven.setTextColor(context.getResources().getColor(android.R.color.holo_red_dark));
holder.submissionEven.setVisibility(View.GONE);
holder.submissionEvenDetails.setVisibility(View.GONE);
}
private void ShowHideEditLectureIcons(TimelineRecyclerHolder holder, int visibility) {
holder.imgCancelBtnOdd.setVisibility(visibility);
holder.imgDelayBtnOdd.setVisibility(visibility);
holder.imgNotificationBtnOdd.setVisibility(visibility);
}
private void ShowHideEditLectureIconsEven(TimelineRecyclerEvenHolder holder, int visibility) {
holder.imgCancelBtnEven.setVisibility(visibility);
holder.imgDelayBtnEven.setVisibility(visibility);
holder.imgNotificationBtnEven.setVisibility(visibility);
}
public void editEvent(boolean isVisible) {
this.isVisible = isVisible;
Log.d("Is Visible", String.valueOf(isVisible));
notifyDataSetChanged();
}
public void submission(final TimeLineResponseDO.content timeLineModel) {
final IGetSubmissionDetails iGetSubmissionDetails = new GetSubmissionDetailsBO();
iGetSubmissionDetails.getSubmissionByTrn(context, timeLineModel.getTrnLectureId(), new Response<ArrayList<String>>() {
#Override
public void onSuccess(ArrayList<String> callback) throws IOException {
StorageService.getInstance().putString("SubmissionId", callback.get(0));
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
NoticeBoard noticeBoard = new NoticeBoard();
Bundle bundle = new Bundle();
bundle.putStringArrayList("submissionIds", callback);
bundle.putString("submissionId",callback.get(0));
bundle.putString("isFrom","submission_view");
noticeBoard.setArguments(bundle);
fragmentTransaction.replace(R.id.container, noticeBoard, NoticeBoard.class.getSimpleName());
fragmentTransaction.addToBackStack(NoticeBoard.class.getSimpleName());
fragmentTransaction.commit();
}
#Override
public void onError(Exception e, int i, String msg) {
Log.d("trnId error",e.toString());
}
});
}
public void submissionLoadId(final TimeLineResponseDO.content timeLineModel) {
final IGetSubmissionDetails iGetSubmissionDetails = new GetSubmissionDetailsBO();
iGetSubmissionDetails.getSubmissionByTrn(context, timeLineModel.getTrnLectureId(), new ResponseNew<ArrayList<String>>() {
#Override
public void onSuccess(ArrayList<String> callback, String id) throws IOException {
for (int i = 0; i < callback.size(); i++){
Log.e("trnId error", id + " " + callback.get(i));
if (AppConstant.initializeDbORReturn(context).isSubmissionIdExists(id, callback.get(i))){
Log.e("Already Exists :- ", id + " " + callback.get(i));
}
else {
AppConstant.initializeDbORReturn(context).insertSubmissionId(id, callback.get(i));
}
}
}
#Override
public void onError(Exception e, int i, String msg) {
}
});
}
private void localTimeLinedb(List<TimeLineResponseDO> callback, Context context) {
for (int i = 0; i < callback.size(); i++) {
TimeLineResponseDO timeLineResponseDO = callback.get(i);
for (int j = 0; j < timeLineResponseDO.getContentList().size(); j++) {
TimeLineResponseDO.content content = timeLineResponseDO.getContentList().get(j);
if (AppConstant.initializeDbORReturn(context).isTimeLineExits(timeLineResponseDO.getDate(),
content.getStaticLectureId(), content.getDate(),
content.getTrnLectureId(), content.getSlotId(), content.getSubject(),
content.getSubmissionId(), content.getLectureSubmission())) {
AppConstant.initializeDbORReturn(context).updateTimeLine(content, timeLineResponseDO.getDate());
} else {
AppConstant.initializeDbORReturn(context).insertIntoTimeLine(content, timeLineResponseDO.getDate());
}
}
}
}
public void getTimeLineData(String date, final Context context, final TimelineRecyclerHolder holder, final Enum.LayoutType even_odd, final Enum.EditLectureType editLectureType) {
inputFormat = new SimpleDateFormat("dd-mm-yyyy");
outputFormat = new SimpleDateFormat("yyyy-mm-dd");
ITimeLine timeLine = new TimeLineBO();
TimeLinePostDO timeLinePostDO = new TimeLinePostDO();
if (date == null) {
if (!TextUtils.isEmpty(StorageService.getInstance().getString("Running_Teacher_Id"))){
timeLinePostDO.setUserId(StorageService.getInstance().getString("Running_Teacher_Id"));
}
else {
timeLinePostDO.setUserId(StorageService.getInstance().getString("NEWUSER_ID"));
}
timeLinePostDO.setUserRoles(StorageService.getInstance().getString("USER_ROLES"));
} else {
if (!TextUtils.isEmpty(StorageService.getInstance().getString("Running_Teacher_Id"))){
timeLinePostDO.setUserId(StorageService.getInstance().getString("Running_Teacher_Id"));
}
else {
timeLinePostDO.setUserId(StorageService.getInstance().getString("NEWUSER_ID"));
}
timeLinePostDO.setUserRoles(StorageService.getInstance().getString("USER_ROLES"));
try {
timeLinePostDO.setDate(outputFormat.format(inputFormat.parse(date)));
} catch (ParseException e) {
e.printStackTrace();
}
}
timeLine.getTimeLineData(context, timeLinePostDO, new Response<List<TimeLineResponseDO>>() {
#Override
public void onSuccess(List<TimeLineResponseDO> callback) {
localTimeLinedb(callback, context);
getProgressDialog(context).dismiss();
switch (editLectureType){
case Cancel:
ChangeFontColor(holder, even_odd);
Toast.makeText(context, "Lecture Cancelled", Toast.LENGTH_SHORT).show();
ShowHideEditLectureIcons(holder, View.GONE);
break;
case Delay:
Toast.makeText(context, "Notification sent for delay", Toast.LENGTH_SHORT).show();
break;
case CreateNotification:
Toast.makeText(context, "Notification sent", Toast.LENGTH_SHORT).show();
break;
}
if (fragment1 instanceof TimeLineFragment){
((TimeLineFragment) fragment1).refreshList();
}
}
#Override
public void onError(Exception e, int i, String msg) {
}
});
}
}
Thanks in advance !
RecyclerViewAdapter recycles its views and thus the behavior is much expected.
When I read your code I saw that you've written if statements without else like :
if (!TextUtils.isEmpty(response.getRoomNo()))
holder.locationEven.setText(response.getRoomNo());
if (!TextUtils.isEmpty(response.getSubject()))
holder.subjectEven.setText(response.getSubject());
These will be reused if the view in the position before the newly loaded view doesn't fulfill that if statement.
Try adding else to your every if statement. By adding else I mean add something like
else {
holder.locationEven.setText("");
}
// OR
else {
holder.locationEven.setVisibility(View.GONE);
}

Categories

Resources