Recyclerview notifydatasetchanged not working
I have a list using recycler view. List has 3 different types of elements
- (near xxx, in xxx, etc.)
- actual item (black rectangle)
- footer (loadmore) -> footer only if the actual element in one header is more than 3
when loadmore clicked I want to show all remaining actual item in the same title, but when I call notifydatasetchanged the list is not updated,
all remaining actual data was only shown after I scrolled loadmore to an invisible position (top or bottom) and scrolled back
but the list count (getItemCount) is not updated so some items that appear before loading will be removed Note: R.layout.nearby_deal_item and R.layout.last_nearby_deal_item are the same item type (2.actual item) but some have there are only a few fields
here is my code
public class NearbyPromoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<List<String>> list;
private List<Boolean> isLoadMore;
private Context context;
public NearbyPromoAdapter(List<List<String>> list, Context context) {
this.context = context;
this.list = list;
isLoadMore = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
isLoadMore.add(false);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (i == 1) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.nearby_deal_header, viewGroup,
false);
HeaderViewHolder vh = new HeaderViewHolder(v);
return vh;
} else if (i == 2) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.nearby_deal_item, viewGroup,
false);
ViewHolder vh = new ViewHolder(v);
return vh;
} else if (i == 3) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.last_nearby_deal_item, viewGroup,
false);
ViewHolder vh = new ViewHolder(v);
return vh;
} else {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.nearby_deal_footer, viewGroup,
false);
FooterViewHolder vh = new FooterViewHolder(v);
vh.index = -i;
return vh;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof ViewHolder) {
ViewHolder holder = (ViewHolder) viewHolder;
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, PromoDetailActivity.class);
context.startActivity(intent);
}
});
} else if (viewHolder instanceof HeaderViewHolder) {
HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
holder.firstText.setText(getItem(i));
} else if (viewHolder instanceof FooterViewHolder) {
final FooterViewHolder holder = (FooterViewHolder) viewHolder;
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isLoadMore.set(holder.index, true);
final NearbyPromoAdapter adapter = NearbyPromoAdapter.this;
// TODO
}
});
}
}
private class RefreshTask extends AsyncTask<NearbyPromoAdapter, NearbyPromoAdapter,
NearbyPromoAdapter> {
protected NearbyPromoAdapter doInBackground(NearbyPromoAdapter... adapter) {
return adapter[0];
}
protected void onPostExecute(NearbyPromoAdapter result) {
result.notifyDataSetChanged();
}
}
@Override
public long getItemId(int i) {
return 0;
}
public String getItem(int position) {
int index = 0;
for (int i = 0; i < list.size(); i++) {
if (index == position) {
return "Title " + i;
}
index++;
if (list.get(i).size() <= 3) {
for (int j = 0; j < list.get(i).size(); j++) {
if (index == position) {
return list.get(i).get(j);
}
index++;
}
} else if (isLoadMore.get(i) == false) {
for (int j = 0; j < 4; j++) {
if (index == position) {
if (j == 3)
return "Footer";
else
return list.get(i).get(j);
}
index++;
}
} else {
for (int j = 0; j < list.get(i).size(); j++) {
if (index == position) {
return list.get(i).get(j);
}
index++;
}
}
}
return null;
}
@Override
public int getItemViewType(int position) {
int index = 0;
for (int i = 0; i < list.size(); i++) {
if (index == position) {
return 1;
}
index++;
if (list.get(i).size() <= 3) {
for (int j = 0; j < list.get(i).size(); j++) {
if (index == position) {
if (j == list.get(i).size() - 1)
return 3;
else
return 2;
}
index++;
}
} else if (isLoadMore.get(i) == false) {
for (int j = 0; j < 4; j++) {
if (index == position) {
if (j == 3)
return -i;
else
return 2;
}
index++;
}
} else {
for (int j = 0; j < list.get(i).size(); j++) {
if (index == position) {
if (j == list.get(i).size() - 1)
return 3;
else
return 2;
}
index++;
}
}
}
return 999;
}
@Override
public int getItemCount() {
int count = list.size();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).size() <= 3) {
count += list.get(i).size();
} else if (isLoadMore.get(i) == false) {
count += 4;
} else {
count += list.get(i).size();
}
}
return count;
}
public List<List<String>> getList() {
return list;
}
public void setList(List<List<String>> list) {
this.list = list;
isLoadMore.clear();
for (int i = 0; i < list.size(); i++) {
isLoadMore.add(false);
}
}
}
below are all my attempts (with // TODO)
adapter.notifyDataSetChanged();
NearbyPromoActivity act = (NearbyPromoActivity) context;
act.runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
new RefreshTask().equals(adapter);
+3
source to share
No one has answered this question yet
Check out similar questions: