如何在recyclerview的每个项目上添加layout_params?

时间:2019-07-06 10:17:33

标签: android list android-recyclerview

我有List <>个项目,这些项目是模型类的一部分。每当将项目添加到List <>时,我都想添加布局参数 例如margin_leftmargin_right仅用于添加的项目。

{....} 
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {   
        Comment com = mList.get(position);
    }
    @Override
    public int getItemCount() {
        return mList.size();
    }

1 个答案:

答案 0 :(得分:0)

可以使用RecyclerView.Adapter的{​​{1}}方法来完成。它获取适配器位置并返回 int viewType ,稍后可在getItemViewType(int position)中使用,以根据onCreateViewHolder(@NonNull ViewGroup parent, int viewType)扩大布局。您可以进一步了解here。在下面,我如何实现分别为每个项目设置viewType

使用LayoutParams将对象标记为对象的接口:

LayoutParams

模型类:

interface Parameterized {

    RecyclerView.LayoutParams  getParams();
}

使用public class Comment implements Parameterized { private String content; private RecyclerView.LayoutParams params; public Comment(String content) { this.content = content; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public RecyclerView.LayoutParams getParams() { return params; } public void setParams(RecyclerView.LayoutParams params) { this.params = params; } } 方法,我们可以将setParams()设置为注释对象,以后可以由适配器使用。

适配器:

RecyclerView.LayoutParams

使用活动/片段作为示例:

public class CommentRecyclerAdapter extends RecyclerView.Adapter<CommentRecyclerAdapter.MyViewHolder> {

    private List<Comment> comments = new ArrayList<>();

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_name, parent, false);
        v.setLayoutParams(comments.get(viewType).getParams());
        return new MyViewHolder(v);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Comment comment = comments.get(position);
        holder.tvComment.setText(comment.getContent());
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    public void updateComments(List<Comment> comments) {
        this.comments = comments;
        notifyDataSetChanged();
    }

    public void addComment(Comment comment) {
        comments.add(comment);
        notifyItemInserted(comments.size());
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvComment;

        MyViewHolder(@NonNull View itemView) {
            super(itemView);

            tvComment = itemView.findViewById(R.id.tv_comment);
        }
    }
}

适配器的@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); recyclerView = findViewById(R.id.rv_comments); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new CommentRecyclerAdapter(); recyclerView.setAdapter(adapter); adapter.updateComments(getGeneratedComments()); Comment comment = new Comment("Added comment"); RecyclerView.LayoutParams params = new RecyclerView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT ); params.rightMargin = 30; params.leftMargin = 30; params.topMargin = 50; comment.setParams(params); adapter.addComment(comment); } private List<Comment> getGeneratedComments() { List<Comment> comments = new ArrayList<>(); for (int i = 1; i < 7; i++) { Comment comment = new Comment("Comment " + i); RecyclerView.LayoutParams params = new RecyclerView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT ); params.topMargin = 20 * i; comment.setParams(params); comments.add(comment); } return comments; } 方法更新所有列表,updateComments(List<Comment> comments)在列表末尾添加带有参数的项目。

这里看起来像吗?

example_screenshot