在我的代码中,我使用recylcer视图试图显示一些数据。
滚动后,图像总是乱糟糟的。
这是我的一些代码。
public class LeaderBoardAdapter extends UltimateViewAdapter<RecyclerView.ViewHolder> {
private List<Profile> mProfilesList;
private BaseActivity mBaseActivity;
public LeaderBoardAdapter(final BaseActivity pBaseActivity, final List<Profile> pProfilesList) {
mProfilesList = pProfilesList;
mBaseActivity = pBaseActivity;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder pHolder, final int position) {
final ViewHolder holder = (ViewHolder) pHolder;
holder.paginationTextView.setText(mProfilesList.get(position).getPosition() + ".");
holder.nameTextView.setText(mProfilesList.get(position).getProfile().getName());
if (!TextUtils.isEmpty(mProfilesList.get(position).getProfile().getImage())) {
Picasso
.with(mBaseActivity)
.load(mProfilesList.get(position).getProfile().getImage())
.placeholder(R.drawable.default_avatar)
.error(R.drawable.default_avatar)
.into(holder.userImageView);
} else {
holder.userImageView.setBackgroundResource(R.drawable.img_edit_profile_button);
}
if (position == 0 || position == 1 || position == 2) {
holder.layout.setBackgroundColor(mBaseActivity.getResources().getColor(R.color.white));
} else {
holder.layout.setBackgroundColor(mBaseActivity.getResources().getColor(R.color.white_transparent));
}
}
@Override
public int getAdapterItemCount() {
return mProfilesList.size();
}
@Override
public int getItemCount() {
return mProfilesList.size();
}
@Override
public RecyclerView.ViewHolder getViewHolder(View view) {
return new UltimateRecyclerviewViewHolder(view);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.leaders_board_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public long generateHeaderId(int position) {
return 0;
}
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.leaders_board_item, viewGroup, false);
return new RecyclerView.ViewHolder(view) {
};
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
}
class ViewHolder extends UltimateRecyclerviewViewHolder {
TextView paginationTextView;
TextView nameTextView;
TextView securityIndexValueTextView;
CircularImageView userImageView;
ImageView securityIndexImageView;
LinearLayout layout;
public ViewHolder(View itemView) {
super(itemView);
paginationTextView = (TextView) itemView.findViewById(R.id.leaders_board_item_pagination_text_view);
nameTextView = (TextView) itemView.findViewById(R.id.leaders_board_item_name_text_view);
securityIndexValueTextView = (TextView) itemView.findViewById(R.id.leaders_board_item_security_index_value_text_view);
userImageView = (CircularImageView) itemView.findViewById(R.id.leaders_board_item_user_image_view);
securityIndexImageView = (ImageView) itemView.findViewById(R.id.leaders_board_item_security_index_image_view);
layout = (LinearLayout) itemView.findViewById(R.id.leaders_board_item_layout);
}
}
}
这里有什么问题可以告诉我吗?
答案 0 :(得分:1)
您未能将观看者回收考虑在内。你需要的是像
if (!TextUtils.isEmpty(mProfilesList.get(position).getProfile().getImage())) {
Picasso.with(mBaseActivity)
.load(mProfilesList.get(position).getProfile().getImage())
.placeholder(R.drawable.default_avatar)
.error(R.drawable.default_avatar)
.into(holder.userImageView);
} else {
// Cancel any pending requests to load an image into the imageview
Picasso.with(mBaseActivity).cancelRequest(holder.userImageView);
// Set the imageview to the default avatar
holder.userImageView.setImageResource(R.drawable.default_avatar);
holder.userImageView.setBackgroundResource(R.drawable.img_edit_profile_button);
}
答案 1 :(得分:0)
final ViewHolder holder = (ViewHolder) pHolder;
这一行让我觉得你实施有问题。 你不应该需要它来施展它。
请检查基本示例。
另一个问题可能是毕加索会使用缓存机制。我记得应该有防止它的方法。
如果您可以发布完整的适配器代码,我们可以查看更多
祝你好运