在性能方面,什么是在Android应用程序中实现的更好选项:
-
多个适配器,用于每个具有不同布局的对象列表,
-
单个适配器,处理不同的数据 .
我们假设我们需要创造
-
评论,
-
视频,
-
问题
在适配器中列出正确显示数据 . 每个列表属于不同的布局,因此我们有不同的 TextViews
, ImageViews
等 .
公共类FilesAdapter扩展了RecyclerView.Adapter {
public static final int VIDEO_FILES_ADAPTER = 1; public static final int COMMENT_ADAPTER = 2; public static final int QUESTIONS_ADAPTER = 3;
private int CURRENT_ADAPTER;
私人列表videoFiles;私人清单评论;私人清单问题;
boolean isForUser;
public FilesAdapter(int currentAdapter,List videoFiles,boolean isForUser)
public FilesAdapter(int currentAdapter,List comments)
public FilesAdapter(int currentAdapter,List questions,int unusedVariable)
@NonNull @Override public FilesViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){View view; FilesViewHolder viewHolder;
switch(CURRENT_ADAPTER){
case VIDEO_FILES_ADAPTER:
if(isForUser){
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_layout_file_user, parent, false);
}else{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_layout_file_main, parent, false);
}
viewHolder = new FilesViewHolder(VIDEO_FILES_ADAPTER, view, isForUser);
return viewHolder;
case COMMENT_ADAPTER:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_layout_comment, parent, false);
viewHolder = new FilesViewHolder(COMMENT_ADAPTER, view, false);
return viewHolder;
case QUESTIONS_ADAPTER:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_layout_question, parent, false);
viewHolder = new FilesViewHolder(QUESTIONS_ADAPTER, view, false);
return viewHolder;
}
return null;
}
@Override public void onBindViewHolder(@NonNull FilesViewHolder holder,int position){
switch (CURRENT_ADAPTER){
case VIDEO_FILES_ADAPTER:
holder.videoTitle.setText(videoFiles.get(position).getFileName());
holder.videoDescription.setText(videoFiles.get(position).getFileDescription());
if(isForUser){
holder.videoDate.setText(videoFiles.get(position).getFileCreatedAt());
}else{
holder.videoUser.setText(videoFiles.get(position).getUsername());
}
holder.videoCount.setText(videoFiles.get(position).getWatched());
break;
case COMMENT_ADAPTER:
holder.commentComment.setText(comments.get(position).getComment());
holder.commentUsername.setText(comments.get(position).getUsername());
holder.commentCreatedAt.setText(comments.get(position).getCreatedAt());
break;
case QUESTIONS_ADAPTER:
holder.questionTitle.setText(questions.get(position).getProviderUsername());
break;
}
}
@Override public int getItemCount(){
switch (CURRENT_ADAPTER){
case VIDEO_FILES_ADAPTER:
return videoFiles.size();
case COMMENT_ADAPTER:
return comments.size();
case QUESTIONS_ADAPTER:
return questions.size();
}
return 0;
}
public static class FilesViewHolder扩展了RecyclerView.ViewHolder {
private static final int VIDEO_FILES_ADAPTER = 1;
private static final int COMMENT_ADAPTER = 2;
private static final int QUESTIONS_ADAPTER = 3;
private int CURRENT_ADAPTER;
// Related to audio file class
public TextView videoTitle, videoDescription, videoDate, videoUser, videoCount;
// Related to comment class
private TextView commentComment, commentUsername, commentCreatedAt;
// Related to subscription class
private TextView questionTitle;
// @param isForUser applies only if CURRENT_ADAPTER == VIDEO_FILES_ADAPTER
public FilesViewHolder(int CURRENT_ADAPTER, View itemView, boolean isForUser) {
super(itemView);
switch(CURRENT_ADAPTER){
case VIDEO_FILES_ADAPTER:
videoTitle = itemView.findViewById(R.id.tV_title);
videoDescription = itemView.findViewById(R.id.tV_description);
if(isForUser){
videoDate = itemView.findViewById(R.id.tV_date);
}else{
videoUser = itemView.findViewById(R.id.tV_username);
}
videoCount = itemView.findViewById(R.id.tV_watched);
break;
case COMMENT_ADAPTER:
commentComment = itemView.findViewById(R.id.tV_comment);
commentUsername = itemView.findViewById(R.id.tV_comment_username);
commentCreatedAt = itemView.findViewById(R.id.tV_comment_created_at);
break;
case QUESTIONS_ADAPTER:
questionTitle = itemView.findViewById(R.id.tV_question);
break;
}
}
}}
还有另一种方法可以做到这一点 . 每个列表的单个适配器例如:
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder>{
private List<Comment> commentList;
public CommentAdapter(List<Comment> comments){
this.commentList = comments;
}
public static class CommentViewHolder extends RecyclerView.ViewHolder{
private TextView comment, username, createdAt;
public CommentViewHolder(View itemView) {
super(itemView);
comment = itemView.findViewById(R.id.tV_comment);
username = itemView.findViewById(R.id.tV_comment_username);
createdAt = itemView.findViewById(R.id.tV_comment_created_at);
}
}
@NonNull
@Override
public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_layout_comment, parent, false);
return new CommentViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
holder.comment.setText(commentList.get(position).getComment());
holder.username.setText(commentList.get(position).getUsername());
holder.createdAt.setText(commentList.get(position).getCreatedAt());
}
@Override
public int getItemCount() {
return commentList.size();
}
}
我的问题是在性能方面哪些更好实施?如果我决定为不同的列表实现一个适配器,是否会出现明显的性能延迟或补丁?
1 回答
无论您的布局和处理附加到单元格的复杂性如何,我建议您为每个列表使用单个适配器 .
只是一张小图片来说明RecyclerView的主要操作:RecyclerView operation
根据RecyclerView.Adapter的documentation:当必须创建新的可重用单元格时,调用方法
createViewHolder()
(取决于可以在屏幕上显示的单元格数量) . 所以表演if语句不会对表现产生太大影响 . 如果屏幕上显示10个单元格,onCreateViewHolder()
可能会再调用10次 . 问题可能出在每次RecyclerView需要显示新单元格时调用的onBindViewHolder()
上 . 因此,如果您有一个包含100个元素的列表,并且您的用户会向底部移动,那么系统将比通常执行100个if语句 .此外,我认为如果为每个列表制作单个适配器,您的代码将更易于维护 .
我将通过以下事实得出结论:我不确定这是一种很好的编码方式 . 我建议你通过在
onCreateViewHolder
和onBindViewHolder
上放置断点来测试RecyclerView生命周期,以了解它们何时被调用 .我希望它对你有所帮助!祝你好运,玩得开心