I see a similar question here AnimationDrawable in RecyclerView item not animating on notifyDataSetChanged. But couldn't resolve this issue or fix. I think its because notifyDataSetChanged call multiple times to update UI. While I comment or notification change call for the position it works fine. I have three items in the recycled view.
- Progress bar
- Text view
- Button
I need to call an API on button click and increment the textview value. while API calls I need to show the progress bar and hide while API finish
Button click as follow,
holder.image_add_overlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.progressBar2.setVisibility(View.VISIBLE);
UpdateProductItem(holder, position);
}
});
private void UpdateProductItem(ShoppinglistAdapter.ViewHolder holder, int position) {
try {
Boolean value = CartFunctions.UpdateCartPostValue(cartAdd, mAPIService);
if (value) {
holder.text_item_cart_count.setText("" + holder.cart_count_value);
// notifyDataSetChanged();
holder.progressBar2.setVisibility(View.GONE);
Task task = new Task();
task.execute();
Toast.makeText(context, context.getResources().getString(R.string.cart_update_toast_message), Toast.LENGTH_SHORT).show();
} else {
holder.cart_count_value = holder.cart_count_value - 1;
}
} catch (IOException e) {
e.printStackTrace();
}
}
I also try the
@SuppressLint("StaticFieldLeak")
public class Task extends AsyncTask<String, Integer, Integer> {
@Override
protected Integer doInBackground(String... strings) {
Log.i("test", "doInBackground");
// notifyDataSetChanged();
return null;
}
@Override
protected void onPostExecute(Integer integer) {
Log.i("test", "onPostExecute");
notifyDataSetChanged();
super.onPostExecute(integer);
}
}
And call the this using
Task task = new Task();
//task.doInBackground();
task.onPostExecute(0);
May the 2 item in recyclerview are same, so the count is shown to both become same. so i need notifyDataSetChanged() instead of notifyItemChanged(position); Did any guys know the solution?