In my application I want to use recyclerView
to show some data from the server.
My list is:
item1, item2, item3, item4, item5
item1 is oldest and item5 is newest .
I want first sort item such as item5, item4, item3, item2, item1
then add into adapter
.
item1 is oldest and item5 is newest .
I want first sort item such as item5, item4, item3, item2, item1
then add into adapter
.
I fill adapter data with this code into OnResponse on Retrofit :
//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();
}
My adapter codes:
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);
}
}
}
How can i solve this?
您可以使用它来反转设置适配器的列表
Collections.reverse(yourList);
You can use reverse()
method from Collections
Collections.reverse(your_list);
your_recyclerview.setAdapter(your_list);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.