英文:
Binded ImageView disappears when scrolling RecyclerView
问题
我在我的应用程序中创建了一个排行榜活动,在其中通过 RecyclerView
显示前25名用户。
当加载完成时,它的显示效果如下:
然而,出现了一个问题,当我滚动 RecyclerView
时,奖牌消失了,显示如下:
我的适配器是:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
storageRef = FirebaseStorage.getInstance().getReference();
otherUserID = usersID.get(position);
otherUserName = usersName.get(position);
if (tabCase == 0) {
String result = String.format("%.2f", usersResult.get(position));
otherUserResult = result;
} else {
otherUserResult = String.format("%.0f", usersResult.get(position));
}
holder.tv_PersonName.setText(otherUserName);
holder.tv_Result.setText(otherUserResult);
holder.tv_Ranking.setText(Integer.toString(position + 1));
if (position == 0) {
holder.iv_Medal.setImageResource(R.drawable.ic_gold_medal);
} else if (position == 1) {
holder.iv_Medal.setImageResource(R.drawable.ic_silver_medal);
} else if (position == 2) {
holder.iv_Medal.setImageResource(R.drawable.ic_bronze_medal);
} else {
holder.iv_Medal.setVisibility(View.GONE);
}
storageRef.child("/Images/" + otherUserID + ".jpg").getDownloadUrl().addOnSuccessListener(uri -> Glide.with(mView.getContext()).load(uri.toString()).into(holder.iv_PersonPic)).addOnFailureListener(e -> {
Glide.clear(holder.iv_PersonPic);
holder.iv_PersonPic.setImageDrawable(null);
});
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(mView.getContext(), UsersPreviewActivity.class);
intent.putExtra(AppConstants.PREVIEW_KEY, otherUserID);
mView.getContext().startActivity(intent);
});
}
有办法让这些奖牌始终固定在前三名吗?
谢谢
英文:
I've created a Leaderboard activity in my app where I show the top 25 users in a RecyclerView
.
It all works well when it is loaded and it appears as:
However, for some reason, when I scroll the RecyclerView
it removes the medals and it shows:
My adapter is:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
storageRef = FirebaseStorage.getInstance().getReference();
otherUserID = usersID.get( position );
otherUserName = usersName.get( position );
if (tabCase == 0) {
String result = String.format( "%.2f", usersResult.get( position ) );
otherUserResult = result;
} else {
otherUserResult = String.format( "%.0f", usersResult.get( position ) );
}
holder.tv_PersonName.setText( otherUserName );
holder.tv_Result.setText( otherUserResult );
holder.tv_Ranking.setText( Integer.toString( position + 1 ) );
if (position == 0) {
holder.iv_Medal.setImageResource( R.drawable.ic_gold_medal );
} else if (position == 1) {
holder.iv_Medal.setImageResource( R.drawable.ic_silver_medal );
} else if (position == 2) {
holder.iv_Medal.setImageResource( R.drawable.ic_bronze_medal );
} else {
holder.iv_Medal.setVisibility( View.GONE );
}
storageRef.child( "/Images/" + otherUserID + ".jpg" ).getDownloadUrl().addOnSuccessListener( uri -> Glide.with( mView.getContext() ).load( uri.toString() ).into( holder.iv_PersonPic ) ).addOnFailureListener( e -> {
Glide.clear( holder.iv_PersonPic );
holder.iv_PersonPic.setImageDrawable( null );
} );
holder.itemView.setOnClickListener( v -> {
Intent intent = new Intent( mView.getContext(), UsersPreviewActivity.class );
intent.putExtra( AppConstants.PREVIEW_KEY, otherUserID );
mView.getContext().startActivity( intent );
} );
}
Any way to make those medals stick to the first 3?
Thank you
答案1
得分: 0
在其他条件中调用以下代码,并将视图设置为可见:
holder.iv_Medal.setVisibility(View.VISIBLE);
类似于:
if (position == 0) {
holder.iv_Medal.setImageResource(R.drawable.ic_gold_medal);
holder.iv_Medal.setVisibility(View.VISIBLE);
} else if (position == 1) {
holder.iv_Medal.setImageResource(R.drawable.ic_silver_medal);
holder.iv_Medal.setVisibility(View.VISIBLE);
} else if (position == 2) {
holder.iv_Medal.setImageResource(R.drawable.ic_bronze_medal);
holder.iv_Medal.setVisibility(View.VISIBLE);
} else {
holder.iv_Medal.setVisibility(View.GONE);
}
英文:
call the following in other if conditions and set the view to VISIBLE
holder.iv_Medal.setVisibility( View.VISIBLE);
like so:
if (position == 0) {
holder.iv_Medal.setImageResource( R.drawable.ic_gold_medal );
holder.iv_Medal.setVisibility( View.VISIBLE);
} else if (position == 1) {
holder.iv_Medal.setImageResource( R.drawable.ic_silver_medal );
holder.iv_Medal.setVisibility( View.VISIBLE);
} else if (position == 2) {
holder.iv_Medal.setImageResource( R.drawable.ic_bronze_medal );
holder.iv_Medal.setVisibility( View.VISIBLE);
} else {
holder.iv_Medal.setVisibility( View.GONE );
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论