我有一个使用Recycler视图的列表。该列表有3种不同类型的项目
当loadmore点击时我想在同一个标题中显示所有剩余的实际项目但是当我调用notifydatasetchanged时列表没有刷新,
所有剩余的实际项目仅在我将loadmore滚动到不可见位置(顶部或底部)并向后滚动后显示,但列表计数(getItemCount)未更新,因此在loadmore clicked之前出现的某些项目将消失
注意:R.layout.nearby_deal_item
和R.layout.last_nearby_deal_item
相同的项目类型(2.实际项目)在某些视图中只有一些不同的边距
这是我的代码
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);
}
}
}
以下是我的所有尝试(在// 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);