onCreateViewHolder,这会导致用户第一次滚动时出现滞后。
应该是这样吗?我以为onCreateViewHolder只能为每个viewType调用一次。
这是我的适配器:
public class CompetitionTableAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final CompetitionAdapterOnItemClickHandler mClickHandler;
private List<CompetitionTableItem> mTable;
int calls = 0; //Used to see how many times onCreateViewHolder is called
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
calls++;
if(viewType == CompetitionTableItem.TYPE_HEADER){
View view = LayoutInflater.from(mContext).inflate(R.layout.table_header, viewGroup, false);
Log.i("CompetitionTableAdapter", "onCreateViewHolder "+calls);
return new CompetitionAdapterHeaderViewHolder(view);
}else{
View view = LayoutInflater.from(mContext).inflate(R.layout.table_item, viewGroup, false);
view.setFocusable(true);
Log.i("CompetitionTableAdapter", "onCreateViewHolder "+calls);
return new CompetitionAdapterViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
int type = getItemViewType(position);
if (type == CompetitionTableItem.TYPE_HEADER) {
HeaderItem header = (HeaderItem) mTable.get(position);
CompetitionAdapterHeaderViewHolder holder = (CompetitionAdapterHeaderViewHolder) viewHolder;
holder.headerTextView.setText(header.getTitle());
} else {
CompetitionAdapterViewHolder holder = (CompetitionAdapterViewHolder) viewHolder;
TeamItem currentTeamTableEntity = (TeamItem)mTable.get(position);
TableEntryEntity tableEntry = currentTeamTableEntity.getTableEntryEntity();
holder.teamNameTextView.setText(tableEntry.getTeam().getName());
holder.pointsTextView.setText(String.valueOf(tableEntry.getPoints()));
holder.positionTextView.setText(String.valueOf(tableEntry.getPosition()));
holder.playedTextView.setText(String.valueOf(tableEntry.getPlayedGames()));
holder.lostTextView.setText(String.valueOf(tableEntry.getLost()));
holder.drawnTextView.setText(String.valueOf(tableEntry.getDraw()));
holder.wonTextView.setText(String.valueOf(tableEntry.getWon()));
String crestUrl = NetworkUtils.getCrestUrl(tableEntry.getTeam().getId(),NetworkUtils.IMAGE_QUALITY_SD);
Picasso.get().load(crestUrl).placeholder(R.drawable.default_crest).error(R.drawable.default_crest).into(holder.crestView);
}
}
@Override
public long getItemId(int position) {
int type = getItemViewType(position);
if (type == CompetitionTableItem.TYPE_HEADER) {
return NO_ID;
}else{
return ((TeamItem)mTable.get(position)).getTableEntryEntity().getTeam().getId();
}
}
public void swapTable(final List<CompetitionTableItem> table) {
mTable = table;
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mTable.get(position).getType();
}
public interface CompetitionAdapterOnItemClickHandler {
void onItemClick(TableEntryEntity competition);
}
class CompetitionAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
final ImageView crestView;
final TextView teamNameTextView;
final TextView positionTextView;
final TextView pointsTextView;
final TextView playedTextView;
final TextView wonTextView;
final TextView drawnTextView;
final TextView lostTextView;
CompetitionAdapterViewHolder(View view) {
super(view);
crestView = view.findViewById(R.id.team_crest);
teamNameTextView = view.findViewById(R.id.team_name);
pointsTextView = view.findViewById(R.id.team_points);
positionTextView = view.findViewById(R.id.team_position);
playedTextView = view.findViewById(R.id.matches_played);
wonTextView = view.findViewById(R.id.matches_won);
drawnTextView = view.findViewById(R.id.matches_drawn);
lostTextView = view.findViewById(R.id.matches_lost);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
TableEntryEntity tableEntryEntity = ((TeamItem)mTable.get(adapterPosition)).getTableEntryEntity();
mClickHandler.onItemClick(tableEntryEntity);
}
}
class CompetitionAdapterHeaderViewHolder extends RecyclerView.ViewHolder {
final TextView headerTextView;
CompetitionAdapterHeaderViewHolder(View view) {
super(view);
headerTextView = view.findViewById(R.id.headerText);
}
}
}
还有RecyclerView所在的片段:https://github.com/s6joui/football-data-android-java/blob/master/app/src/main/java/tech/joeyck/livefootball/ui/BaseListFragment.java
完整的源代码:https://github.com/s6joui/football-data-android-java
我正在使用androidx recyclerview版本1.1.0-alpha01