[英]How to add layout_params on each item on recyclerview?
I have a List <>
of items, these items are part of a model class. 我有一个项目的List <>
,这些项目是模型类的一部分。 Whenever an item is added to the List <>
, I would like to add layout params like margin_left
or margin_right
for only the added item. 每当将项目添加到List <>
,我margin_right
为添加的项目添加诸如margin_left
或margin_right
类的布局参数。
{....}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{
Comment com = mList.get(position);
}
@Override
public int getItemCount() {
return mList.size();
}
It can be done by using RecyclerView.Adapter
's getItemViewType(int position)
method. 可以使用RecyclerView.Adapter
的getItemViewType(int position)
方法来完成。 It gets adapter position and returns int viewType which can be used later in onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
to inflate layout according to viewType
. 它获取适配器位置,并返回int viewType ,以后可以在onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
以根据viewType
膨胀布局。 You can learn more about it here . 您可以在此处了解更多信息。 Bellow how I could achieve setting LayoutParams
for every item individually: 在下面,我如何实现分别为每个项目设置LayoutParams
的方法:
An interface to mark an object as object with LayoutParams
: 使用LayoutParams
将对象标记为对象的接口:
interface Parameterized {
RecyclerView.LayoutParams getParams();
}
The model class: 模型类:
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;
}
}
Using setParams()
method, we are able to set RecyclerView.LayoutParams
to a comment object, that can be used later by an adapter later on. 使用setParams()
方法,我们可以将RecyclerView.LayoutParams
设置为注释对象,以后可以由适配器使用。
The adapter: 适配器:
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);
}
}
}
Usage in an activity / a fragment as an example: 以活动/片段的用法为例:
@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;
}
Adapter's updateComments(List<Comment> comments)
method updates all the list, and addComment(Comment comment)
adds an item with params at the end of the list. 适配器的updateComments(List<Comment> comments)
方法将更新所有列表,而addComment(Comment comment)
将在列表末尾添加带有参数的项目。
Here haw does this look like: 这看起来像吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.