在性能方面,在Android应用中实现以下哪种选项更好:
- 为每个对象列表使用多个适配器,每个适配器具有不同的布局
- 使用单个适配器处理不同的数据
假设我们需要创建:
- Comments,
Videos,
Questions
lists in adapter to display data properly. Each list belongs to different layout, so that we have different
TextViews
,ImageViews
and so on.public class FilesAdapter extends 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;
private List videoFiles; private List comments; private List questions;
boolean isForUser;
public FilesAdapter(int currentAdapter, List videoFiles, boolean isForUser){ this.videoFiles = videoFiles; this.CURRENT_ADAPTER = currentAdapter; this.isForUser = isForUser; }
public FilesAdapter(int currentAdapter, List comments){ this.comments = comments; this.CURRENT_ADAPTER = currentAdapter; }
public FilesAdapter(int currentAdapter, List questions, int unusedVariable){ this.questions = questions; this.CURRENT_ADAPTER = currentAdapter; }
@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 extends 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();
}
就性能而言,实现哪种方法更好?如果我决定为不同的列表实现一个适配器,是否会出现明显的延迟或性能问题?