繁体   English   中英

在android中添加到recyclerView适配器之前如何反转列表

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM