如何在添加到 android 中的 recyclerView 适配器之前反转列表
How to reverse list before add to recyclerView adapter in android
在我的应用程序中,我想使用 recyclerView
来显示来自服务器的一些数据。
我的名单是:
item1, item2, item3, item4, item5
item1 是 最旧 并且 item5 是 最新.
我想要先排序项目,例如 item5, item4, item3, item2, item1
然后添加到 adapter
.
item1 是 最旧 并且 item5 是 最新.
我想要先排序项目,例如 item5, item4, item3, item2, item1
然后添加到 adapter
.
我使用此代码将适配器数据填充到 Retrofit 的 OnResponse 中:
//Comments
if (postResponse.getCommentCount() == 0) {
detailPage_commentsSellAll.setVisibility(View.GONE);
detailPage_commentEmpty.setVisibility(View.VISIBLE);
detailPage_commentList.setVisibility(View.GONE);
} else {
detailPage_commentsSellAll.setVisibility(View.VISIBLE);
detailPage_commentEmpty.setVisibility(View.GONE);
detailPage_commentList.setVisibility(View.VISIBLE);
detailPage_commentsTitle.setText(getString(R.string.comments) + " (" + postResponse.getCommentCount() + ")");
commentsModel.clear();
commentsModel.addAll(postResponse.getComments());
commentAdapter.notifyDataSetChanged();
}
我的适配器代码:
public class DetailCommentAdapter extends RecyclerView.Adapter<DetailCommentAdapter.ViewHolder> {
private List<CommentsItem> model;
private Context context;
public DetailCommentAdapter(List<CommentsItem> model) {
this.model = model;
}
@Override
public DetailCommentAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_comment_row, viewGroup, false);
context = viewGroup.getContext();
return new DetailCommentAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final DetailCommentAdapter.ViewHolder viewHolder, int position) {
int newPos = model.size() - 1;
//Image
if (!TextUtils.isEmpty(model.get(newPos).getUrl())) {
Glide.with(context)
.asBitmap()
.load(model.get(position).getUrl())
.into(new BitmapImageViewTarget(viewHolder.detailCmRow_img) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
viewHolder.detailCmRow_img.setImageDrawable(circularBitmapDrawable);
}
});
} else {
viewHolder.detailCmRow_img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.default_avatar));
}
//Name
viewHolder.detailCmRow_name.setText(model.get(newPos).getName());
//Date
viewHolder.detailCmRow_date.setText(model.get(newPos).getDate());
//Comment
viewHolder.detailCmRow_comment.setText(Html.fromHtml(model.get(newPos).getContent()));
}
@Override
public int getItemCount() {
return 1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.detailCmRow_img)
ImageView detailCmRow_img;
@BindView(R.id.detailCmRow_name)
TextView detailCmRow_name;
@BindView(R.id.detailCmRow_date)
TextView detailCmRow_date;
@BindView(R.id.detailCmRow_comment)
TextView detailCmRow_comment;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
我该如何解决这个问题?
您可以使用它来反转您设置适配器的列表
Collections.reverse(yourList);
您可以使用 Collections
中的 reverse()
方法
Collections.reverse(your_list);
your_recyclerview.setAdapter(your_list);
在我的应用程序中,我想使用 recyclerView
来显示来自服务器的一些数据。
我的名单是:
item1, item2, item3, item4, item5
item1 是 最旧 并且 item5 是 最新.
我想要先排序项目,例如 item5, item4, item3, item2, item1
然后添加到 adapter
.
item1 是 最旧 并且 item5 是 最新.
我想要先排序项目,例如 item5, item4, item3, item2, item1
然后添加到 adapter
.
我使用此代码将适配器数据填充到 Retrofit 的 OnResponse 中:
//Comments
if (postResponse.getCommentCount() == 0) {
detailPage_commentsSellAll.setVisibility(View.GONE);
detailPage_commentEmpty.setVisibility(View.VISIBLE);
detailPage_commentList.setVisibility(View.GONE);
} else {
detailPage_commentsSellAll.setVisibility(View.VISIBLE);
detailPage_commentEmpty.setVisibility(View.GONE);
detailPage_commentList.setVisibility(View.VISIBLE);
detailPage_commentsTitle.setText(getString(R.string.comments) + " (" + postResponse.getCommentCount() + ")");
commentsModel.clear();
commentsModel.addAll(postResponse.getComments());
commentAdapter.notifyDataSetChanged();
}
我的适配器代码:
public class DetailCommentAdapter extends RecyclerView.Adapter<DetailCommentAdapter.ViewHolder> {
private List<CommentsItem> model;
private Context context;
public DetailCommentAdapter(List<CommentsItem> model) {
this.model = model;
}
@Override
public DetailCommentAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail_comment_row, viewGroup, false);
context = viewGroup.getContext();
return new DetailCommentAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final DetailCommentAdapter.ViewHolder viewHolder, int position) {
int newPos = model.size() - 1;
//Image
if (!TextUtils.isEmpty(model.get(newPos).getUrl())) {
Glide.with(context)
.asBitmap()
.load(model.get(position).getUrl())
.into(new BitmapImageViewTarget(viewHolder.detailCmRow_img) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
viewHolder.detailCmRow_img.setImageDrawable(circularBitmapDrawable);
}
});
} else {
viewHolder.detailCmRow_img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.default_avatar));
}
//Name
viewHolder.detailCmRow_name.setText(model.get(newPos).getName());
//Date
viewHolder.detailCmRow_date.setText(model.get(newPos).getDate());
//Comment
viewHolder.detailCmRow_comment.setText(Html.fromHtml(model.get(newPos).getContent()));
}
@Override
public int getItemCount() {
return 1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.detailCmRow_img)
ImageView detailCmRow_img;
@BindView(R.id.detailCmRow_name)
TextView detailCmRow_name;
@BindView(R.id.detailCmRow_date)
TextView detailCmRow_date;
@BindView(R.id.detailCmRow_comment)
TextView detailCmRow_comment;
public ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
我该如何解决这个问题?
您可以使用它来反转您设置适配器的列表
Collections.reverse(yourList);
您可以使用 Collections
reverse()
方法
Collections.reverse(your_list);
your_recyclerview.setAdapter(your_list);