[英]How to add dynamic views to android layout in recyclerview adapter
[英]How to reverse list before add to recyclerView adapter in android
在我的應用程序中,我想使用recyclerView
來顯示來自服務器的一些數據。
我的清單是:
項目 1、項目 2、項目 3、項目 4、項目 5
item1是最舊的,而item5是最新的。
我想先對item5, item4, item3, item2, item1
等項目進行排序item5, item4, item3, item2, item1
然后添加到adapter
。
item1是最舊的,而item5是最新的。
我想先對item5, item4, item3, item2, item1
等項目進行排序item5, item4, item3, item2, item1
然后添加到adapter
。
我使用此代碼將適配器數據填充到 OnResponse onRetrofit 中:
//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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.